d48db147a00784c7891c4359efb704fb98aa76a5
[mono.git] / mcs / class / referencesource / SMDiagnostics / System / ServiceModel / Diagnostics / PiiTraceSource.cs
1 //------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //------------------------------------------------------------
4
5 namespace System.ServiceModel.Diagnostics
6 {
7     using System.Configuration;
8     using System.Diagnostics;
9     using System.Runtime;
10     using System.Runtime.Diagnostics;
11     using System.ServiceModel.Configuration;
12
13     class PiiTraceSource : TraceSource
14     {
15         string eventSourceName = String.Empty;
16         internal const string LogPii = "logKnownPii";
17         bool shouldLogPii = false;
18         bool initialized = false;
19         object localSyncObject = new object();
20
21         internal PiiTraceSource(string name, string eventSourceName)
22             : base(name)
23         { 
24 #pragma warning disable 618
25             Fx.Assert(!String.IsNullOrEmpty(eventSourceName), "Event log source name must be valid");
26 #pragma warning restore 618
27             this.eventSourceName = eventSourceName;
28         }
29
30         internal PiiTraceSource(string name, string eventSourceName, SourceLevels levels)
31             : base(name, levels)
32         {
33 #pragma warning disable 618
34             Fx.Assert(!String.IsNullOrEmpty(eventSourceName), "Event log source name must be valid");
35 #pragma warning restore 618
36             this.eventSourceName = eventSourceName;
37         }
38
39         void Initialize()
40         {
41             if (!this.initialized)
42             {
43                 lock (localSyncObject)
44                 {
45                     if (!this.initialized)
46                     {
47                         string attributeValue = this.Attributes[PiiTraceSource.LogPii];
48                         bool shouldLogPii = false;
49                         if (!string.IsNullOrEmpty(attributeValue))
50                         {
51                             if (!bool.TryParse(attributeValue, out shouldLogPii))
52                             {
53                                 shouldLogPii = false;
54                             }
55                         }
56
57                         if (shouldLogPii)
58                         {
59 #pragma warning disable 618
60                             System.Runtime.Diagnostics.EventLogger logger = new System.Runtime.Diagnostics.EventLogger(this.eventSourceName, null);
61 #pragma warning restore 618
62                             if (MachineSettingsSection.EnableLoggingKnownPii)
63                             {
64                                 logger.LogEvent(TraceEventType.Information,
65                                     (ushort)System.Runtime.Diagnostics.EventLogCategory.MessageLogging,
66                                     (uint)System.Runtime.Diagnostics.EventLogEventId.PiiLoggingOn,
67                                     false);
68                                 this.shouldLogPii = true;
69                             }
70                             else
71                             {
72                                 logger.LogEvent(TraceEventType.Error,
73                                         (ushort)System.Runtime.Diagnostics.EventLogCategory.MessageLogging,
74                                         (uint)System.Runtime.Diagnostics.EventLogEventId.PiiLoggingNotAllowed,
75                                         false);
76                             }
77                         }
78                         this.initialized = true;
79                     }
80                 }
81             }
82         }
83
84         protected override string[] GetSupportedAttributes()
85         {
86             return new string[] { PiiTraceSource.LogPii };
87         }
88
89         internal bool ShouldLogPii
90         {
91             get
92             {
93                 // ShouldLogPii is called very frequently, don't call Initialize unless we have to.
94                 if (!this.initialized)
95                 {
96                     Initialize();
97                 }
98                 return this.shouldLogPii;
99             }
100             set
101             {
102                 // If you call this, you know what you're doing
103                 this.initialized = true;
104                 this.shouldLogPii = value;
105             }
106         }
107     }
108 }