|
| 1 | +--- |
| 2 | +title: "Event counters in SqlClient" |
| 3 | +description: Use Microsoft SqlClient Data Provider for SQL Server event counters to monitor your application status and its connection resources in .NET Core and .NET Standard. |
| 4 | +ms.date: "05/31/2021" |
| 5 | +dev_langs: |
| 6 | + - "csharp" |
| 7 | +ms.prod: sql |
| 8 | +ms.prod_service: connectivity |
| 9 | +ms.technology: connectivity |
| 10 | +ms.topic: conceptual |
| 11 | +author: David-Engel |
| 12 | +ms.author: v-daenge |
| 13 | +ms.reviewer: v-deshtehari |
| 14 | +--- |
| 15 | +# Event counters in SqlClient |
| 16 | + |
| 17 | +[!INCLUDE[appliesto-xxxx-netcore-netst-md](../../includes/appliesto-xxxx-netcore-netst-md.md)] |
| 18 | + |
| 19 | +[!INCLUDE[Driver_ADONET_Download](../../includes/driver_adonet_download.md)] |
| 20 | + |
| 21 | +> [!IMPORTANT] |
| 22 | +> Event counters are available when targeting **.NET Core 3.1** and higher or **.NET Standard 2.1** and higher. This feature is available starting with `Microsoft.Data.SqlClient` **version 3.0.0**. |
| 23 | +
|
| 24 | +You can use <xref:Microsoft.Data.SqlClient> event counters to monitor the status of your application and the connection resources that it uses. Event counters can be monitored by `.NET CLI global tools` and `perfView` or can be accessed programmatically using the <xref:System.Diagnostics.Tracing.EventListener> class in the <xref:System.Diagnostics.Tracing> namespace. |
| 25 | + |
| 26 | +## Available event counters |
| 27 | + |
| 28 | +Currently there are 16 different event counters available for <xref:Microsoft.Data.SqlClient> as described in the following table: |
| 29 | + |
| 30 | +|Name|Display name|Description| |
| 31 | +|-------------------------|-----------------|-----------------| |
| 32 | +|**active-hard-connections**|Actual active connections currently made to servers|The number of connections that are currently open to database servers.| |
| 33 | +|**hard-connects**|Actual connection rate to servers|The number of connections per second that are being opened to database servers.| |
| 34 | +|**hard-disconnects**|Actual disconnection rate from servers|The number of disconnects per second that are being made to database servers.| |
| 35 | +|**active-soft-connects**|Active connections retrieved from the connection pool|The number of already-open connections being consumed from the connection pool.| |
| 36 | +|**soft-connects**|Rate of connections retrieved from the connection pool|The number of connections per second that are being consumed from the connection pool.| |
| 37 | +|**soft-disconnects**|Rate of connections returned to the connection pool|The number of connections per second that are being returned to the connection pool.| |
| 38 | +|**number-of-non-pooled-connections**|Number of connections not using connection pooling|The number of active connections that aren't pooled.| |
| 39 | +|**number-of-pooled-connections**|Number of connections managed by the connection pool|The number of active connections that are being managed by the connection pooling infrastructure.| |
| 40 | +|**number-of-active-connection-pool-groups**|Number of active unique connection strings|The number of unique connection pool groups that are active. This counter is controlled by the number of unique connection strings that are found in the AppDomain.| |
| 41 | +|**number-of-inactive-connection-pool-groups**|Number of unique connection strings waiting for pruning|The number of unique connection pool groups that are marked for pruning. This counter is controlled by the number of unique connection strings that are found in the AppDomain.| |
| 42 | +|**number-of-active-connection-pools**|Number of active connection pools|The total number of connection pools.| |
| 43 | +|**number-of-inactive-connection-pools**|Number of inactive connection pools|The number of inactive connection pools that haven't had any recent activity and are waiting to be disposed.| |
| 44 | +|**number-of-active-connections**|Number of active connections|The number of active connections that are currently in use.| |
| 45 | +|**number-of-free-connections**|Number of ready connections in the connection pool|The number of open connections available for use in the connection pools.| |
| 46 | +|**number-of-stasis-connections**|Number of connections currently waiting to be ready|The number of connections currently awaiting completion of an action and which are unavailable for use by the application.| |
| 47 | +|**number-of-reclaimed-connections**|Number of reclaimed connections from GC|The number of connections that have been reclaimed through garbage collection where `Close` or `Dispose` wasn't called by the application. **Note** Not explicitly closing or disposing connections hurts performance.| |
| 48 | + |
| 49 | +## Retrieve event counter values |
| 50 | + |
| 51 | +There are two primary ways of consuming EventCounters, either in-proc, or out-of-proc. For more information, see [Consume EventCounters](/dotnet/core/diagnostics/event-counters). |
| 52 | + |
| 53 | +### Consume out-of-proc |
| 54 | + |
| 55 | +In Windows, you can use [PerfView](https://github.com/microsoft/perfview) and [Xperf](/windows-hardware/test/wpt/) to collect event counters data. For more information, see [Enable event tracing in SqlClient](enable-eventsource-tracing.md). |
| 56 | +You can use [dotnet-counters](/dotnet/core/diagnostics/dotnet-counters) and [dotnet-trace](/dotnet/core/diagnostics/dotnet-trace), which are cross platform **.NET** tools to monitor and collect event counters data. |
| 57 | + |
| 58 | +#### Out-of-proc example |
| 59 | + |
| 60 | +The following command runs and collects SqlClient event counters values once every second. Replacing `EventCounterIntervalSec=1` with a higher value allows collection of a smaller trace with less granularity in the counter data. |
| 61 | + |
| 62 | +```Console |
| 63 | +PerfView /onlyProviders=*Microsoft.Data.SqlClient.EventSource:EventCounterIntervalSec=1 run "<application-Path>" |
| 64 | +``` |
| 65 | + |
| 66 | +The following command collects SqlClient event counters values once every second. |
| 67 | + |
| 68 | +```Console |
| 69 | +dotnet-trace collect --process-id <pid> --providers Microsoft.Data.SqlClient.EventSource:0:1:EventCounterIntervalSec=1 |
| 70 | +``` |
| 71 | + |
| 72 | +The following command monitors SqlClient event counters values once every three seconds. |
| 73 | + |
| 74 | +```Console |
| 75 | +dotnet-counters monitor Microsoft.Data.SqlClient.EventSource -p <process-id> --refresh-interval 3 |
| 76 | +``` |
| 77 | + |
| 78 | +The following command monitors selected SqlClient event counters values once every second. |
| 79 | + |
| 80 | +```Console |
| 81 | +dotnet-counters monitor Microsoft.Data.SqlClient.EventSource[hard-connects,hard-disconnects] -p <process-id> |
| 82 | +``` |
| 83 | + |
| 84 | +### Consume in-proc |
| 85 | + |
| 86 | +You can consume the counter values via the [EventListener](/dotnet/api/system.diagnostics.tracing.eventlistener) API. An `EventListener` is an in-proc way of consuming any event written by instances of an [EventSource](/dotnet/api/system.diagnostics.tracing.eventsource) in your application. For more information, see [EventListener](/dotnet/api/system.diagnostics.tracing.eventlistener). |
| 87 | + |
| 88 | +#### In-proc example |
| 89 | + |
| 90 | +The following sample code captures `Microsoft.Data.SqlClient.EventSource` events using `EventCounterIntervalSec=1`. It writes the counter name and its `Mean` value on each event counter update. |
| 91 | + |
| 92 | +> [!NOTE] |
| 93 | +> It's required to specify the `EventCounterIntervalSec` property value when enabling this event. |
| 94 | +
|
| 95 | +[!code-csharp[SqlClientDiagnosticCounter#1](~/../sqlclient/doc/samples/SqlClientDiagnosticCounter.cs#1)] |
| 96 | + |
| 97 | +``` Output |
| 98 | +Actual active connections currently made to servers 0 |
| 99 | +Active connections retrieved from the connection pool 26 |
| 100 | +Number of connections not using connection pooling 0 |
| 101 | +Number of connections managed by the connection pool 26 |
| 102 | +Number of active unique connection strings 1 |
| 103 | +Number of unique connection strings waiting for pruning 0 |
| 104 | +Number of active connection pools 1 |
| 105 | +Number of inactive connection pools 0 |
| 106 | +Number of active connections 26 |
| 107 | +Number of ready connections in the connection pool 0 |
| 108 | +Number of connections currently waiting to be ready 0 |
| 109 | +... |
| 110 | +``` |
| 111 | + |
| 112 | +## See also |
| 113 | + |
| 114 | +- [Performance counters in SqlClient](performance-counters.md) |
| 115 | +- [dotnet-counters](/dotnet/core/diagnostics/dotnet-counters) |
| 116 | +- [dotnet-trace](/dotnet/core/diagnostics/dotnet-trace) |
| 117 | +- [Enable event tracing in SqlClient](enable-eventsource-tracing.md) |
| 118 | +- [Microsoft ADO.NET for SQL Server](microsoft-ado-net-sql-server.md) |
0 commit comments