Skip to content

Commit 99651c2

Browse files
committed
Respect EventSource::IsSupported setting in more codepaths
1 parent 598a7fd commit 99651c2

File tree

8 files changed

+41
-39
lines changed

8 files changed

+41
-39
lines changed

src/libraries/System.Private.CoreLib/generators/EventSourceGenerator.Emitter.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ partial class {ec.ClassName}
5252
{{");
5353
GenerateConstructor(ec);
5454

55+
GenerateLogSingleton(ec.ClassName);
56+
5557
GenerateProviderMetadata(ec.SourceName);
5658

5759
_builder.AppendLine($@"
@@ -68,6 +70,15 @@ private void GenerateConstructor(EventSourceClass ec)
6870
{
6971
_builder.AppendLine($@"
7072
private {ec.ClassName}() : base(new Guid({ec.Guid.ToString("x").Replace("{", "").Replace("}", "")}), ""{ec.SourceName}"") {{ }}");
73+
74+
_builder.AppendLine($@"
75+
private {ec.ClassName}(bool _) : base() {{ }}");
76+
}
77+
78+
private void GenerateLogSingleton(string className)
79+
{
80+
_builder.AppendLine($@"
81+
public static readonly {className} Log = IsSupported ? new() : new(false);");
7182
}
7283

7384
private void GenerateProviderMetadata(string sourceName)

src/libraries/System.Private.CoreLib/generators/EventSourceGenerator.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ public partial class EventSourceGenerator : ISourceGenerator
2828
// {
2929
// private RuntimeEventSource() : base(new Guid(0x49592c0f,0x5a05,0x516d,0xaa,0x4b,0xa6,0x4e,0x02,0x02,0x6c,0x89), "System.Runtime") { }
3030
//
31+
// private RuntimeEventSource(bool _) : base() { }
32+
//
33+
// public static readonly RuntimeEventSource Log = IsSupported ? new() : new(false);
34+
//
3135
// 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, };
3236
// }
3337
// }

src/libraries/System.Private.CoreLib/src/System/Buffers/ArrayPoolEventSource.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ internal sealed partial class ArrayPoolEventSource : EventSource
1313
#if !ES_BUILD_STANDALONE
1414
private const string EventSourceSuppressMessage = "Parameters to this method are primitive and are trimmer safe";
1515
#endif
16-
internal static readonly ArrayPoolEventSource Log = new ArrayPoolEventSource();
17-
1816
/// <summary>Bucket ID used when renting/returning an array that's too large for a pool.</summary>
1917
internal const int NoBucketId = -1;
2018

@@ -38,10 +36,6 @@ internal enum BufferDroppedReason : int
3836
OverMaximumSize,
3937
}
4038

41-
// Parameterized constructor to block initialization and ensure the EventSourceGenerator is creating the default constructor
42-
// as you can't make a constructor partial.
43-
private ArrayPoolEventSource(int _) { }
44-
4539
/// <summary>
4640
/// Event for when a buffer is rented. This is invoked once for every successful call to Rent,
4741
/// regardless of whether a buffer is allocated or a buffer is taken from the pool. In a

src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,9 @@ public static void SendCommand(EventSource eventSource, EventCommand command, ID
493493
/// </summary>
494494
public override string ToString()
495495
{
496+
if (!IsSupported)
497+
return string.Empty;
498+
496499
return SR.Format(SR.EventSource_ToString, Name, Guid);
497500
}
498501

@@ -666,8 +669,15 @@ public static void SetCurrentThreadActivityId(Guid activityId, out Guid oldActiv
666669
/// the ETW provider name.
667670
/// </summary>
668671
protected EventSource()
669-
: this(EventSourceSettings.EtwManifestEventFormat)
670672
{
673+
if (!IsSupported)
674+
return;
675+
676+
#if FEATURE_PERFTRACING
677+
m_eventHandleTable = new TraceLoggingEventHandleTable();
678+
#endif
679+
m_config = EventSourceSettings.EtwManifestEventFormat;
680+
Initialize(null);
671681
}
672682

673683
/// <summary>
@@ -701,19 +711,23 @@ protected EventSource(EventSourceSettings settings) : this(settings, null) { }
701711
/// <param name="traits">A collection of key-value strings (must be an even number).</param>
702712
protected EventSource(EventSourceSettings settings, params string[]? traits)
703713
{
704-
if (IsSupported)
705-
{
714+
if (!IsSupported)
715+
return;
716+
706717
#if FEATURE_PERFTRACING
707-
m_eventHandleTable = new TraceLoggingEventHandleTable();
718+
m_eventHandleTable = new TraceLoggingEventHandleTable();
708719
#endif
709-
m_config = ValidateSettings(settings);
720+
m_config = ValidateSettings(settings);
721+
Initialize(traits);
722+
}
710723

711-
Type myType = this.GetType();
712-
Guid eventSourceGuid = GetGuid(myType);
713-
string eventSourceName = GetName(myType);
724+
private void Initialize(string[]? traits)
725+
{
726+
Type myType = this.GetType();
727+
Guid eventSourceGuid = GetGuid(myType);
728+
string eventSourceName = GetName(myType);
714729

715-
Initialize(eventSourceGuid, eventSourceName, traits);
716-
}
730+
Initialize(eventSourceGuid, eventSourceName, traits);
717731
}
718732

719733
#if FEATURE_PERFTRACING
@@ -1546,6 +1560,8 @@ internal EventSource(Guid eventSourceGuid, string eventSourceName, EventSourceSe
15461560
/// </summary>
15471561
private unsafe void Initialize(Guid eventSourceGuid, string eventSourceName, string[]? traits)
15481562
{
1563+
Debug.Assert(IsSupported);
1564+
15491565
try
15501566
{
15511567
m_traits = traits;

src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/FrameworkEventSource.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ internal sealed partial class FrameworkEventSource : EventSource
1414
#if !ES_BUILD_STANDALONE
1515
private const string EventSourceSuppressMessage = "Parameters to this method are primitive and are trimmer safe";
1616
#endif
17-
public static readonly FrameworkEventSource Log = new FrameworkEventSource();
1817

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

36-
// Parameterized constructor to block initialization and ensure the EventSourceGenerator is creating the default constructor
37-
// as you can't make a constructor partial.
38-
private FrameworkEventSource(int _) { }
39-
4035
// optimized for common signatures (used by the ThreadTransferSend/Receive events)
4136
#if !ES_BUILD_STANDALONE
4237
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:UnrecognizedReflectionPattern",

src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/NativeRuntimeEventSource.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@ namespace System.Diagnostics.Tracing
1717
internal sealed partial class NativeRuntimeEventSource : EventSource
1818
{
1919
internal const string EventSourceName = "Microsoft-Windows-DotNETRuntime";
20-
public static readonly NativeRuntimeEventSource Log = new NativeRuntimeEventSource();
21-
22-
// Parameterized constructor to block initialization and ensure the EventSourceGenerator is creating the default constructor
23-
// as you can't make a constructor partial.
24-
private NativeRuntimeEventSource(int _) { }
2520

2621
#if FEATURE_PERFTRACING
2722
/// <summary>

src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/RuntimeEventSource.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,6 @@ public static void Initialize()
4545
s_RuntimeEventSource = new RuntimeEventSource();
4646
}
4747

48-
// Parameterized constructor to block initialization and ensure the EventSourceGenerator is creating the default constructor
49-
// as you can't make a constructor partial.
50-
private RuntimeEventSource(int _) { }
51-
5248
protected override void OnEventCommand(EventCommandEventArgs command)
5349
{
5450
if (command.Command == EventCommand.Enable)

src/libraries/System.Private.CoreLib/src/System/Threading/Tasks/TplEventSource.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,6 @@ protected override void OnEventCommand(EventCommandEventArgs command)
4646
DebugActivityId = IsEnabled(EventLevel.Informational, Keywords.DebugActivityId);
4747
}
4848

49-
/// <summary>
50-
/// Defines the singleton instance for the TPL ETW provider.
51-
/// </summary>
52-
public static readonly TplEventSource Log = new TplEventSource();
53-
54-
// Parameterized constructor to block initialization and ensure the EventSourceGenerator is creating the default constructor
55-
// as you can't make a constructor partial.
56-
private TplEventSource(int _) { }
57-
5849
/// <summary>Configured behavior of a task wait operation.</summary>
5950
public enum TaskWaitBehavior : int
6051
{

0 commit comments

Comments
 (0)