我正在使用Azure Device SDK .NET Core,以便将我的设备连接到 Azure IoT 中心。服务器有时会拒绝来自设备的一些消息(例如孪生更新或遥测消息)并以状态代码 400 进行响应。因此,客户端会抛出异常,但由于其异步性质,它们会被吞没在 Azure 内部的某个位置SDK,并且从未向我的代码抛出过问题。
我如何才能真正收到有关这些错误的通知,以便我可以处理和显示它们?
我还可以从 Azure Device SDK 代码中看到它使用某种日志记录 (EventSource),但这在代码中从未启用:
From Logging.Common.cs:
Log.IsEnabled() // always returns false
您能否指出一些方法,让我能够 1) 在 Azure 设备 SDK 中实际启用日志记录并 2) 查找实际记录的内容?
更新:有关在某处被吞没的异常的详细信息
// Fired here after I send twin reported properties to server:
AmqpTransportHandler.VerifyResponseMessage:
if (status >= 400)
{
throw new InvalidOperationException("Service rejected the message with status: " + status);
}
// Then becomes caught and re-fired here:
AmqpTransportHandler.SendTwinPatchAsync:
throw AmqpClientHelper.ToIotHubClientContract(exception);
// Then it disappears somewhere in the "dance" of the async tasks
最佳答案
您可以捕获痕迹:https://github.com/Azure/azure-iot-sdk-csharp/tree/master/tools/CaptureLogs
我们的示例演示了有关异常捕获的最佳实践,例如:https://github.com/Azure/azure-iot-sdk-csharp/blob/master/iothub/device/samples/DeviceClientMqttSample/Program.cs
关于azure - 如何捕获 Azure 设备 SDK 中引发的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53301710/