Skip to content

Commit 2b08758

Browse files
committed
Add native sni tracing
1 parent 6ff039c commit 2b08758

3 files changed

Lines changed: 76 additions & 0 deletions

File tree

docs/connect/ado-net/enable-eventsource-tracing.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,82 @@ The following example enables event tracing for a data operation on the **Advent
4848

4949
[!code-csharp [SqlClientEventSource#1](~/../sqlclient/doc/samples/SqlClientEventSource.cs#1)]
5050

51+
## Event tracing support in Native SNI
52+
53+
**Microsoft.Data.SqlClient** v2.1.0 extends event tracing support in **Microsoft.Data.SqlClient.SNI** and **Microsoft.Data.SqlClient.SNI.runtime**. By sending an EventCommand to `SqlClientEventSource`, events in native SNI.dll can be collected using [Xperf](https://docs.microsoft.com/windows-hardware/test/wpt/) and [PerfView](https://github.com/microsoft/perfview) tools. The valid EventCommand values are listed as below:
54+
55+
```cs
56+
// Enables trace events:
57+
EventSource.SendCommand(eventSource, (EventCommand)8192, null);
58+
59+
// Enables flow events:
60+
EventSource.SendCommand(eventSource, (EventCommand)16384, null);
61+
62+
// Enables both trace and flow events:
63+
EventSource.SendCommand(eventSource, (EventCommand)(8192 | 16384), null);
64+
```
65+
66+
The following example enables event tracing in native SNI.dll when the application targets .NET Framework.
67+
68+
```cs
69+
// Native SNI tracing example
70+
// .NET Framework application
71+
using System;
72+
using System.Diagnostics.Tracing;
73+
using Microsoft.Data.SqlClient;
74+
75+
public class SqlClientListener : EventListener
76+
{
77+
protected override void OnEventSourceCreated(EventSource eventSource)
78+
{
79+
if (eventSource.Name.Equals("Microsoft.Data.SqlClient.EventSource"))
80+
{
81+
// Enables both trace and flow events
82+
EventSource.SendCommand(eventSource, (EventCommand)(8192 | 16384), null);
83+
}
84+
}
85+
}
86+
87+
class Program
88+
{
89+
static string connectionString = @"Data Source = localhost; Initial Catalog = AdventureWorks;Integrated Security=true;";
90+
91+
static void Main(string[] args)
92+
{
93+
using (SqlClientListener listener = new SqlClientListener())
94+
using (SqlConnection connection = new SqlConnection(connectionString))
95+
{
96+
connection.Open();
97+
}
98+
}
99+
}
100+
```
101+
102+
### Using Xperf to collect trace log
103+
104+
1. Start tracing using the following command line.
105+
```
106+
xperf -start trace -f myTrace.etl -on *Microsoft.Data.SqlClient.EventSource
107+
```
108+
2. Run the native SNI tracing example to connect to SQL Server.
109+
3. Stop tracing using the following command line.
110+
```
111+
xperf -stop trace
112+
```
113+
4. Use PerfView to open the myTrace.etl file specified in Step 1. The SNI tracing log can be found with `Microsoft.Data.SqlClient.EventSource/SNIScope` and `Microsoft.Data.SqlClient.EventSource/SNITrace` event names.
114+
![Use PerfView to view SNI trace file](media/view-event-trace-native-sni.png)
115+
116+
### Using PerfView to collect trace log
117+
118+
1. Start PerfView and run `Collect > Collect` from menu bar.
119+
2. Configure trace file name, output path, and provider name.
120+
![Configure Prefview before collection](media/collect-event-trace-native-sni.png)
121+
3. Start collection.
122+
4. Run the native SNI tracing example to connect to SQL Server.
123+
5. Stop collection from PerfView. It will take a while to generate PerfViewData.etl file according to configuration in Step 2.
124+
6. Open the etl file in PerfView. The SNI tracing log can be found with `Microsoft.Data.SqlClient.EventSource/SNIScope` and `Microsoft.Data.SqlClient.EventSource/SNITrace` event names.
125+
126+
51127
## External resources
52128
For more information, see the following resources.
53129

198 KB
Loading
284 KB
Loading

0 commit comments

Comments
 (0)