Unable to get azure sdk logs to console for "Microsoft.Azure.Devices" package

Pramod, Pramod 20 Reputation points
2023-08-25T04:11:11.22+00:00

I have created a sample .NET 6 console application and added the below line of code as mentioned in the below Microsoft learn website to enable azure sdk client library logs.

https://learn.microsoft.com/en-us/dotnet/azure/sdk/logging

using Azure.Core.Diagnostics;
using Azure.Storage.Blobs;
using Microsoft.Azure.Devices;
using System.Text;

using AzureEventSourceListener consoleListener =
                                AzureEventSourceListener.CreateConsoleLogger(System.Diagnostics.Tracing.EventLevel.Verbose);

In the sample application I am downloading a blob using BlobClient which is defined in the "Azure.Storage.Blobs" nuget package. And also I am sending a cloud to device message using ServiceClient which is defined in the "Microsoft.Azure.Devices" nuget package.

For the blob client operation I am able to get the sdk logs in console, but for cloud to device message operation using "ServiceClient" I am not able to get the sdk logs.

The sdk logs for the blob operation which is logged in console is attached.

ToUpload

Similar log for service client operation is not logged.

Could you please help me in getting the sdk logs in console for "Microsoft.Azure.Devices" library

Complete sample application code

using Azure.Core.Diagnostics; 
using Azure.Storage.Blobs; 
using Microsoft.Azure.Devices; 
using System.Text;  
namespace SDKLogs 
{     
	internal class Program
     {
         static async Task Main(string[] args)
         {
             using AzureEventSourceListener consoleListener =                                     		AzureEventSourceListener.CreateConsoleLogger(System.Diagnostics.Tracing.EventLevel.Verbose);            var x = new BlobClient(new Uri("BLOB URL")).Download();               ServiceClient _serviceClient = 
               ServiceClient.CreateFromConnectionString("IOT HUB OWNER CONNECTION STRING");              				string msg = "Hi Qwerty";             
Message message = new Message(Encoding.ASCII.GetBytes(msg));
              await _serviceClient.SendAsync("DEVICE ID", message);              	   Console.ReadLine();
         }
     }
 }
Azure IoT SDK
Azure IoT SDK
An Azure software development kit that facilitates building applications that connect to Azure IoT services.
207 questions
{count} votes

Accepted answer
  1. Matthijs van der Veer 4,376 Reputation points MVP
    2023-08-25T08:04:37.0566667+00:00

    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

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful