File: LegacyDiagnosticTrace.cs

package info (click to toggle)
mono 6.14.1%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,282,732 kB
  • sloc: cs: 11,182,461; xml: 2,850,281; ansic: 699,123; cpp: 122,919; perl: 58,604; javascript: 30,841; asm: 21,845; makefile: 19,602; sh: 10,973; python: 4,772; pascal: 925; sql: 859; sed: 16; php: 1
file content (310 lines) | stat: -rw-r--r-- 12,468 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
//------------------------------------------------------------

namespace System.ServiceModel.Diagnostics
{
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Configuration;
    using System.Diagnostics;
    using System.Globalization;
    using System.Runtime;
    using System.Runtime.Diagnostics;
    using System.Security;
    using System.Security.Permissions;
    using System.Text;
    using System.Xml;
    using System.Diagnostics.CodeAnalysis;

    class LegacyDiagnosticTrace : DiagnosticTraceBase
    {
        const int MaxTraceSize = 65535;
        bool shouldUseActivity = false;
        TraceSourceKind traceSourceType = TraceSourceKind.PiiTraceSource;
        const string subType = "";
        const string version = "1";
        const int traceFailureLogThreshold = 1;
        const SourceLevels DefaultLevel = SourceLevels.Off;
        static object classLockObject = new object();

        protected override void OnSetLevel(SourceLevels level)
        {
            if (this.TraceSource != null)
            {
                if (this.TraceSource.Switch.Level != SourceLevels.Off &&
                    level == SourceLevels.Off)
                {
                    TraceSource temp = this.TraceSource;
                    this.CreateTraceSource();
                    temp.Close();
                }
                this.shouldUseActivity = (level & SourceLevels.ActivityTracing) != 0;
            }
        }

        [Obsolete("For SMDiagnostics.dll use only. Call DiagnosticUtility.ShouldUseActivity instead")]
        internal bool ShouldUseActivity
        {
            get { return this.shouldUseActivity; }
        }

#pragma warning disable 56500
        [Obsolete("For SMDiagnostics.dll use only. Never 'new' this type up unless you are DiagnosticUtility.")]
        [Fx.Tag.SecurityNote(Critical = "Sets eventSourceName.")]
        [SecurityCritical]
        [SuppressMessage(FxCop.Category.Usage, FxCop.Rule.DoNotCallOverridableMethodsInConstructors,
                Justification = "LegacyDiagnosticTrace is an internal class without derived classes")]
        internal LegacyDiagnosticTrace(TraceSourceKind sourceType, string traceSourceName, string eventSourceName)
            : base(traceSourceName)
        {
            this.traceSourceType = sourceType;
            this.EventSourceName = eventSourceName;

            try
            {
                this.CreateTraceSource();
                this.AddDomainEventHandlersForCleanup();
            }
#if !NO_CONFIGURATION
            catch (ConfigurationErrorsException)
            {
                throw;
            }
#endif
            catch (Exception e)
            {
                if (Fx.IsFatal(e))
                {
                    throw;
                }
                System.Runtime.Diagnostics.EventLogger logger = new System.Runtime.Diagnostics.EventLogger(this.EventSourceName, null);
                logger.LogEvent(TraceEventType.Error, (ushort)System.Runtime.Diagnostics.EventLogCategory.Tracing, (uint)System.Runtime.Diagnostics.EventLogEventId.FailedToSetupTracing, false,
                    e.ToString());
            }
        }
#pragma warning restore 56500
        
        [SecuritySafeCritical]
        void CreateTraceSource()
        {
            PiiTraceSource tempSource = null;
            if (this.traceSourceType == TraceSourceKind.PiiTraceSource)
            {
                tempSource = new PiiTraceSource(this.TraceSourceName, this.EventSourceName, LegacyDiagnosticTrace.DefaultLevel);
            }
            else
            {
                tempSource = new DiagnosticTraceSource(this.TraceSourceName, this.EventSourceName, LegacyDiagnosticTrace.DefaultLevel);
            }

            SetTraceSource(tempSource);
        }

#pragma warning disable 56500
        internal void TraceEvent(TraceEventType type, int code, string msdnTraceCode, string description, TraceRecord trace, Exception exception, object source)
        {
#pragma warning disable 618
            Fx.Assert(exception == null || type <= TraceEventType.Information, "Exceptions should be traced at Information or higher");
            Fx.Assert(!string.IsNullOrEmpty(description), "All TraceCodes should have a description");
#pragma warning restore 618
            TraceXPathNavigator navigator = null;
            try
            {
#pragma warning disable 618
                if (this.TraceSource != null && this.HaveListeners)
#pragma warning restore 618
                {
                    try
                    {
                        BuildTrace(type, msdnTraceCode, description, trace, exception, source, out navigator);
                    }
                    catch (PlainXmlWriter.MaxSizeExceededException)
                    {
                        StringTraceRecord codeTraceRecord = new StringTraceRecord("TruncatedTraceId", msdnTraceCode);
                        this.TraceEvent(type, DiagnosticsTraceCode.TraceTruncatedQuotaExceeded, LegacyDiagnosticTrace.GenerateMsdnTraceCode("System.ServiceModel.Diagnostics", "TraceTruncatedQuotaExceeded"), TraceSR.GetString(TraceSR.TraceCodeTraceTruncatedQuotaExceeded), codeTraceRecord, null, null);
                    }
                    this.TraceSource.TraceData(type, code, navigator);
                    if (this.CalledShutdown)
                    {
                        this.TraceSource.Flush();
                    }
                    // Must have been a successful trace.
                    this.LastFailure = DateTime.MinValue;
                }
            }
            catch (Exception e)
            {
                if (Fx.IsFatal(e))
                {
                    throw;
                }
                LogTraceFailure(navigator == null ? string.Empty : navigator.ToString(), e);
            }
        }
#pragma warning restore 56500

        internal void TraceEvent(TraceEventType type, int code, string msdnTraceCode, string description, TraceRecord trace, Exception exception, Guid activityId, object source)
        {
#pragma warning disable 618
            using ((this.ShouldUseActivity && Guid.Empty != activityId) ? Activity.CreateActivity(activityId) : null)
#pragma warning restore 618
            {
                this.TraceEvent(type, code, msdnTraceCode, description, trace, exception, source);
            }
        }

        // helper for standardized trace code generation
        static internal string GenerateMsdnTraceCode(string traceSource, string traceCodeString)
        {
            return string.Format(CultureInfo.InvariantCulture,
                "http://msdn.microsoft.com/{0}/library/{1}.{2}.aspx",
                CultureInfo.CurrentCulture.Name,
                traceSource, traceCodeString);
        }

#pragma warning disable 56500
        internal void TraceTransfer(Guid newId)
        {
#pragma warning disable 618
            if (this.ShouldUseActivity)
#pragma warning restore 618
            {
                Guid oldId = LegacyDiagnosticTrace.ActivityId;
                if (newId != oldId)
                {
#pragma warning disable 618
                    if (this.HaveListeners)
#pragma warning restore 618
                    {
                        try
                        {
                            this.TraceSource.TraceTransfer(0, null, newId);
                        }
                        catch (Exception e)
                        {
                            if (Fx.IsFatal(e))
                            {
                                throw;
                            }
                            LogTraceFailure(null, e);
                        }
                    }
                }
            }
        }

        protected override void OnShutdownTracing()
        {
            if (null != this.TraceSource)
            {
#pragma warning disable 618
                if (this.Level != SourceLevels.Off)
                {
                    if (this.ShouldTrace(TraceEventType.Information))
#pragma warning restore 618
                    {
                        Dictionary<string, string> values = new Dictionary<string, string>(3);
                        values["AppDomain.FriendlyName"] = AppDomain.CurrentDomain.FriendlyName;
                        values["ProcessName"] = DiagnosticTraceBase.ProcessName;
                        values["ProcessId"] = DiagnosticTraceBase.ProcessId.ToString(CultureInfo.CurrentCulture);
                        this.TraceEvent(TraceEventType.Information, DiagnosticsTraceCode.AppDomainUnload, LegacyDiagnosticTrace.GenerateMsdnTraceCode("System.ServiceModel.Diagnostics", "AppDomainUnload"), TraceSR.GetString(TraceSR.TraceCodeAppDomainUnload),
                            new DictionaryTraceRecord(values), null, null);
                    }
                    this.TraceSource.Flush();
                }
            }
        }

        protected override void OnUnhandledException(Exception exception)
        {
            TraceEvent(TraceEventType.Critical, DiagnosticsTraceCode.UnhandledException, "UnhandledException", TraceSR.GetString(TraceSR.UnhandledException), null, exception, null);
        }

        public bool ShouldLogPii
        {
            get
            {
                PiiTraceSource traceSource = this.TraceSource as PiiTraceSource;
                if (traceSource != null)
                {
                    return traceSource.ShouldLogPii;
                }

                return false;
            }

            set
            {
                PiiTraceSource traceSource = this.TraceSource as PiiTraceSource;
                if (traceSource != null)
                {
                    traceSource.ShouldLogPii = value;
                }
            }
        }

        void BuildTrace(TraceEventType type, string msdnTraceCode, string description, TraceRecord trace,
            Exception exception, object source, out TraceXPathNavigator navigator)
        {
            PlainXmlWriter xmlWriter = new PlainXmlWriter(LegacyDiagnosticTrace.MaxTraceSize);
            navigator = xmlWriter.Navigator;

            this.BuildTrace(xmlWriter, type, msdnTraceCode, description, trace, exception, source);

            if (!ShouldLogPii)
            {
                navigator.RemovePii(DiagnosticStrings.HeadersPaths);
            }
        }

        void BuildTrace(PlainXmlWriter xml, TraceEventType type, string msdnTraceCode, string description,
            TraceRecord trace, Exception exception, object source)
        {
            xml.WriteStartElement(DiagnosticStrings.TraceRecordTag);
            xml.WriteAttributeString(DiagnosticStrings.NamespaceTag, LegacyDiagnosticTrace.TraceRecordVersion);
            xml.WriteAttributeString(DiagnosticStrings.SeverityTag, DiagnosticTraceBase.LookupSeverity(type));

            xml.WriteElementString(DiagnosticStrings.TraceCodeTag, msdnTraceCode);
            xml.WriteElementString(DiagnosticStrings.DescriptionTag, description);
            xml.WriteElementString(DiagnosticStrings.AppDomain, DiagnosticTraceBase.AppDomainFriendlyName);

            if (source != null)
            {
                xml.WriteElementString(DiagnosticStrings.SourceTag, CreateSourceString(source));
            }

            if (trace != null)
            {
                xml.WriteStartElement(DiagnosticStrings.ExtendedDataTag);
                xml.WriteAttributeString(DiagnosticStrings.NamespaceTag, trace.EventId);

                trace.WriteTo(xml);

                xml.WriteEndElement();
            }

            if (exception != null)
            {
                xml.WriteStartElement(DiagnosticStrings.ExceptionTag);
                AddExceptionToTraceString(xml, exception);
                xml.WriteEndElement();
            }

            xml.WriteEndElement();
        }

        public override bool IsEnabled()
        {
            return true;
        }

        public override void TraceEventLogEvent(TraceEventType type, TraceRecord traceRecord)
        {
            TraceEvent(type,
                DiagnosticsTraceCode.EventLog, LegacyDiagnosticTrace.GenerateMsdnTraceCode("System.ServiceModel.Diagnostics", "EventLog"),
                TraceSR.GetString(TraceSR.TraceCodeEventLog),
                traceRecord, null, null);
        }
    }
}