mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-02-16 10:58:30 -05:00
Add SqlClient event listener (#1544)
* Add SqlClient event listener * comments * ensure not disposed early
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
//
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
|
||||
using System.Diagnostics.Tracing;
|
||||
using Microsoft.SqlTools.Utility;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.Utility
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// This listener class will listen for events from the SqlClientEventSource class
|
||||
/// and forward them to the logger.
|
||||
/// </summary>
|
||||
internal 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 https://docs.microsoft.com/sql/connect/ado-net/enable-eventsource-tracing for all available keywords.
|
||||
EnableEvents(eventSource, EventLevel.Informational, (EventKeywords)2);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This callback runs whenever an event is written by SqlClientEventSource.
|
||||
/// Event data is accessed through the EventWrittenEventArgs parameter.
|
||||
/// </summary>
|
||||
/// <param name="eventData">The data for the event</param>
|
||||
protected override void OnEventWritten(EventWrittenEventArgs eventData)
|
||||
{
|
||||
if (eventData.Payload == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
foreach (object payload in eventData.Payload)
|
||||
{
|
||||
if (payload != null)
|
||||
{
|
||||
Logger.Verbose(payload.ToString());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -23,6 +23,7 @@ namespace Microsoft.SqlTools.ServiceLayer
|
||||
/// </summary>
|
||||
internal static void Main(string[] args)
|
||||
{
|
||||
SqlClientListener? sqlClientListener = null;
|
||||
try
|
||||
{
|
||||
// read command-line arguments
|
||||
@@ -39,6 +40,12 @@ namespace Microsoft.SqlTools.ServiceLayer
|
||||
}
|
||||
|
||||
Logger.Initialize(tracingLevel: commandOptions.TracingLevel, logFilePath: logFilePath, traceSource: "sqltools", commandOptions.AutoFlushLog);
|
||||
// Only enable SQL Client logging when verbose or higher to avoid extra overhead when the
|
||||
// detailed logging it provides isn't needed
|
||||
if (Logger.TracingLevel.HasFlag(SourceLevels.Verbose))
|
||||
{
|
||||
sqlClientListener = new SqlClientListener();
|
||||
}
|
||||
|
||||
// set up the host details and profile paths
|
||||
var hostDetails = new HostDetails(version: new Version(1, 0));
|
||||
@@ -73,6 +80,7 @@ namespace Microsoft.SqlTools.ServiceLayer
|
||||
finally
|
||||
{
|
||||
Logger.Close();
|
||||
sqlClientListener?.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user