Skip to content

Commit

Permalink
Enable ETW error logging for S.SM.Federation (#4391)
Browse files Browse the repository at this point in the history
Co-authored-by: Matt Connew <matt.connew@microsoft.com>
  • Loading branch information
mconnew and Matt Connew authored Oct 14, 2020
1 parent 25e7e59 commit 1b395bf
Show file tree
Hide file tree
Showing 28 changed files with 895 additions and 755 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,20 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.


using System.Diagnostics.CodeAnalysis;
using System.Runtime;
using System.Runtime.Diagnostics;

namespace System
{
internal static partial class FxTrace
internal static class FxTrace
{
private static ExceptionTrace s_exceptionTrace;
private const string baseEventSourceName = "System.ServiceModel";
private const string EventSourceVersion = "4.0.0.0";

private static Guid s_etwProviderId;
private static string s_eventSourceName;
private static EtwDiagnosticTrace s_diagnosticTrace;
private static ExceptionTrace s_exceptionTrace;
private static readonly object s_lockObject = new object();

[SuppressMessage(FxCop.Category.Performance, FxCop.Rule.AvoidUncalledPrivateCode,
Justification = "This template is shared across all assemblies, some of which use this accessor.")]


public static ExceptionTrace Exception
{
get
Expand All @@ -38,18 +30,6 @@ public static ExceptionTrace Exception
}
}

[SuppressMessage(FxCop.Category.Performance, FxCop.Rule.AvoidUncalledPrivateCode,
Justification = "This template is shared across all assemblies, some of which use this accessor.")]
public static EtwDiagnosticTrace Trace
{
get
{
EnsureEtwProviderInitialized();
return FxTrace.s_diagnosticTrace;
}
}


private static string EventSourceName
{
get
Expand All @@ -63,31 +43,33 @@ private static string EventSourceName
}
}

[SuppressMessage(FxCop.Category.ReliabilityBasic, FxCop.Rule.UseNewGuidHelperRule,
Justification = "This is a method that creates ETW provider passing Guid Provider ID.")]
private static EtwDiagnosticTrace InitializeTracing()
public static EtwDiagnosticTrace Trace
{
//Etw tracing is switched off by not enabling the session
s_etwProviderId = EtwDiagnosticTrace.DefaultEtwProviderId;

EtwDiagnosticTrace trace = new EtwDiagnosticTrace(baseEventSourceName, s_etwProviderId);

return trace;
get
{
EnsureEtwProviderInitialized();
return s_diagnosticTrace;
}
}


private static void EnsureEtwProviderInitialized()
{
if (null == FxTrace.s_diagnosticTrace)
if (null == s_diagnosticTrace)
{
lock (FxTrace.s_lockObject)
lock (s_lockObject)
{
if (null == FxTrace.s_diagnosticTrace)
if (null == s_diagnosticTrace)
{
FxTrace.s_diagnosticTrace = InitializeTracing();
s_diagnosticTrace = InitializeTracing();
}
}
}
}

private static EtwDiagnosticTrace InitializeTracing()
{
EtwDiagnosticTrace trace = new EtwDiagnosticTrace();
return trace;
}
}
}
17 changes: 17 additions & 0 deletions src/Common/src/Internals/System/Runtime/AssertHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Diagnostics;

namespace System.Runtime
{
internal static class AssertHelper
{
internal static void FireAssert(string message)
{
Debug.Assert(false, message);
}
}
}

19 changes: 19 additions & 0 deletions src/Common/src/Internals/System/Runtime/DiagnosticStrings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

namespace System.Runtime
{
internal static partial class DiagnosticStrings
{
internal const string DataTag = "Data";
internal const string DataItemsTag = "DataItems";
internal const string ExceptionStringTag = "ExceptionString";
internal const string ExceptionTag = "Exception";
internal const string ExceptionTypeTag = "ExceptionType";
internal const string KeyTag = "Key";
internal const string InnerExceptionTag = "InnerException";
internal const string MessageTag = "Message";
internal const string StackTraceTag = "StackTrace";
internal const string ValueTag = "Value";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Diagnostics;
using System.Text;

namespace System.Runtime.Diagnostics
{
internal abstract partial class DiagnosticTraceBase
{
public DiagnosticTraceBase()
{
}

//only used for exceptions, perf is not important
public static string XmlEncode(string text)
{
if (string.IsNullOrEmpty(text))
{
return text;
}

int len = text.Length;
StringBuilder encodedText = new StringBuilder(len + 8); //perf optimization, expecting no more than 2 > characters

for (int i = 0; i < len; ++i)
{
char ch = text[i];
switch (ch)
{
case '<':
encodedText.Append("&lt;");
break;
case '>':
encodedText.Append("&gt;");
break;
case '&':
encodedText.Append("&amp;");
break;
default:
encodedText.Append(ch);
break;
}
}
return encodedText.ToString();
}

protected static string StackTraceString(Exception exception)
{
string retval = exception.StackTrace;
if (string.IsNullOrEmpty(retval))
{
// This means that the exception hasn't been thrown yet. We need to manufacture the stack then.
StackTrace stackTrace = new StackTrace(false);
// Figure out how many frames should be throw away
StackFrame[] stackFrames = stackTrace.GetFrames();

int frameCount = 0;
bool breakLoop = false;
foreach (StackFrame frame in stackFrames)
{
string methodName = frame.GetMethod().Name;
switch (methodName)
{
case "StackTraceString":
case "AddExceptionToTraceString":
case "BuildTrace":
case "TraceEvent":
case "TraceException":
case "GetAdditionalPayload":
++frameCount;
break;
default:
if (methodName.StartsWith("ThrowHelper", StringComparison.Ordinal))
{
++frameCount;
}
else
{
breakLoop = true;
}
break;
}
if (breakLoop)
{
break;
}
}

stackTrace = new StackTrace(frameCount, false);
retval = stackTrace.ToString();
}
return retval;
}
}
}
Loading

0 comments on commit 1b395bf

Please sign in to comment.