How To Setup Diagnostic Logs With An IoT Hub

Introduction

 
If a problem occurs in your IoT Hub solution while running in production, you may want to enable diagnostic logs because that can help you diagnose the problem and fix it immediately. In this article, let us see how to enable diagnostic logs with an IoT Hub.
 
Please read the previous parts of the article before continuing with this one.
  1. How To Create Azure IoT Hub Using PowerShell
  2. How to Register IoT Device in Azure IoT Hub Using PowerShell
  3. How To Send Telemetry From An IoT Device To The Azure IoT Hub Using C#

Enable the diagnostic logs

 
When creating a new IoT Hub, the diagnostic logs are disabled by default. Let's see how to enable them.
  • Open your Azure portal and log in to your Azure account.
  • Select your IoT Hub from the resource groups.
  • Click "Diagnostic settings" in the Monitoring section. You can see the diagnostic status is disabled by default.
How To Setup Diagnostic Logs With An IoT Hub
  • Now, double click the resource. You can "Turn on diagnostics" like in the below image. 
How To Setup Diagnostic Logs With An IoT Hub
  • Once you click the "Turn on diagnostics" link, the Diagnostic settings pane is displayed, as shown below. 
How To Setup Diagnostic Logs With An IoT Hub
  • Enter the name for your diagnostic setting. Here, I named it diags-hub.
  • Check "Archive to a storage account".
  • Click "Configure" to select the storage account screen.
  • Choose your storage account and click OK to return to the diagnostic settings pane.
How To Setup Diagnostic Logs With An IoT Hub
  • Under LOG, check "Connections" and "Device Telemetry" and set the Retention (days) to 7 for each one. Your Diagnostic settings should look like this. 
How To Setup Diagnostic Logs With An IoT Hub
  • Click "Save" to save the settings. Close the Diagnostic settings pane.
  • You can see a new setting that we created in the below image.
How To Setup Diagnostic Logs With An IoT Hub
  • IoT Hub connections and device telemetry logs are sent to the specified destination as soon as a new event data is generated.

To See the diagnostic logs

  • Go to your resource group and select your storage account, i.e., Hubflyiotstoragedev.
  • Select the Blobs from the Blob service, you can see the insight-logs-connections container.
How To Setup Diagnostic Logs With An IoT Hub
  • Select the insight-logs-connections container. Drill down until you get the current date and choose the recent file.
     
     
    How To Setup Diagnostic Logs With An IoT Hub
  • Click "Download" to download and open it. You can see the logs of device connections and device telemetry to IoT Hub. See the below image sample.
     
     
    How To Setup Diagnostic Logs With An IoT Hub
     
    That’s it. I hope you have learned how to set up diagnostic logs with an IoT Hub. Feel free to fill up the comment box below if you need any further assistance.