Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect EventSource::IsSupported setting in more codepaths #51869

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ partial class {ec.ClassName}
{{");
GenerateConstructor(ec);

GenerateLogSingleton(ec.ClassName);

GenerateProviderMetadata(ec.SourceName);

_builder.AppendLine($@"
Expand All @@ -68,6 +70,15 @@ private void GenerateConstructor(EventSourceClass ec)
{
_builder.AppendLine($@"
private {ec.ClassName}() : base(new Guid({ec.Guid.ToString("x").Replace("{", "").Replace("}", "")}), ""{ec.SourceName}"") {{ }}");

_builder.AppendLine($@"
private {ec.ClassName}(bool _) : base() {{ }}");
}

private void GenerateLogSingleton(string className)
{
_builder.AppendLine($@"
public static readonly {className} Log = IsSupported ? new() : new(false);");
}

private void GenerateProviderMetadata(string sourceName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ public partial class EventSourceGenerator : ISourceGenerator
// {
// private RuntimeEventSource() : base(new Guid(0x49592c0f,0x5a05,0x516d,0xaa,0x4b,0xa6,0x4e,0x02,0x02,0x6c,0x89), "System.Runtime") { }
//
// private RuntimeEventSource(bool _) : base() { }
//
// public static readonly RuntimeEventSource Log = IsSupported ? new() : new(false);
//
// private protected override ReadOnlySpan<byte> ProviderMetadata => new byte[] { 0x11, 0x0, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x52, 0x75, 0x6e, 0x74, 0x69, 0x6d, 0x65, 0x0, };
// }
// }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ internal sealed partial class ArrayPoolEventSource : EventSource
#if !ES_BUILD_STANDALONE
private const string EventSourceSuppressMessage = "Parameters to this method are primitive and are trimmer safe";
#endif
internal static readonly ArrayPoolEventSource Log = new ArrayPoolEventSource();

/// <summary>Bucket ID used when renting/returning an array that's too large for a pool.</summary>
internal const int NoBucketId = -1;

Expand All @@ -38,10 +36,6 @@ internal enum BufferDroppedReason : int
OverMaximumSize,
}

// Parameterized constructor to block initialization and ensure the EventSourceGenerator is creating the default constructor
// as you can't make a constructor partial.
private ArrayPoolEventSource(int _) { }

/// <summary>
/// Event for when a buffer is rented. This is invoked once for every successful call to Rent,
/// regardless of whether a buffer is allocated or a buffer is taken from the pool. In a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,9 @@ public static void SendCommand(EventSource eventSource, EventCommand command, ID
/// </summary>
public override string ToString()
{
if (!IsSupported)
return string.Empty;

return SR.Format(SR.EventSource_ToString, Name, Guid);
}

Expand Down Expand Up @@ -666,8 +669,15 @@ public static void SetCurrentThreadActivityId(Guid activityId, out Guid oldActiv
/// the ETW provider name.
/// </summary>
protected EventSource()
: this(EventSourceSettings.EtwManifestEventFormat)
{
if (!IsSupported)
return;

#if FEATURE_PERFTRACING
m_eventHandleTable = new TraceLoggingEventHandleTable();
#endif
m_config = EventSourceSettings.EtwManifestEventFormat;
Initialize(null);
}

/// <summary>
Expand Down Expand Up @@ -701,19 +711,23 @@ protected EventSource(EventSourceSettings settings) : this(settings, null) { }
/// <param name="traits">A collection of key-value strings (must be an even number).</param>
protected EventSource(EventSourceSettings settings, params string[]? traits)
{
if (IsSupported)
{
if (!IsSupported)
return;

#if FEATURE_PERFTRACING
m_eventHandleTable = new TraceLoggingEventHandleTable();
m_eventHandleTable = new TraceLoggingEventHandleTable();
#endif
m_config = ValidateSettings(settings);
m_config = ValidateSettings(settings);
Initialize(traits);
}

Type myType = this.GetType();
Guid eventSourceGuid = GetGuid(myType);
string eventSourceName = GetName(myType);
private void Initialize(string[]? traits)
{
Type myType = this.GetType();
Guid eventSourceGuid = GetGuid(myType);
string eventSourceName = GetName(myType);

Initialize(eventSourceGuid, eventSourceName, traits);
}
Initialize(eventSourceGuid, eventSourceName, traits);
}

#if FEATURE_PERFTRACING
Expand Down Expand Up @@ -1546,6 +1560,8 @@ internal EventSource(Guid eventSourceGuid, string eventSourceName, EventSourceSe
/// </summary>
private unsafe void Initialize(Guid eventSourceGuid, string eventSourceName, string[]? traits)
{
Debug.Assert(IsSupported);

try
{
m_traits = traits;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ internal sealed partial class FrameworkEventSource : EventSource
#if !ES_BUILD_STANDALONE
private const string EventSourceSuppressMessage = "Parameters to this method are primitive and are trimmer safe";
#endif
public static readonly FrameworkEventSource Log = new FrameworkEventSource();

// Keyword definitions. These represent logical groups of events that can be turned on and off independently
// Often each task has a keyword, but where tasks are determined by subsystem, keywords are determined by
Expand All @@ -33,10 +32,6 @@ public static class Keywords
public const EventTask ThreadTransfer = (EventTask)3;
}

// Parameterized constructor to block initialization and ensure the EventSourceGenerator is creating the default constructor
// as you can't make a constructor partial.
private FrameworkEventSource(int _) { }

// optimized for common signatures (used by the ThreadTransferSend/Receive events)
#if !ES_BUILD_STANDALONE
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:UnrecognizedReflectionPattern",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ namespace System.Diagnostics.Tracing
internal sealed partial class NativeRuntimeEventSource : EventSource
{
internal const string EventSourceName = "Microsoft-Windows-DotNETRuntime";
public static readonly NativeRuntimeEventSource Log = new NativeRuntimeEventSource();

// Parameterized constructor to block initialization and ensure the EventSourceGenerator is creating the default constructor
// as you can't make a constructor partial.
private NativeRuntimeEventSource(int _) { }

#if FEATURE_PERFTRACING
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ public static void Initialize()
s_RuntimeEventSource = new RuntimeEventSource();
}

// Parameterized constructor to block initialization and ensure the EventSourceGenerator is creating the default constructor
// as you can't make a constructor partial.
private RuntimeEventSource(int _) { }

protected override void OnEventCommand(EventCommandEventArgs command)
{
if (command.Command == EventCommand.Enable)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,6 @@ protected override void OnEventCommand(EventCommandEventArgs command)
DebugActivityId = IsEnabled(EventLevel.Informational, Keywords.DebugActivityId);
}

/// <summary>
/// Defines the singleton instance for the TPL ETW provider.
/// </summary>
public static readonly TplEventSource Log = new TplEventSource();

// Parameterized constructor to block initialization and ensure the EventSourceGenerator is creating the default constructor
// as you can't make a constructor partial.
private TplEventSource(int _) { }

/// <summary>Configured behavior of a task wait operation.</summary>
public enum TaskWaitBehavior : int
{
Expand Down