Certainly possible! This SDK does not implement the default AzureEventSourceListener
, but that doesn't stop us from creating our own. Here's an implementation of a simple listener:
using System.Diagnostics.Tracing;
public class CustomEventListener : EventListener
{
protected override void OnEventSourceCreated(EventSource eventSource)
{
if (eventSource.Name == "Microsoft-Azure-Devices-Service-Client")
{
EnableEvents(eventSource, EventLevel.Verbose);
}
}
protected override void OnEventWritten(EventWrittenEventArgs eventData)
{
// Handle the event, for example, write to the console
Console.WriteLine($"Event ID: {eventData.EventId}, Message: {eventData.Message}");
// Dump the payload to console matched with payload name
for (int payloadIndex = 0; payloadIndex < eventData.Payload.Count; payloadIndex++)
{
Console.WriteLine($"\tName:{eventData.PayloadNames[payloadIndex]}, Value: {eventData.Payload[payloadIndex]}");
}
}
}
And you can use it this way:
_ = new CustomEventListener();
ServiceClient _serviceClient = ServiceClient.CreateFromConnectionString(Constants.ConnectionString);
string msg = "Hi Qwerty";
Message message = new Message(Encoding.ASCII.GetBytes(msg));
await _serviceClient.SendAsync("test1", message);
Console.WriteLine("All done.");
For a little more context: I looked into the ServiceClient, it uses an EventSource with this attribute:
[EventSource(Name = "Microsoft-Azure-Devices-Service-Client")]
internal sealed class Logging : EventSource
This allows us to create a custom listener that listens to Microsoft-Azure-Devices-Service-Client