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

238 views Asked by At

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.

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

SDK Logs

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.

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

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

Here is my code

using AzureEventSourceListener consoleListener = AzureEventSourceListener.CreateConsoleLogger(System.Diagnostics.Tracing.EventLevel.Verbose); 
var x = new BlobClient(new Uri("BLOB URL")).Download(); 
ServiceClient _serviceClient = ServiceClient.CreateFromConnectionString("CONN STRING"); 
string msg = "Hi Qwerty"; 
Message message = new Message(Encoding.ASCII.GetBytes(msg)); 
await _serviceClient.SendAsync("DEVICE ID", message);
1

There are 1 answers

5
Sampath On
  • using the package Microsoft.Extensions.Logging we can log the data

Install the below packages

Microsoft.Extensions.Logging.Console
Microsoft.Extensions.Logging
  • I am able to get the logs
 AzureEventSourceListener consoleListener = AzureEventSourceListener.CreateConsoleLogger(System.Diagnostics.Tracing.EventLevel.Verbose);

 // Set up logger
 var serviceProvider = new ServiceCollection()
     .AddLogging(builder =>
     {
         builder.AddConsole();
     })
     .BuildServiceProvider();

 var logger = serviceProvider.GetService<ILogger<Program>>();

 try
 {
     // Download a blob
     BlobClient blobClient = new BlobClient(new Uri("https://samstoragehello.blob.core.windows.net/hellocont/_ViewImports.cshtml?sp"));
     BlobDownloadInfo downloadedContent = await blobClient.DownloadAsync();

     // Read the downloaded content
     using (var streamReader = new StreamReader(downloadedContent.Content))
     {
         string downloadedContentString = await streamReader.ReadToEndAsync();

         // Send a message to an IoT device
         ServiceClient _serviceClient = ServiceClient.CreateFromConnectionString("HostName=samiothubtodel1.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey=");
         string msg = "Hi Qwerty";
         Microsoft.Azure.Devices.Message message = new Microsoft.Azure.Devices.Message(Encoding.ASCII.GetBytes(msg));
         await _serviceClient.SendAsync("mywebpi", message);

         // Log information including blob content and IoT message content
         logger.LogInformation("Blob downloaded successfully. Content: {BlobContent}", downloadedContentString);

        
         logger.LogInformation("IoT message sent. Content: {IoTMessageContent}", msg);
     }
 }
 catch (Exception ex)
 {
     // Log error
     logger.LogError(ex, "An error occurred: {ErrorMessage}", ex.Message);
 }

Output:

enter image description here

  • With Microsoft.Azure.Devices.Client from this reference MSDOC
   DeviceClient deviceClient = DeviceClient.CreateFromConnectionString(connectionString, TransportType.Mqtt);

 
   deviceClient.SetConnectionStatusChangesHandler((status, reason) =>
   {
       logger.LogInformation($"Connection Status Changed - Status: {status}, Reason: {reason}");
   });

   await deviceClient.OpenAsync();

enter image description here