Azure service fabric 参与者方法开始/停止日志|添加其他信息

Azure service fabric 参与者方法开始/停止日志|添加其他信息,azure-service-fabric,Azure Service Fabric,对于azure actor服务,actor方法在诊断窗口中启动停止日志,如下所示 如何在每次调用方法时添加一些额外的细节,例如相关Id { "Timestamp": "2016-09-14T19:46:40.9955448+05:30", "ProviderName": "Microsoft-ServiceFabric-Actors", "Id": 7, "Message": "Actor method is being invoked. Method name: IStore.

对于azure actor服务,actor方法在诊断窗口中启动停止日志,如下所示

如何在每次调用方法时添加一些额外的细节,例如相关Id

{
  "Timestamp": "2016-09-14T19:46:40.9955448+05:30",
  "ProviderName": "Microsoft-ServiceFabric-Actors",
  "Id": 7,
  "Message": "Actor method is being invoked. Method name: IStore.GetStoreById, actor type: Backend.Actor.Store.Store, actor ID: STORE_6.",
  "ProcessId": 30736,
  "Level": "Verbose",
  "Keywords": "0x0000F00000000002",
  "EventName": "ActorMethod/Start",
  "Payload": {
    "methodName": "IStore.GetStoreById",
    "methodSignature": "System.Threading.Tasks.Task`1[Backend.Models.Store.StoreView] GetStoreById(System.String)",
    "actorType": "Backend.Actor.Store.Store",
    "actorId": "STORE_6",
    "actorIdKind": 2,
    "replicaOrInstanceId": 131183360004211655,
    "partitionId": "8af1c125-3666-40d0-b630-e3570c41833b",
    "serviceName": "fabric:/MultiBannerBackend/StoreActorService",
    "applicationName": "fabric:/MultiBannerBackend",
    "serviceTypeName": "StoreActorServiceType",
    "applicationTypeName": "MultiBannerBackendType",
    "nodeName": "_Node_4"
  }
}

为了记录每个参与者操作的自定义数据,可以使用以下方法:

protected override Task OnPreActorMethodAsync(ActorMethodContext c)
protected override Task OnPostActorMethodAsync(ActorMethodContext c)
为了获取调用上下文,我发现CallContext.LogicalGetData在这种情况下不起作用。幸运的是,演员本身确实知道它的背景。你可以通过一些反射得到它

例如:

protected override Task OnPreActorMethodAsync(ActorMethodContext c)
        {
            var correlationID = this.GetActorContext() ?? Guid.Empty.ToString("N");
            string message = $"Actor method is being invoked. Method name: {c.MethodName}, actor type: {GetType().FullName}, actor ID: {Id}, CorrelationID:{correlationID}";
            ActorEventSource.Current.ActorMessage(this, message);
            return Task.FromResult(true);
        }

        protected override Task OnPostActorMethodAsync(ActorMethodContext c)
        {
            var correlationID = this.GetActorContext() ?? Guid.Empty.ToString("N");
            string message = $"Actor method has completed. Method name: {c.MethodName}, actor type: {GetType().FullName}, actor ID: {Id}, CorrelationID:{correlationID}";
            ActorEventSource.Current.ActorMessage(this, message);
            return Task.FromResult(true);
        }
结合:

public static class ActorContextExtensions
    {
        public static string GetActorContext(this Actor actor)
        {
            var concurrencyLockProperty = GetPropertyInfo("ConcurrencyLock", typeof(ActorBase));
            var concurrencyLockPropertyValue = concurrencyLockProperty.GetValue(actor);

            var currentContextProperty = GetPropertyInfo("Test_CurrentContext", concurrencyLockPropertyValue.GetType());
            string currentContextPropertyValue = (string)currentContextProperty.GetValue(concurrencyLockPropertyValue);
            return currentContextPropertyValue;
        }

        private static PropertyInfo GetPropertyInfo(string propertyName, IReflect owner)
        {
            var property = owner.GetProperty(propertyName, BindingFlags.NonPublic | BindingFlags.Instance);
            if (property == null) throw new InvalidOperationException($"Failed to find property '{propertyName}' on '{owner}'.");
            return property;
        }
    }

这样做的明显缺点是,每当ActorBase的内部发生更改时,您都需要相应地更改反射代码。

为了记录每个Actor操作的自定义数据,您可以使用以下方法:

protected override Task OnPreActorMethodAsync(ActorMethodContext c)
protected override Task OnPostActorMethodAsync(ActorMethodContext c)
为了获取调用上下文,我发现CallContext.LogicalGetData在这种情况下不起作用。幸运的是,演员本身确实知道它的背景。你可以通过一些反射得到它

例如:

protected override Task OnPreActorMethodAsync(ActorMethodContext c)
        {
            var correlationID = this.GetActorContext() ?? Guid.Empty.ToString("N");
            string message = $"Actor method is being invoked. Method name: {c.MethodName}, actor type: {GetType().FullName}, actor ID: {Id}, CorrelationID:{correlationID}";
            ActorEventSource.Current.ActorMessage(this, message);
            return Task.FromResult(true);
        }

        protected override Task OnPostActorMethodAsync(ActorMethodContext c)
        {
            var correlationID = this.GetActorContext() ?? Guid.Empty.ToString("N");
            string message = $"Actor method has completed. Method name: {c.MethodName}, actor type: {GetType().FullName}, actor ID: {Id}, CorrelationID:{correlationID}";
            ActorEventSource.Current.ActorMessage(this, message);
            return Task.FromResult(true);
        }
结合:

public static class ActorContextExtensions
    {
        public static string GetActorContext(this Actor actor)
        {
            var concurrencyLockProperty = GetPropertyInfo("ConcurrencyLock", typeof(ActorBase));
            var concurrencyLockPropertyValue = concurrencyLockProperty.GetValue(actor);

            var currentContextProperty = GetPropertyInfo("Test_CurrentContext", concurrencyLockPropertyValue.GetType());
            string currentContextPropertyValue = (string)currentContextProperty.GetValue(concurrencyLockPropertyValue);
            return currentContextPropertyValue;
        }

        private static PropertyInfo GetPropertyInfo(string propertyName, IReflect owner)
        {
            var property = owner.GetProperty(propertyName, BindingFlags.NonPublic | BindingFlags.Instance);
            if (property == null) throw new InvalidOperationException($"Failed to find property '{propertyName}' on '{owner}'.");
            return property;
        }
    }

这样做的明显缺点是,每当ActorBase的内部发生更改时,您都需要相应地更改您的反射代码。

非常感谢,这正是我要找的。非常感谢,这正是我要找的。