From 26b0f3506083eef26b9e87925c93c7e3e3f32375 Mon Sep 17 00:00:00 2001
From: Johnny Pham <v-jopha@microsoft.com>
Date: Mon, 8 Jun 2020 12:28:28 -0700
Subject: [PATCH 01/12] Set up CI with Azure Pipelines

[skip ci]
---
 azure-pipelines.yml | 3 +++
 1 file changed, 3 insertions(+)
 create mode 100644 azure-pipelines.yml

diff --git a/azure-pipelines.yml b/azure-pipelines.yml
new file mode 100644
index 0000000000..2020209994
--- /dev/null
+++ b/azure-pipelines.yml
@@ -0,0 +1,3 @@
+- task: CmdLine@2
+  inputs:
+    script: 'SET'
\ No newline at end of file

From 3da84f8b0139f549561022e107741e5bf5b7c7a5 Mon Sep 17 00:00:00 2001
From: Johnny Pham <v-jopha@microsoft.com>
Date: Mon, 8 Jun 2020 14:03:26 -0700
Subject: [PATCH 02/12] Delete azure-pipelines.yml

---
 azure-pipelines.yml | 3 ---
 1 file changed, 3 deletions(-)
 delete mode 100644 azure-pipelines.yml

diff --git a/azure-pipelines.yml b/azure-pipelines.yml
deleted file mode 100644
index 2020209994..0000000000
--- a/azure-pipelines.yml
+++ /dev/null
@@ -1,3 +0,0 @@
-- task: CmdLine@2
-  inputs:
-    script: 'SET'
\ No newline at end of file

From d02b69c55c2ccb45c5bc65af7121b781d30c93b9 Mon Sep 17 00:00:00 2001
From: Johnny Pham <v-jopha@microsoft.com>
Date: Mon, 8 Jun 2020 12:28:28 -0700
Subject: [PATCH 03/12] Set up CI with Azure Pipelines

[skip ci]
---
 azure-pipelines.yml | 3 +++
 1 file changed, 3 insertions(+)
 create mode 100644 azure-pipelines.yml

diff --git a/azure-pipelines.yml b/azure-pipelines.yml
new file mode 100644
index 0000000000..2020209994
--- /dev/null
+++ b/azure-pipelines.yml
@@ -0,0 +1,3 @@
+- task: CmdLine@2
+  inputs:
+    script: 'SET'
\ No newline at end of file

From 896caa151d55c67ff7f18b7ce32d4cc2eec425ca Mon Sep 17 00:00:00 2001
From: Johnny Pham <v-jopha@microsoft.com>
Date: Mon, 8 Jun 2020 14:03:26 -0700
Subject: [PATCH 04/12] Delete azure-pipelines.yml

---
 azure-pipelines.yml | 3 ---
 1 file changed, 3 deletions(-)
 delete mode 100644 azure-pipelines.yml

diff --git a/azure-pipelines.yml b/azure-pipelines.yml
deleted file mode 100644
index 2020209994..0000000000
--- a/azure-pipelines.yml
+++ /dev/null
@@ -1,3 +0,0 @@
-- task: CmdLine@2
-  inputs:
-    script: 'SET'
\ No newline at end of file

From acac0ccafb6135e763bcdb6bf3979baa59848671 Mon Sep 17 00:00:00 2001
From: Johnny Pham <23270162+johnnypham@users.noreply.github.com>
Date: Tue, 14 Jul 2020 10:02:02 -0700
Subject: [PATCH 05/12] Event tracing in SNI.dll

---
 .../Interop/SNINativeMethodWrapper.Windows.cs | 20 +++++++++-
 .../src/Microsoft.Data.SqlClient.csproj       |  5 +++
 .../netfx/src/Microsoft.Data.SqlClient.csproj |  3 ++
 .../Interop/SNINativeManagedWrapperX64.cs     |  6 +++
 .../Interop/SNINativeManagedWrapperX86.cs     |  6 +++
 .../Data/Interop/SNINativeMethodWrapper.cs    | 34 +++++++++++++++--
 .../SqlClient/SqlClientEventSource.Windows.cs | 38 +++++++++++++++++++
 .../Data/SqlClient/SqlClientEventSource.cs    |  2 +-
 8 files changed, 108 insertions(+), 6 deletions(-)
 create mode 100644 src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlClientEventSource.Windows.cs

diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Interop/SNINativeMethodWrapper.Windows.cs b/src/Microsoft.Data.SqlClient/netcore/src/Interop/SNINativeMethodWrapper.Windows.cs
index 2566f441f0..dc2ee997fc 100644
--- a/src/Microsoft.Data.SqlClient/netcore/src/Interop/SNINativeMethodWrapper.Windows.cs
+++ b/src/Microsoft.Data.SqlClient/netcore/src/Interop/SNINativeMethodWrapper.Windows.cs
@@ -304,7 +304,13 @@ private static extern unsafe uint SNISecGenClientContextWrapper(
 
         [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)]
         private static extern uint SNIWriteSyncOverAsync(SNIHandle pConn, [In] SNIPacket pPacket);
-        #endregion
+       
+		[DllImport(SNI, CallingConvention = CallingConvention.Cdecl)]
+		internal static extern bool RegisterTraceProviderWrapper(int eventKeyword);
+
+		[DllImport(SNI, CallingConvention = CallingConvention.Cdecl)]
+		internal static extern void UnregisterTraceProviderWrapper();
+	    #endregion
 
         internal static uint SniGetConnectionId(SNIHandle pConn, ref Guid connId)
         {
@@ -461,6 +467,18 @@ private static void MarshalConsumerInfo(ConsumerInfo consumerInfo, ref Sni_Consu
                 : IntPtr.Zero;
             native_consumerInfo.ConsumerKey = consumerInfo.key;
         }
+
+		internal static bool RegisterTraceProvider(int eventKeyword)
+		{
+			// Registers the TraceLogging provider, enabling it to generate events.
+			// Return true if enabled, otherwise false.
+			return RegisterTraceProviderWrapper(eventKeyword);
+		}
+
+		internal static void UnregisterTraceProvider()
+		{
+			UnregisterTraceProviderWrapper();
+		}
     }
 }
 
diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj
index 29a0f61505..40003150cf 100644
--- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj
+++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj
@@ -29,6 +29,11 @@
     <DebugSymbols>true</DebugSymbols>
   </PropertyGroup>
   <!-- Contains common items shared between NetFx and NetCore -->
+  <ItemGroup Condition="'$(OSGroup)' != 'AnyOS' AND '$(TargetsWindows)' == 'true'">
+	<Compile Include="..\..\src\Microsoft\Data\SqlClient\SqlClientEventSource.Windows.cs">
+	  <Link>Microsoft\Data\SqlClient\SqlClientEventSource.Windows.cs</Link>
+	</Compile>
+  </ItemGroup>
   <ItemGroup Condition="'$(OSGroup)' != 'AnyOS'">
     <Compile Include="..\..\src\Microsoft\Data\SqlClient\SqlClientEventSource.cs">
       <Link>Microsoft\Data\SqlClient\SqlClientEventSource.cs</Link>
diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj
index f3b68d3da1..9764fe9e38 100644
--- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj
+++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj
@@ -90,6 +90,9 @@
     <Compile Include="..\..\src\Microsoft\Data\SqlClient\SqlClientEventSource.cs">
       <Link>Microsoft\Data\SqlClient\SqlClientEventSource.cs</Link>
     </Compile>
+    <Compile Include="..\..\src\Microsoft\Data\SqlClient\SqlClientEventSource.Windows.cs">
+      <Link>Microsoft\Data\SqlClient\SqlClientEventSource.Windows.cs</Link>
+    </Compile>
     <Compile Include="..\..\src\Microsoft\Data\SqlClient\SqlClientLogger.cs">
       <Link>Microsoft\Data\SqlClient\SqlClientLogger.cs</Link>
     </Compile>
diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/Interop/SNINativeManagedWrapperX64.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/Interop/SNINativeManagedWrapperX64.cs
index 0cddc32dc1..9b454db81c 100644
--- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/Interop/SNINativeManagedWrapperX64.cs
+++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/Interop/SNINativeManagedWrapperX64.cs
@@ -133,5 +133,11 @@ internal static extern unsafe uint SNISecGenClientContextWrapper(
 
         [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)]
         internal static extern IntPtr SNIClientCertificateFallbackWrapper(IntPtr pCallbackContext);
+
+		[DllImport(SNI, CallingConvention = CallingConvention.Cdecl)]
+		internal static extern bool RegisterTraceProviderWrapper(int eventKeyword);
+
+		[DllImport(SNI, CallingConvention = CallingConvention.Cdecl)]
+		internal static extern void UnregisterTraceProviderWrapper();
     }
 }
diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/Interop/SNINativeManagedWrapperX86.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/Interop/SNINativeManagedWrapperX86.cs
index 398ecc4872..de6260bdde 100644
--- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/Interop/SNINativeManagedWrapperX86.cs
+++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/Interop/SNINativeManagedWrapperX86.cs
@@ -133,5 +133,11 @@ internal static extern unsafe uint SNISecGenClientContextWrapper(
 
         [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)]
         internal static extern IntPtr SNIClientCertificateFallbackWrapper(IntPtr pCallbackContext);
+
+		[DllImport(SNI, CallingConvention = CallingConvention.Cdecl)]
+		internal static extern bool RegisterTraceProviderWrapper(int eventKeyword);
+
+		[DllImport(SNI, CallingConvention = CallingConvention.Cdecl)]
+		internal static extern void UnregisterTraceProviderWrapper();
     }
 }
diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/Interop/SNINativeMethodWrapper.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/Interop/SNINativeMethodWrapper.cs
index 66efa587b6..a79e0e71e5 100644
--- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/Interop/SNINativeMethodWrapper.cs
+++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/Interop/SNINativeMethodWrapper.cs
@@ -51,7 +51,7 @@ internal static class SNINativeMethodWrapper
         internal const int LocalDBInvalidSqlUserInstanceDllPath = 55;
         internal const int LocalDBFailedToLoadDll = 56;
         internal const int LocalDBBadRuntime = 57;
-		internal const int SniIP6AddrStringBufferLength = 48; // from SNI layer
+        internal const int SniIP6AddrStringBufferLength = 48; // from SNI layer
 
         internal static int SniMaxComposedSpnLength
         {
@@ -725,12 +725,12 @@ internal static uint SniGetConnectionId(SNIHandle pConn, ref Guid connId)
         {
             return SNIGetInfoWrapper(pConn, QTypes.SNI_QUERY_CONN_CONNID, out connId);
         }
-       
+
         internal static uint SniGetProviderNumber(SNIHandle pConn, ref ProviderEnum provNum)
         {
             return SNIGetInfoWrapper(pConn, QTypes.SNI_QUERY_CONN_PROVIDERNUM, out provNum);
         }
-     
+
         internal static uint SniGetConnectionPort(SNIHandle pConn, ref ushort portNum)
         {
             return SNIGetInfoWrapper(pConn, QTypes.SNI_QUERY_CONN_PEERPORT, out portNum);
@@ -812,7 +812,7 @@ internal static unsafe uint SNIOpenSyncEx(ConsumerInfo consumerInfo, string cons
                 clientConsumerInfo.DNSCacheInfo.wszCachedTcpIPv4 = cachedDNSInfo?.AddrIPv4;
                 clientConsumerInfo.DNSCacheInfo.wszCachedTcpIPv6 = cachedDNSInfo?.AddrIPv6;
                 clientConsumerInfo.DNSCacheInfo.wszCachedTcpPort = cachedDNSInfo?.Port;
-                
+
                 if (spnBuffer != null)
                 {
                     fixed (byte* pin_spnBuffer = &spnBuffer[0])
@@ -1075,6 +1075,32 @@ private static void MarshalConsumerInfo(ConsumerInfo consumerInfo, ref Sni_Consu
                 : IntPtr.Zero;
             native_consumerInfo.ConsumerKey = consumerInfo.key;
         }
+
+        internal static bool RegisterTraceProvider(int eventKeyword)
+        {
+            // Registers the TraceLogging provider, enabling it to generate events.
+            // Return true if enabled, otherwise false.
+            if (s_is64bitProcess)
+            {
+                return SNINativeManagedWrapperX64.RegisterTraceProviderWrapper(eventKeyword);
+            }
+            else
+            {
+                return SNINativeManagedWrapperX86.RegisterTraceProviderWrapper(eventKeyword);
+            }
+        }
+
+        internal static void UnregisterTraceProvider()
+        {
+            if (s_is64bitProcess)
+            {
+                SNINativeManagedWrapperX64.UnregisterTraceProviderWrapper();
+            }
+            else
+            {
+                SNINativeManagedWrapperX86.UnregisterTraceProviderWrapper();
+            }
+        }
     }
 }
 
diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlClientEventSource.Windows.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlClientEventSource.Windows.cs
new file mode 100644
index 0000000000..362c94ed17
--- /dev/null
+++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlClientEventSource.Windows.cs
@@ -0,0 +1,38 @@
+// 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;
+
+        protected override void OnEventCommand(EventCommandEventArgs e)
+        {
+            // Internally, EventSource.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.
+
+            // 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 & ((EventCommand)8192 | (EventCommand)16384)) != 0)
+            {
+                int eventKeyword = (int)(e.Command & ((EventCommand)8192 | (EventCommand)16384));
+                _traceLoggingProviderEnabled = SNINativeMethodWrapper.RegisterTraceProvider(eventKeyword);
+                Debug.Assert(_traceLoggingProviderEnabled, "Failed to enable TraceLogging provider.");
+            }
+            else if (_traceLoggingProviderEnabled && (e.Command == (EventCommand)32768))
+            {
+                // Only unregister the provider if it's currently registered.
+                SNINativeMethodWrapper.UnregisterTraceProvider();
+                _traceLoggingProviderEnabled = false;
+            }
+        }
+    }
+}
diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlClientEventSource.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlClientEventSource.cs
index 93e544a713..f9000f5d9f 100644
--- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlClientEventSource.cs
+++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlClientEventSource.cs
@@ -8,7 +8,7 @@
 namespace Microsoft.Data.SqlClient
 {
     [EventSource(Name = "Microsoft.Data.SqlClient.EventSource")]
-    internal class SqlClientEventSource : EventSource
+    internal partial class SqlClientEventSource : EventSource
     {
         // Defines the singleton instance for the Resources ETW provider
         internal static readonly SqlClientEventSource Log = new SqlClientEventSource();

From 74a7b8ce25463e8a14bf0d33e9092eaf2e0becb6 Mon Sep 17 00:00:00 2001
From: Johnny Pham <23270162+johnnypham@users.noreply.github.com>
Date: Tue, 14 Jul 2020 10:46:54 -0700
Subject: [PATCH 06/12] Update BUILDGUIDE.md

---
 BUILDGUIDE.md | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/BUILDGUIDE.md b/BUILDGUIDE.md
index 71f068be23..12ee82cfad 100644
--- a/BUILDGUIDE.md
+++ b/BUILDGUIDE.md
@@ -56,7 +56,7 @@ Once the environment is setup properly, execute the desired set of commands belo
 ```
 
 ```bash
-> msbuild /t:BuildAllOSes
+> msbuild /t:BuildNetCoreAllOS
 # Builds the driver for all Operating Systems.
 ```
 

From 5bdcb339033a176a2996ca75f40fa4f554ca2bde Mon Sep 17 00:00:00 2001
From: Johnny Pham <23270162+johnnypham@users.noreply.github.com>
Date: Tue, 14 Jul 2020 15:15:46 -0700
Subject: [PATCH 07/12] Update BUILDGUIDE.md

---
 BUILDGUIDE.md | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/BUILDGUIDE.md b/BUILDGUIDE.md
index 12ee82cfad..6f56080aa0 100644
--- a/BUILDGUIDE.md
+++ b/BUILDGUIDE.md
@@ -57,7 +57,7 @@ Once the environment is setup properly, execute the desired set of commands belo
 
 ```bash
 > msbuild /t:BuildNetCoreAllOS
-# Builds the driver for all Operating Systems.
+# Builds the .NET Core driver for all Operating Systems.
 ```
 
 ## Building Tests

From c7895417cca811a34653548b26d3f2239bbcb112 Mon Sep 17 00:00:00 2001
From: Johnny Pham <23270162+johnnypham@users.noreply.github.com>
Date: Tue, 21 Jul 2020 09:32:28 -0700
Subject: [PATCH 08/12] Address feedback

---
 .../Interop/SNINativeManagedWrapperX64.cs     | 12 +++++------
 .../Interop/SNINativeManagedWrapperX86.cs     | 12 +++++------
 .../SqlClient/SqlClientEventSource.Windows.cs | 21 ++++++++++++++++---
 3 files changed, 30 insertions(+), 15 deletions(-)

diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/Interop/SNINativeManagedWrapperX64.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/Interop/SNINativeManagedWrapperX64.cs
index 9b454db81c..3b2549e5de 100644
--- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/Interop/SNINativeManagedWrapperX64.cs
+++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/Interop/SNINativeManagedWrapperX64.cs
@@ -133,11 +133,11 @@ internal static extern unsafe uint SNISecGenClientContextWrapper(
 
         [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)]
         internal static extern IntPtr SNIClientCertificateFallbackWrapper(IntPtr pCallbackContext);
-
-		[DllImport(SNI, CallingConvention = CallingConvention.Cdecl)]
-		internal static extern bool RegisterTraceProviderWrapper(int eventKeyword);
-
-		[DllImport(SNI, CallingConvention = CallingConvention.Cdecl)]
-		internal static extern void UnregisterTraceProviderWrapper();
+        
+        [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)]
+        internal static extern bool RegisterTraceProviderWrapper(int eventKeyword);
+        
+        [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)]
+        internal static extern void UnregisterTraceProviderWrapper();
     }
 }
diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/Interop/SNINativeManagedWrapperX86.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/Interop/SNINativeManagedWrapperX86.cs
index de6260bdde..fc1e90750c 100644
--- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/Interop/SNINativeManagedWrapperX86.cs
+++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/Interop/SNINativeManagedWrapperX86.cs
@@ -133,11 +133,11 @@ internal static extern unsafe uint SNISecGenClientContextWrapper(
 
         [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)]
         internal static extern IntPtr SNIClientCertificateFallbackWrapper(IntPtr pCallbackContext);
-
-		[DllImport(SNI, CallingConvention = CallingConvention.Cdecl)]
-		internal static extern bool RegisterTraceProviderWrapper(int eventKeyword);
-
-		[DllImport(SNI, CallingConvention = CallingConvention.Cdecl)]
-		internal static extern void UnregisterTraceProviderWrapper();
+        
+        [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)]
+        internal static extern bool RegisterTraceProviderWrapper(int eventKeyword);
+        
+        [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)]
+        internal static extern void UnregisterTraceProviderWrapper();
     }
 }
diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlClientEventSource.Windows.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlClientEventSource.Windows.cs
index 362c94ed17..945a806e58 100644
--- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlClientEventSource.Windows.cs
+++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlClientEventSource.Windows.cs
@@ -12,6 +12,21 @@ 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, EventSource.EnableEvents sends an event command, with a reserved value of 0, -2, or -3.
@@ -21,13 +36,13 @@ protected override void OnEventCommand(EventCommandEventArgs e)
 
             // 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 & ((EventCommand)8192 | (EventCommand)16384)) != 0)
+            if (!_traceLoggingProviderEnabled && e.Command > 0 && (e.Command & (SNINativeTrace | SNINativeScope)) != 0)
             {
-                int eventKeyword = (int)(e.Command & ((EventCommand)8192 | (EventCommand)16384));
+                int eventKeyword = (int)(e.Command & (SNINativeTrace | SNINativeScope));
                 _traceLoggingProviderEnabled = SNINativeMethodWrapper.RegisterTraceProvider(eventKeyword);
                 Debug.Assert(_traceLoggingProviderEnabled, "Failed to enable TraceLogging provider.");
             }
-            else if (_traceLoggingProviderEnabled && (e.Command == (EventCommand)32768))
+            else if (_traceLoggingProviderEnabled && (e.Command == SNINativeDisable))
             {
                 // Only unregister the provider if it's currently registered.
                 SNINativeMethodWrapper.UnregisterTraceProvider();

From 63ce7061e0178c5e2c61282203b722c354f06aad Mon Sep 17 00:00:00 2001
From: Johnny Pham <23270162+johnnypham@users.noreply.github.com>
Date: Tue, 21 Jul 2020 09:35:00 -0700
Subject: [PATCH 09/12] spacing

---
 .../src/Interop/SNINativeMethodWrapper.Windows.cs    | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Interop/SNINativeMethodWrapper.Windows.cs b/src/Microsoft.Data.SqlClient/netcore/src/Interop/SNINativeMethodWrapper.Windows.cs
index dc2ee997fc..e3b91c6ee5 100644
--- a/src/Microsoft.Data.SqlClient/netcore/src/Interop/SNINativeMethodWrapper.Windows.cs
+++ b/src/Microsoft.Data.SqlClient/netcore/src/Interop/SNINativeMethodWrapper.Windows.cs
@@ -304,12 +304,12 @@ private static extern unsafe uint SNISecGenClientContextWrapper(
 
         [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)]
         private static extern uint SNIWriteSyncOverAsync(SNIHandle pConn, [In] SNIPacket pPacket);
-       
-		[DllImport(SNI, CallingConvention = CallingConvention.Cdecl)]
-		internal static extern bool RegisterTraceProviderWrapper(int eventKeyword);
-
-		[DllImport(SNI, CallingConvention = CallingConvention.Cdecl)]
-		internal static extern void UnregisterTraceProviderWrapper();
+        
+        [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)]
+        internal static extern bool RegisterTraceProviderWrapper(int eventKeyword);
+        
+        [DllImport(SNI, CallingConvention = CallingConvention.Cdecl)]
+        internal static extern void UnregisterTraceProviderWrapper();
 	    #endregion
 
         internal static uint SniGetConnectionId(SNIHandle pConn, ref Guid connId)

From f6e8be33de2eda93794dfd47d0c5baec9d2453a1 Mon Sep 17 00:00:00 2001
From: Johnny Pham <23270162+johnnypham@users.noreply.github.com>
Date: Fri, 24 Jul 2020 09:33:43 -0700
Subject: [PATCH 10/12] Update SqlClientEventSource.Windows.cs

---
 .../Microsoft/Data/SqlClient/SqlClientEventSource.Windows.cs    | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlClientEventSource.Windows.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlClientEventSource.Windows.cs
index 945a806e58..2ce9ebf380 100644
--- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlClientEventSource.Windows.cs
+++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlClientEventSource.Windows.cs
@@ -29,7 +29,7 @@ internal partial class SqlClientEventSource : EventSource
 
         protected override void OnEventCommand(EventCommandEventArgs e)
         {
-            // Internally, EventSource.EnableEvents sends an event command, with a reserved value of 0, -2, or -3.
+            // 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.

From 4e174c5b8fa8c1482814382a3625cf56528f91d5 Mon Sep 17 00:00:00 2001
From: Johnny Pham <23270162+johnnypham@users.noreply.github.com>
Date: Tue, 28 Jul 2020 16:52:01 -0700
Subject: [PATCH 11/12] Update SqlClientEventSource.Windows.cs

---
 .../src/Microsoft/Data/SqlClient/SqlClientEventSource.Windows.cs | 1 -
 1 file changed, 1 deletion(-)

diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlClientEventSource.Windows.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlClientEventSource.Windows.cs
index 2ce9ebf380..eebeeb3d92 100644
--- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlClientEventSource.Windows.cs
+++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlClientEventSource.Windows.cs
@@ -2,7 +2,6 @@
 // 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;
 

From 2637d6cf9ca8969a487ef8612a10f863c3eaa8ca Mon Sep 17 00:00:00 2001
From: Johnny Pham <23270162+johnnypham@users.noreply.github.com>
Date: Wed, 29 Jul 2020 08:55:25 -0700
Subject: [PATCH 12/12] check for managed networking

---
 .../Data/SqlClient/SqlClientEventSource.Windows.cs        | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlClientEventSource.Windows.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlClientEventSource.Windows.cs
index eebeeb3d92..f9af6024e4 100644
--- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlClientEventSource.Windows.cs
+++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlClientEventSource.Windows.cs
@@ -2,6 +2,7 @@
 // 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;
 
@@ -33,6 +34,13 @@ protected override void OnEventCommand(EventCommandEventArgs e)
             // 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)