Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Event tracing broken in Microsoft.Data.SqlClient 6.1.1 ? #3693

Unanswered
vincent-duret asked this question in Q&A
Discussion options

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);
 }
 }
}
You must be logged in to vote

Replies: 1 comment 1 reply

Comment options

Hi @vincent-duret - can you try updating to use MDS 6.1.2? If the problem persists, please open an issue from this discussion. If the problem is fixed, please report back here and I will make a note of it in the 6.1.2 release notes.

You must be logged in to vote
1 reply
Comment options

I do have the same issue in 6.1.2.
The OnEventWritten is called many times but never with an eventData.EventSource.Name equals to "Microsoft.Data.SqlClient.EventSource"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /