Skip to content

Commit

Permalink
Change System.Data.Common's EventSource to a DiagnosticListener
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub committed Jun 20, 2017
1 parent f018b14 commit 22bdd08
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/System.Data.Common/src/System.Data.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@
<Reference Include="System.Diagnostics.Debug" />
<Reference Include="System.Diagnostics.Tools" />
<Reference Include="System.Diagnostics.TraceSource" />
<Reference Include="System.Diagnostics.Tracing" />
<Reference Include="System.Diagnostics.DiagnosticSource" />
<Reference Include="System.Linq" />
<Reference Include="System.Linq.Expressions" />
<Reference Include="System.ObjectModel" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,94 +2,82 @@
// 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.Tracing;
using System.Diagnostics;
using System.Threading;

namespace System.Data
{
[EventSource(Name = "System.Data.DataCommonEventSource")]
internal class DataCommonEventSource : EventSource
internal sealed class DataCommonEventSource : DiagnosticListener
{
internal static readonly DataCommonEventSource Log = new DataCommonEventSource();
private static long s_nextScopeId = 0;

private const int TraceEventId = 1;
private const int EnterScopeId = 2;
private const int ExitScopeId = 3;
private DataCommonEventSource() : base("System.Data.Common") { }

[Event(TraceEventId, Level = EventLevel.Informational)]
internal void Trace(string message)
{
WriteEvent(TraceEventId, message);
if (!Log.IsEnabled(nameof(Trace))) return;
Write(nameof(Trace), new { Message = message });
}

[NonEvent]
internal void Trace<T0>(string format, T0 arg0)
{
if (!Log.IsEnabled()) return;
if (!Log.IsEnabled(nameof(Trace))) return;
Trace(string.Format(format, arg0));
}

[NonEvent]
internal void Trace<T0, T1>(string format, T0 arg0, T1 arg1)
{
if (!Log.IsEnabled()) return;
if (!Log.IsEnabled(nameof(Trace))) return;
Trace(string.Format(format, arg0, arg1));
}

[NonEvent]
internal void Trace<T0, T1, T2>(string format, T0 arg0, T1 arg1, T2 arg2)
{
if (!Log.IsEnabled()) return;
if (!Log.IsEnabled(nameof(Trace))) return;
Trace(string.Format(format, arg0, arg1, arg2));
}

[NonEvent]
internal void Trace<T0, T1, T2, T3>(string format, T0 arg0, T1 arg1, T2 arg2, T3 arg3)
{
if (!Log.IsEnabled()) return;
if (!Log.IsEnabled(nameof(Trace))) return;
Trace(string.Format(format, arg0, arg1, arg2, arg3));
}

[NonEvent]
internal void Trace<T0, T1, T2, T3, T4>(string format, T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4)
{
if (!Log.IsEnabled()) return;
if (!Log.IsEnabled(nameof(Trace))) return;
Trace(string.Format(format, arg0, arg1, arg2, arg3, arg4));
}

[NonEvent]
internal void Trace<T0, T1, T2, T3, T4, T5, T6>(string format, T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6)
{
if (!Log.IsEnabled()) return;
if (!Log.IsEnabled(nameof(Trace))) return;
Trace(string.Format(format, arg0, arg1, arg2, arg3, arg4, arg5, arg6));
}

[Event(EnterScopeId, Level = EventLevel.Verbose)]
internal long EnterScope(string message)
{
const string EventName = "StartScope";
long scopeId = 0;
if (Log.IsEnabled())
if (Log.IsEnabled(EventName))
{
scopeId = Interlocked.Increment(ref s_nextScopeId);
WriteEvent(EnterScopeId, scopeId, message);
Write(EventName, new { ScopeId = scopeId, Message = message });
}
return scopeId;
}

[NonEvent]
internal long EnterScope<T1>(string format, T1 arg1) => Log.IsEnabled() ? EnterScope(string.Format(format, arg1)) : 0;
[NonEvent]
internal long EnterScope<T1, T2>(string format, T1 arg1, T2 arg2) => Log.IsEnabled() ? EnterScope(string.Format(format, arg1, arg2)) : 0;
[NonEvent]
internal long EnterScope<T1, T2, T3>(string format, T1 arg1, T2 arg2, T3 arg3) => Log.IsEnabled() ? EnterScope(string.Format(format, arg1, arg2, arg3)) : 0;
[NonEvent]
internal long EnterScope<T1, T2, T3, T4>(string format, T1 arg1, T2 arg2, T3 arg3, T4 arg4) => Log.IsEnabled() ? EnterScope(string.Format(format, arg1, arg2, arg3, arg4)) : 0;

[Event(ExitScopeId, Level = EventLevel.Verbose)]
internal void ExitScope(long scopeId)
{
WriteEvent(ExitScopeId, scopeId);
const string EventName = "StopScope";
if (!Log.IsEnabled(EventName))
Write(EventName, new { ScopeId = scopeId });
}
}
}

0 comments on commit 22bdd08

Please sign in to comment.