SqlClient에서 이벤트 추적 사용
적용 대상: .NET Framework
.NET
.NET Standard
ETW(Windows용 이벤트 추적)는 디버깅 및 테스트를 위해 드라이버에서 정의된 이벤트를 기록할 수 있는 효율적인 커널 수준 추적 기능입니다. SqlClient는 다양한 정보 수준에서 ETW 이벤트 캡처를 지원합니다. 이벤트 추적 캡처를 시작하려면 클라이언트 애플리케이션이 SqlClient의 EventSource 구현에서 이벤트를 수신 대기해야 합니다.
Microsoft.Data.SqlClient.EventSource
현재 구현에서는 다음과 같은 이벤트 키워드를 지원합니다.
키워드 이름 | 값 | 설명 |
---|---|---|
ExecutionTrace | 1 | 명령 실행 전후에 이벤트 시작/중지 캡처를 활성화합니다. |
Trace | 2 | 기본 애플리케이션 흐름 추적 이벤트 캡처를 활성화합니다. |
범위 | 4 | 이벤트 입장 및 나가기 캡처를 활성화합니다 |
NotificationTrace | 8 | SqlNotification 추적 이벤트 캡처를 활성화합니다 |
NotificationScope | 16 | SqlNotification 범위 이벤트 입장 및 나가기 캡처를 활성화합니다 |
PoolerTrace | 32 | 연결 풀링 흐름 추적 이벤트 캡처를 활성화합니다. |
PoolerScope | 64 | 연결 풀링 범위 추적 이벤트 캡처를 활성화합니다. |
AdvancedTrace | 128 | 고급 흐름 추적 이벤트 캡처를 활성화합니다. |
AdvancedTraceBin | 256 | 추가 정보를 사용하여 고급 흐름 추적 이벤트 캡처를 활성화합니다. |
CorrelationTrace | 512 | 상관 관계 흐름 추적 이벤트를 캡처하도록 설정합니다. |
StateDump | 1024 | SqlConnection 의 전체 상태 덤프 캡처를 활성화합니다 |
SNITrace | 2048 | Managed Networking 구현에서 흐름 추적 이벤트 캡처를 활성화합니다(.NET Core에서만 적용 가능) |
SNIScope | 4096 | Managed Networking 구현에서 범위 이벤트 캡처를 활성화합니다(.NET Core에서만 적용 가능) |
예시
다음 예에서는 AdventureWorks 샘플 데이터베이스에서 데이터 작업에 대해 이벤트 추적을 사용하도록 설정하고 콘솔 창에 이벤트를 표시합니다.
using System;
using System.Diagnostics.Tracing;
using Microsoft.Data.SqlClient;
// This listener class will listen for events from the SqlClientEventSource class.
// SqlClientEventSource is an implementation of the EventSource class which gives
// it the ability to create events.
public class SqlClientListener : EventListener
{
protected override void OnEventSourceCreated(EventSource eventSource)
{
// Only enable events from SqlClientEventSource.
if (eventSource.Name.Equals("Microsoft.Data.SqlClient.EventSource"))
{
// Use EventKeyWord 2 to capture basic application flow events.
// See the above table for all available keywords.
EnableEvents(eventSource, EventLevel.Informational, (EventKeywords)2);
}
}
// This callback runs whenever an event is written by SqlClientEventSource.
// Event data is accessed through the EventWrittenEventArgs parameter.
protected override void OnEventWritten(EventWrittenEventArgs eventData)
{
// Print event data.
Console.WriteLine(eventData.Payload[0]);
}
}
class Program
{
public static void Main()
{
// Create a new event listener.
using (SqlClientListener listener = new SqlClientListener())
{
string connectionString = "Data Source=localhost; " +
"Initial Catalog=AdventureWorks; Integrated Security=true";
// Open a connection to the AdventureWorks database.
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
string sql = "SELECT * FROM Sales.Currency";
SqlCommand command = new SqlCommand(sql, connection);
// Perform a data operation on the server.
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
// Read the data.
}
reader.Close();
}
}
}
}
네이티브 SNI의 이벤트 추적 지원
Microsoft.Data.SqlClient는 v2.1부터 Microsoft.Data.SqlClient.SNI 및 Microsoft.Data.SqlClient.SNI.runtime에서 이벤트 추적 지원을 제공합니다. Xperf 및 PerfView 도구를 사용하여 네이티브 DLL에서 이벤트를 수집할 수 있습니다.
Microsoft.Data.SqlClient v3.0부터 이벤트 수집 도구를 사용하여 클라이언트 애플리케이션을 수정하지 않고도 이벤트 추적을 사용하도록 설정할 수 있습니다.
Microsoft.Data.SqlClient v2.1에서는 이벤트 원본 수신기를 통해 EventCommand
를 구성하여 이벤트 추적을 사용하도록 설정해야 합니다. 네이티브 SNI에 적용할 수 있는 유효한 EventCommand
값은 다음과 같습니다.
// Enables trace events:
EventSource.SendCommand(eventSource, (EventCommand)8192, null);
// Enables flow events:
EventSource.SendCommand(eventSource, (EventCommand)16384, null);
// Enables both trace and flow events:
EventSource.SendCommand(eventSource, (EventCommand)(8192 | 16384), null);
다음 예제에서는 네이티브 SNI DLL에서 이벤트 추적을 사용하도록 설정합니다.
// Native SNI tracing example
using System;
using System.Diagnostics.Tracing;
using Microsoft.Data.SqlClient;
public class SqlClientListener : EventListener
{
protected override void OnEventSourceCreated(EventSource eventSource)
{
if (eventSource.Name.Equals("Microsoft.Data.SqlClient.EventSource"))
{
// Enables both trace and flow events
EventSource.SendCommand(eventSource, (EventCommand)(8192 | 16384), null);
}
}
}
class Program
{
static string connectionString = @"Data Source = localhost; Initial Catalog = AdventureWorks;Integrated Security=true;";
static void Main(string[] args)
{
// Event source listener configuration is not required in v3.0 onwards.
using (SqlClientListener listener = new SqlClientListener())
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
}
}
}
Xperf를 사용하여 추적 로그 수집
다음 명령을 사용하여 추적을 시작합니다.
xperf -start trace -f myTrace.etl -on *Microsoft.Data.SqlClient.EventSource
네이티브 SNI 추적 예제를 실행하여 SQL Server에 연결합니다.
다음 명령줄을 사용하여 추적을 중지합니다.
xperf -stop trace
PerfView를 사용하여 1단계에 지정된 myTrace.etl 파일을 엽니다. SNI 추적 로그는
Microsoft.Data.SqlClient.EventSource/SNIScope
및Microsoft.Data.SqlClient.EventSource/SNITrace
이벤트 이름으로 찾을 수 있습니다.
PerfView를 사용하여 추적 로그 수집
PerfView를 시작하고 메뉴 표시 줄에서
Collect > Collect
을 실행합니다.추적 파일 이름, 출력 경로 및 공급자 이름을 구성합니다.
수집을 시작합니다.
네이티브 SNI 추적 예제를 실행하여 SQL Server에 연결합니다.
PerfView에서 컬렉션을 중지합니다. 2단계의 구성에 따라 PerfViewData.etl 파일을 생성하려면 시간이 소요됩니다.
PerfView에서
etl
파일을 엽니다. SNI 추적 로그는Microsoft.Data.SqlClient.EventSource/SNIScope
및Microsoft.Data.SqlClient.EventSource/SNITrace
이벤트 이름으로 찾을 수 있습니다.
외부 리소스
Microsoft.Data.SqlClient 플랫폼 간을 추적하는 방법에 대한 다른 예제 집합은 CSS SQL 네트워킹 도구 wiki를 참조하세요.
이벤트 추적에 대한 자세한 내용은 다음 리소스를 참조하세요.
리소스 | 설명 |
---|---|
EventSource 클래스 | ETW 이벤트를 만드는 데 사용됩니다. |
EventListener 클래스 | 이벤트 원본에서 이벤트를 사용하도록 그리고 사용하지 않도록 설정하는 메서드를 제공합니다. |
피드백
https://aka.ms/ContentUserFeedback
출시 예정: 2024년 내내 콘텐츠에 대한 피드백 메커니즘으로 GitHub 문제를 단계적으로 폐지하고 이를 새로운 피드백 시스템으로 바꿀 예정입니다. 자세한 내용은 다음을 참조하세요.다음에 대한 사용자 의견 제출 및 보기