1 //------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 //------------------------------------------------------------
5 namespace System.ServiceModel.Diagnostics
7 using System.Configuration;
8 using System.Diagnostics;
10 using System.Runtime.Diagnostics;
11 using System.ServiceModel.Configuration;
13 class PiiTraceSource : TraceSource
15 string eventSourceName = String.Empty;
16 internal const string LogPii = "logKnownPii";
17 bool shouldLogPii = false;
18 bool initialized = false;
19 object localSyncObject = new object();
21 internal PiiTraceSource(string name, string eventSourceName)
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;
30 internal PiiTraceSource(string name, string eventSourceName, SourceLevels levels)
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;
41 if (!this.initialized)
43 lock (localSyncObject)
45 if (!this.initialized)
47 string attributeValue = this.Attributes[PiiTraceSource.LogPii];
48 bool shouldLogPii = false;
49 if (!string.IsNullOrEmpty(attributeValue))
51 if (!bool.TryParse(attributeValue, out shouldLogPii))
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)
64 logger.LogEvent(TraceEventType.Information,
65 (ushort)System.Runtime.Diagnostics.EventLogCategory.MessageLogging,
66 (uint)System.Runtime.Diagnostics.EventLogEventId.PiiLoggingOn,
68 this.shouldLogPii = true;
72 logger.LogEvent(TraceEventType.Error,
73 (ushort)System.Runtime.Diagnostics.EventLogCategory.MessageLogging,
74 (uint)System.Runtime.Diagnostics.EventLogEventId.PiiLoggingNotAllowed,
78 this.initialized = true;
84 protected override string[] GetSupportedAttributes()
86 return new string[] { PiiTraceSource.LogPii };
89 internal bool ShouldLogPii
93 // ShouldLogPii is called very frequently, don't call Initialize unless we have to.
94 if (!this.initialized)
98 return this.shouldLogPii;
102 // If you call this, you know what you're doing
103 this.initialized = true;
104 this.shouldLogPii = value;