PageRenderTime 21ms CodeModel.GetById 16ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/WCFWebApi/src/Microsoft.Server.Common/Microsoft/Server/Common/TraceLevelHelper.cs

#
C# | 96 lines | 77 code | 6 blank | 13 comment | 3 complexity | f7afce1dcfed7fdabcb2a24941e22c52 MD5 | raw file
 1//----------------------------------------------------------------
 2// Copyright (c) Microsoft Corporation.  All rights reserved.
 3//----------------------------------------------------------------
 4
 5namespace Microsoft.Server.Common
 6{
 7    using System.Diagnostics;
 8
 9    /// <remarks>
10    /// dmetzgar (11/15/10, CSDMain 194940) - Previously, this code first checked that the opcode was set to informational.  If not, it would check
11    /// the opcode name for start, stop, suspend, or resume and use that or return Information otherwise.  This does not work well with the latest 
12    /// ETW changes where almost every event has a task and opcode.  With the old logic, if an opcode is set on the event with a level such as 
13    /// warning or error, the level would be incorrectly reported in diagnostic tracing as informational.  Also, start/stop/suspend/resume events 
14    /// have an overloaded meaning in diagnostic tracing that the svctraceviewer would misinterpret.  To keep diagnostic tracing from breaking, this
15    /// class now checks for start/stop/suspend/resume opcodes and returns the level if any of those do not match.  Furthermore, any events defined 
16    /// that are shared between diagnostics and ETW should not use start/stop/suspend/resume opcodes unless explicitly intended for use in 
17    /// diagnostics tracing.
18    /// </remarks>
19    public static class TraceLevelHelper
20    {
21        static TraceEventType[] EtwLevelToTraceEventType = { TraceEventType.Critical, TraceEventType.Critical, TraceEventType.Error,
22                TraceEventType.Warning, TraceEventType.Information, TraceEventType.Verbose
23            };
24
25        public static TraceEventType GetTraceEventType(byte level, byte opCode)
26        {
27            switch (opCode)
28            {
29                case (byte)TraceEventOpcode.Start:
30                    return TraceEventType.Start;
31                case (byte)TraceEventOpcode.Stop:
32                    return TraceEventType.Stop;
33                case (byte)TraceEventOpcode.Suspend:
34                    return TraceEventType.Suspend;
35                case (byte)TraceEventOpcode.Resume:
36                    return TraceEventType.Resume;
37                default:
38                    return EtwLevelToTraceEventType[(int)level];
39            }
40        }
41
42        public static TraceEventType GetTraceEventType(TraceEventLevel level)
43        {
44            return EtwLevelToTraceEventType[(int)level];
45        }
46
47        public static TraceEventType GetTraceEventType(byte level)
48        {
49            return EtwLevelToTraceEventType[(int)level];
50        }
51
52        public static string LookupSeverity(TraceEventLevel level, TraceEventOpcode opCode)
53        {
54            string severity;
55            switch (opCode)
56            {
57                case TraceEventOpcode.Start:
58                    severity = "Start";
59                    break;
60                case TraceEventOpcode.Stop:
61                    severity = "Stop";
62                    break;
63                case TraceEventOpcode.Suspend:
64                    severity = "Suspend";
65                    break;
66                case TraceEventOpcode.Resume:
67                    severity = "Resume";
68                    break;
69                default:
70                    switch (level)
71                    {
72                        case TraceEventLevel.Critical:
73                            severity = "Critical";
74                            break;
75                        case TraceEventLevel.Error:
76                            severity = "Error";
77                            break;
78                        case TraceEventLevel.Warning:
79                            severity = "Warning";
80                            break;
81                        case TraceEventLevel.Informational:
82                            severity = "Information";
83                            break;
84                        case TraceEventLevel.Verbose:
85                            severity = "Verbose";
86                            break;
87                        default:
88                            severity = level.ToString();
89                            break;
90                    }
91                    break;
92            }
93            return severity;
94        }
95    }
96}