我正在使用Hangfire进行后台工作,而使用Serilog进行日志记录。我正在尝试使用TrackingId
丰富我的serilogs,以便来自特定Hangfire作业的所有日志将具有我可以过滤的相同TrackingId
。
我在Startup.cs
中配置这样的Serilog:
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(Configuration)
.WriteTo.Seq(serverUrl: serverUrl, apiKey: apiKey)
// Enrich the logs with a tracking id. Will be a new value per request
.Enrich.WithProperty("TrackingId", Guid.NewGuid())
.CreateLogger();
我排队这样的工作:
BackgroundJob.Enqueue<MyService>(myService => myService.DoIt(someParameter));
但是这样做不会为每个Hangfire工作设置单独的TrackingId
。有什么办法可以实现吗?
为了它的价值,我最终使用服务器/客户端过滤器和GlobalJobFilters
注册将其关闭。我遇到的一个令人讨厌的问题是AutomaticRetryAttribute
默认添加到GlobalJobFilters
集合,并且该类will log errors for failed jobs不知道在我们的自定义JobLoggerAttribute
中创建的Serilog LogContext。就个人而言,我知道我只允许手动重试,所以我只删除了该属性并在IServerFilter.OnPerformed
方法中处理了错误。检查我的帖子的结尾,看看如果它适合你,如何删除它。
如果您要允许自动重试,那么您将需要:1)创建一个自定义属性来装饰AutomaticRetryAttribute
并使其知道自定义LogContext,2)再次从AutomaticRetryAttribute
集合中删除默认的GlobalJobFilters
,并且3)添加你的decorator属性集合。
public class JobLoggerAttribute : JobFilterAttribute, IClientFilter, IServerFilter
{
private ILogger _log;
public void OnCreating(CreatingContext filterContext)
{
_log = GetLogger();
_log.Information("Job is being created for {JobType} with arguments {JobArguments}", filterContext.Job.Type.Name, filterContext.Job.Args);
}
public void OnCreated(CreatedContext filterContext)
{
_log.Information("Job {JobId} has been created.", filterContext.BackgroundJob.Id);
}
public void OnPerforming(PerformingContext filterContext)
{
if (_log == null)
_log = GetLogger();
_log.Information("Job {JobId} is performing.", filterContext.BackgroundJob.Id);
}
public void OnPerformed(PerformedContext filterContext)
{
_log.Information("Job {JobId} has performed.", filterContext.BackgroundJob.Id);
if (filterContext.Exception != null)
{
_log.Error(
filterContext.Exception,
"Job {JobId} failed due to an exception.",
filterContext.BackgroundJob.Id);
}
_log = null;
}
private ILogger GetLogger()
{
return Log.ForContext(GetType()).ForContext("HangfireRequestId", Guid.NewGuid());
}
}
注册......
GlobalJobFilters.Filters.Add(new JobLoggerAttribute());
删除AutomaticRetryAttribute
...
var automaticRetryFilter = GlobalJobFilters.Filters.Where(x => x.Instance is AutomaticRetryAttribute).Single();
GlobalJobFilters.Filters.Remove(automaticRetryFilter.Instance);