简体   繁体   English

EventHub 发送消息,但 Application Insights 上的 System.Net.Sockets.SocketException

[英]EventHub send message, but System.Net.Sockets.SocketException on Application Insights

I have this code below that sends message to azure EventHub, at first it works fine我在下面有这个代码,它将消息发送到 azure EventHub,起初它工作正常

public async Task SendMessage(string message) {
            
    var producer = new EventHubProducerClient(this.connectionString, this.eventHubName);
   
    using (EventDataBatch eventBatch = await producer.CreateBatchAsync()) {
        if (!eventBatch.TryAdd(new EventData(message))) {
            throw new Exception($"Event is too large for the batch and cannot be sent.");
        }

        try {
            await producer.SendAsync(eventBatch);
        }
        catch(Exception ex) {
            saveLog(message, ex);
        }
        finally {
            await producer.DisposeAsync();
        }
    }
}

but in Application Insights I'm facing this message exception:但在 Application Insights 中,我遇到了此消息异常:

A Task's exception(s) were not observed either by Waiting on the Task or accessing its Exception property.通过等待任务或访问其异常属性未观察到任务的异常。 As a result, the unobserved exception was rethrown by the finalizer thread.结果,未观察到的异常被终结器线程重新抛出。 A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond连接尝试失败,因为连接的一方在一段时间后没有正确响应,或者连接失败,因为连接的主机没有响应

Exception type:异常类型:

System.Net.Sockets.SocketException System.Net.Sockets.SocketException

I've tried to add a ContinueWith (OnlyOnFaulted or OnlyOnCanceled), as below, to log it, but now I get in the catch Exception (not in ExceptionHandle ) as "A task was canceled"我试图添加一个 ContinueWith (OnlyOnFaulted 或 OnlyOnCanceled),如下所示,以记录它,但现在我进入 catch Exception (不在 ExceptionHandle 中)作为“任务已取消”

try {    
  await producer.SendAsync(eventBatch)
   .ContinueWith(t => ExceptionHandle(t, message), TaskContinuationOptions.OnlyOnFaulted);
                }

How can I handle these exceptions that App Insights display?如何处理 App Insights 显示的这些异常?

If you need to handle the task exception by using a ContinueWith ( OnlyOnFaulted or OnlyOnCanceled ) we can get this exception in catch block only.如果您需要使用ContinueWithOnlyOnFaultedOnlyOnCanceled )来处理任务异常,我们只能在 catch 块中获取此异常。 By default, if the task runs successfully, it throws Exception if the Task IsFaulted we can get the exception from Catch Block .默认情况下,如果任务运行成功,它会抛出异常,如果任务IsFaulted我们可以从Catch Block中获取异常。

To fix this issue:要解决此问题:

Use UnobservedTaskException event which observe the Task Exceptions and it doesn't terminate by default.使用观察任务异常的UnobservedTaskException事件,默认情况下它不会终止。 The exception can be handled by the runtime .异常可以由运行时处理

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM