serilogとseqを使用して多くの成功を収め、ログメッセージにプロパティを追加するためにエンリッチャーを使用しました。
私のサービスでは、ServiceLogger.CreateLogger(this)を呼び出して、サービスに関するすべての状態を豊かにしたログを取得します。相関トークンが必要な場合は、それを比較的簡単に追加できるはずですが、それはまだ私が行ったことではありません。
私は以下の関連コードをコピーしたと思います! @
public class ServiceFabricEnricher<T> : ILogEventEnricher where T : ServiceContext
{
protected T Context { get; }
private LogEventProperty _nodeName;
public ServiceFabricEnricher(T context)
{
Context = context;
}
public virtual void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
{
if (_nodeName == null) _nodeName = propertyFactory.CreateProperty("NodeName", Context.NodeContext.NodeName);
logEvent.AddPropertyIfAbsent(_nodeName);
}
}
public class ServiceEnricher<T> : ServiceFabricEnricher<T> where T : ServiceContext
{
private LogEventProperty _serviceName;
private LogEventProperty _partitionId;
private LogEventProperty _applicationName;
public ServiceEnricher(T context) : base(context)
{
}
public override void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
{
base.Enrich(logEvent, propertyFactory);
if (_serviceName == null) _serviceName = propertyFactory.CreateProperty("ServiceName", Context.ServiceName);
if (_partitionId == null) _partitionId = propertyFactory.CreateProperty("PartitionId", Context.PartitionId);
if (_applicationName == null) _applicationName = propertyFactory.CreateProperty("ApplicationName", Context.CodePackageActivationContext.ApplicationName);
logEvent.AddPropertyIfAbsent(_serviceName);
logEvent.AddPropertyIfAbsent(_partitionId);
logEvent.AddPropertyIfAbsent(_applicationName);
}
}
public static class ServiceFabricLogger
{
private static ILogger CreaterDefaultLogger()
{
var configurationProvider = new FabricConfigurationProvider("SeqConfig");
var loggerConfiguration = new LoggerConfiguration();
if (configurationProvider.HasConfiguration)
{
var seqServer = configurationProvider.GetValue("SeqServer");
loggerConfiguration =
loggerConfiguration
.WriteTo.Seq(seqServer, period: TimeSpan.FromMilliseconds(500))
;
var level = configurationProvider.GetValue("MinimumLevel");
LogEventLevel minimumLevel;
if (!string.IsNullOrWhiteSpace(level) && Enum.TryParse<LogEventLevel>(level, true, out minimumLevel))
{
loggerConfiguration = loggerConfiguration.MinimumLevel.Is(minimumLevel);
}
}
else
{
loggerConfiguration =
loggerConfiguration
.MinimumLevel.Error()
;
}
Log.Logger = loggerConfiguration.CreateLogger();
return Log.Logger;
}
public static ILogger Logger { get; } = CreaterDefaultLogger();
}
public static class ServiceLogger
{
public static ILogger CreateLogger(this StatefulServiceBase service) =>
ServiceFabricLogger.Logger.ForContext(new[] { new StatefulServiceEnricher(service.Context) });
public static ILogger CreateLogger(this StatelessService service) =>
ServiceFabricLogger.Logger.ForContext(new[] { new StatelessServiceEnricher(service.Context) });
}
ニックradell私の問題は私のピックはあなた(私はcommon.loggin + log4netのに依存している)が、それはかなり無関係であるとは異なって、その目的のために、ログインフラのモデリングに関するものではありません。 私の問題は**トークンをServiceFabricアクター/サービス内のサーバーに流す方法です**。さらにうまくいけば、WCFを使用して通信を確立することができれば、それはかなりシンプルですが、動作によってヘッダを追加すれば十分ですが、問題はリモーティングを使用して同じことをしています(デフォルトであり、 – MonDeveloper