I have an Azure IoT hub that I connect devices to. I want to enable monitoring to monitor devices connecting and disconnecting from the hub.
I've enabled Verbose on Connections in the monitoring categories for my Iot hub:
My devices connect to my Hub and show in Device Explorer:
I then have an Azure Function set to log my data from the Operations Monitoring to an Azure SQL db:
using System;
using System.Configuration;
using System.Data.SqlClient;
using Newtonsoft.Json;
public static void Run(string myEventHubMessage, TraceWriter log)
{
log.Info(myEventHubMessage);
try
{
var connectionString = ConfigurationManager.ConnectionStrings["iotAzureSQLDb"].ConnectionString;
log.Info(connectionString);
var message = JsonConvert.DeserializeObject<MonitoringMessage>(myEventHubMessage);
using (var connection = new SqlConnection(connectionString))
{
var sqlStatement = "insert into [dbo].[DeviceStatuses] " +
"(DeviceId, ConnectionStatus, ConnectionUpdateTime)" +
"values " +
"(@DeviceId, @ConnectionStatus, @ConnectionUpdateTime)";
using (var dataCommand = new SqlCommand(sqlStatement, connection))
{
dataCommand.Parameters.AddWithValue("@ConnectionStatus", message.operationName);
dataCommand.Parameters.AddWithValue("@DeviceId", message.deviceId);
dataCommand.Parameters.AddWithValue("@ConnectionUpdateTime", message.time);
connection.Open();
dataCommand.ExecuteNonQuery();
connection.Close();
log.Info($"Device {message.deviceId} changed state: {message.operationName}");
}
}
}
catch (Exception ex)
{
log.Info(ex.Message);
}
}
public class MonitoringMessage
{
public string deviceId { get; set; }
public string operationName { get; set; }
public int? durationMs { get; set; }
public string authType { get; set; }
public string protocol { get; set; }
public DateTimeOffset? time { get; set; }
public string category { get; set; }
public string level { get; set; }
public int? statusCode { get; set; }
public int? statusType { get; set; }
public string statusDescription { get; set; }
}
If I enable Device Identity Operations in Operations Monitoring, I get create events being logged. So I'm confident the inputs to the function is correct. However, nothing is ever created for Connections???
I can also send messages to my connected devices fine. I'm just seeing no events for connections / disconnections.
I've also tried creating a Stream Analytics and sampling the input for a period where I know I have connections / disconnections and nothing is being found.