Azure Service Fabric Actors - 未处理的异常?

jon*_*ers 9 .net c# asp.net-web-api azure-service-fabric

现在我们的ASF集群正在运行:

  • Web API项目 - 无状态和面向公众
  • Actor项目 - 主要是volatile,将数据保存在内存中,由某些API使用

我们正在尝试应用见解,我也喜欢他们的文档设置未处理的错误跟踪这里有我们的Web API项目.

问题是,我想要我们的Actor项目.

在Actor中是否存在捕获未处理错误的全局位置?我知道它是新的,也许这就是为什么我找不到这方面的文档.

现在我在每个actor方法中都这样做,但似乎不是一个很好的解决方案:

public async Task DoStuff()
{
    try
    {
        //Do all my stuff
    }
    catch (Exception exc)
    {
        //Send to Windows Event Source
        ActorEventSource.Current.ActorMessage(this, "Unhandled error in {0}: {1}", nameof(DoStuff), exc);

        //Send to Application Insights
        new TelemetryClient().TrackException(exc);

        throw exc;
    }
}
Run Code Online (Sandbox Code Playgroud)

Eli*_*bel 6

你有几个选择:

  • Actor有一个内置的ETW提供者(Microsoft-ServiceFabric-Actors),它有一个ActorMethodThrewException事件.你可以:

    • 使用外部进程收集ETW事件并将其转发到Application Insights(例如,使用SLAB或Azure诊断)
    • 使用EventListener该类来监听进程中的事件并将其转发到App Insights(稍微不那么可靠,但更简单)
  • 使用自定义ActorServiceRemotingDispatcher,它是负责向actor调度操作的类

    class CustomActorServiceRemotingDispatcher : ActorServiceRemotingDispatcher
    {
        public CustomActorServiceRemotingDispatcher(ActorService actorService) : base(actorService)
        {
        }
    
        public override async Task<byte[]> RequestResponseAsync(IServiceRemotingRequestContext requestContext, ServiceRemotingMessageHeaders messageHeaders,
            byte[] requestBodyBytes)
        {
                try
                {
                    LogServiceMethodStart(...);
    
                    result = await base.RequestResponseAsync(requestContext, messageHeaders, requestBodyBytes).ConfigureAwait(false);
    
                    LogServiceMethodStop(...);
    
                    return result;
                }
                catch (Exception exception)
                {
                    LogServiceMethodException(...);
    
                    throw;
                }
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)

    要使用此类,您需要创建自定义ActorService类并覆盖该CreateServiceReplicaListeners方法.请注意,这将覆盖ActorRemotingProviderAttribute您可能正在使用的任何内容.

    附注:

    • 您也可以使用此方法来读取您自己的标题(您还需要一个客户端自定义IServiceRemotingClientFactory来添加它们)
    • 相同的技术可以应用于Reliable Services(使用ServiceRemotingDispatcher该类)