-
Notifications
You must be signed in to change notification settings - Fork 317
Closed as duplicate of#3711
Copy link
Labels
Repro Available ✔️Issues that are reproducible with repro provided.Issues that are reproducible with repro provided.
Description
Discussed in #3693
Originally posted by vincent-duret October 15, 2025
I am using the following BackgroundService in a .NET 8 asp.net core application to collect some events from Microsoft.Data.SqlClient.
It is inspired from https://learn.microsoft.com/en-us/sql/connect/ado-net/enable-eventsource-tracing?view=sql-server-ver16
This code is working fine when using Microsoft.Data.SqlClient 5.2.2, but not anymore after ugrading to 6.1.1.
The OnEventWritten method no longer receives events from Microsoft.Data.SqlClient.EventSource.
Any help will be appreciated.
public class MetricsService(IOptions<MetricsOptions> options, IProvisioning provisioning, ILogger<MetricsService> logger) : BackgroundService
{
private readonly MetricsOptions _options = options.Value;
private readonly IProvisioning _provisioning = provisioning ?? throw new ArgumentNullException(nameof(provisioning));
private readonly ILogger<MetricsService> _logger = logger ?? throw new ArgumentNullException(nameof(logger));
private SqlClientEventListener? _listener;
protected override Task ExecuteAsync(CancellationToken stoppingToken)
{
if (_options.Enabled)
{
_logger.LogInformation("🚀 Metrics service started (frequency: {frequency}s).", _options.Frequency);
try
{
_listener = new SqlClientEventListener(_provisioning.CurrentInstance.Name, _options.Frequency, _logger);
stoppingToken.Register(() =>
{
_logger.LogInformation("🛑 Metrics service stopped.");
_listener?.Dispose();
});
}
catch (Exception ex )
{
_logger.LogError(ex, "An exception occured.");
}
}
else
{
_logger.LogInformation("⚠️ Metrics service is disabled.");
}
return Task.CompletedTask;
}
private sealed class SqlClientEventListener(string instanceName, int frequency, ILogger logger) : EventListener
{
private readonly string _instanceName = instanceName;
private readonly int _frequency = frequency;
private readonly ILogger _logger = logger;
protected override void OnEventSourceCreated(EventSource eventSource)
{
if (eventSource.Name.Equals("Microsoft.Data.SqlClient.EventSource"))
{
_logger.LogInformation("✅ Subscribed to {Source}", eventSource.Name);
try
{
EnableEvents(eventSource, EventLevel.Verbose, EventKeywords.All, new Dictionary<string, string?>
{
["EventCounterIntervalSec"] = _frequency.ToString()
});
}
catch (Exception ex)
{
_logger.LogError(ex, "An exception occured.");
}
}
}
protected override void OnEventWritten(EventWrittenEventArgs eventData)
{
if (eventData.EventName != "EventCounters" || eventData.EventSource.Name != "Microsoft.Data.SqlClient.EventSource" || eventData.Payload is null)
return;
var payload = (eventData.Payload![0] as IDictionary<string, object>)!;
string name = payload["Name"].ToString()!;
object? value = payload.ContainsKey("Mean") ? payload["Mean"] : payload["Increment"];
_logger.LogInformation("Metric '{name}': {value}", name, value);
}
}
}
```</div>
Metadata
Metadata
Assignees
Labels
Repro Available ✔️Issues that are reproducible with repro provided.Issues that are reproducible with repro provided.