/
SqlClientEventSource.Windows.cs
60 lines (53 loc) · 2.68 KB
/
SqlClientEventSource.Windows.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// 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;
using System.Diagnostics;
using System.Diagnostics.Tracing;
namespace Microsoft.Data.SqlClient
{
internal partial class SqlClientEventSource : EventSource
{
private bool _traceLoggingProviderEnabled = false;
/// <summary>
/// Captures application flow traces from native networking implementation
/// </summary>
private const EventCommand SNINativeTrace = (EventCommand)8192;
/// <summary>
/// Captures scope trace events from native networking implementation
/// </summary>
private const EventCommand SNINativeScope = (EventCommand)16384;
/// <summary>
/// Disables all event tracing in native networking implementation
/// </summary>
private const EventCommand SNINativeDisable = (EventCommand)32768;
protected override void OnEventCommand(EventCommandEventArgs e)
{
// Internally, EventListener.EnableEvents sends an event command, with a reserved value of 0, -2, or -3.
// When a command is sent via EnableEvents or SendCommand, check if it is a user-defined value
// to enable or disable event tracing in sni.dll.
// If registration fails, all write and unregister commands will be a no-op.
// If managed networking is enabled, don't call native wrapper methods
#if netcoreapp
if (AppContext.TryGetSwitch("Switch.Microsoft.Data.SqlClient.UseManagedNetworkingOnWindows", out bool isEnabled) && isEnabled)
{
return;
}
#endif
// Only register the provider if it's not already registered. Registering a provider that is already
// registered can lead to unpredictable behaviour.
if (!_traceLoggingProviderEnabled && e.Command > 0 && (e.Command & (SNINativeTrace | SNINativeScope)) != 0)
{
int eventKeyword = (int)(e.Command & (SNINativeTrace | SNINativeScope));
_traceLoggingProviderEnabled = SNINativeMethodWrapper.RegisterTraceProvider(eventKeyword);
Debug.Assert(_traceLoggingProviderEnabled, "Failed to enable TraceLogging provider.");
}
else if (_traceLoggingProviderEnabled && (e.Command == SNINativeDisable))
{
// Only unregister the provider if it's currently registered.
SNINativeMethodWrapper.UnregisterTraceProvider();
_traceLoggingProviderEnabled = false;
}
}
}
}