Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 75543e2

Browse files
author
John Salem
authored
[release/3.1] Enable System.Diagnostics.Tracing.EventSouce.AllowDuplicateSourceNames for partner (#28231)
* Backport dotnet/runtime#56873 * typo
1 parent e33a971 commit 75543e2

File tree

1 file changed

+24
-5
lines changed
  • src/System.Private.CoreLib/shared/System/Diagnostics/Tracing

1 file changed

+24
-5
lines changed

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

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2936,14 +2936,17 @@ private void EnsureDescriptorsInitialized()
29362936
m_rawManifest = manifest;
29372937
}
29382938
// TODO Enforce singleton pattern
2939-
Debug.Assert(EventListener.s_EventSources != null, "should be called within lock on EventListener.EventListenersLock which ensures s_EventSources to be initialized");
2940-
foreach (WeakReference eventSourceRef in EventListener.s_EventSources)
2939+
if (!AllowDuplicateSourceNames)
29412940
{
2942-
if (eventSourceRef.Target is EventSource eventSource && eventSource.Guid == m_guid && !eventSource.IsDisposed)
2941+
Debug.Assert(EventListener.s_EventSources != null, "should be called within lock on EventListener.EventListenersLock which ensures s_EventSources to be initialized");
2942+
foreach (WeakReference eventSourceRef in EventListener.s_EventSources)
29432943
{
2944-
if (eventSource != this)
2944+
if (eventSourceRef.Target is EventSource eventSource && eventSource.Guid == m_guid && !eventSource.IsDisposed)
29452945
{
2946-
throw new ArgumentException(SR.Format(SR.EventSource_EventSourceGuidInUse, m_guid));
2946+
if (eventSource != this)
2947+
{
2948+
throw new ArgumentException(SR.Format(SR.EventSource_EventSourceGuidInUse, m_guid));
2949+
}
29472950
}
29482951
}
29492952
}
@@ -3950,6 +3953,22 @@ private bool SelfDescribingEvents
39503953
internal const string s_ActivityStartSuffix = "Start";
39513954
internal const string s_ActivityStopSuffix = "Stop";
39523955

3956+
// This switch controls an opt-in, off-by-default mechanism for allowing multiple EventSources to have the same
3957+
// name and by extension GUID. This is not considered a mainline scenario and is explicitly intended as a release
3958+
// valve for users that make heavy use of AssemblyLoadContext and experience exceptions from EventSource.
3959+
// This does not solve any issues that might arise from this configuration. For instance:
3960+
//
3961+
// * If multiple manifest-mode EventSources have the same name/GUID, it is ambiguous which manifest should be used by an ETW parser.
3962+
// This can result in events being incorrectly parse. The data will still be there, but EventTrace (or other libraries) won't
3963+
// know how to parse it.
3964+
// * Potential issues in parsing self-describing EventSources that use the same name/GUID, event name, and payload type from the same AssemblyLoadContext
3965+
// but have different event IDs set.
3966+
//
3967+
// Most users should not turn this on.
3968+
// see https://github.com/dotnet/runtime/pull/56873 for the upstream changes and conversation
3969+
internal const string DuplicateSourceNamesSwitch = "System.Diagnostics.Tracing.EventSource.AllowDuplicateSourceNames";
3970+
private static readonly bool AllowDuplicateSourceNames = AppContext.TryGetSwitch(DuplicateSourceNamesSwitch, out bool isEnabled) ? isEnabled : false;
3971+
39533972
// WARNING: Do not depend upon initialized statics during creation of EventSources, as it is possible for creation of an EventSource to trigger
39543973
// creation of yet another EventSource. When this happens, these statics may not yet be initialized.
39553974
// Rather than depending on initialized statics, use lazy initialization to ensure that the statics are initialized exactly when they are needed.

0 commit comments

Comments
 (0)