我想在不必为每个作业方法添加它的情况下,将Hangfire JobId(和其他属性)添加到所有作业中。
在Startup.cs中,我添加了一个有前途的过滤器解决方案,但我不知道如何将其应用于我的测试方法,而且我甚至不确定这是否是正确的方法。
Startup.cs
...
GlobalJobFilters.Filters.Add(new JobLoggerAttribute());
JobLoggerAttribute类 - 来自这里:链接
我的任务是在不手动添加到测试方法的情况下,在当前空白单元格中放置JobId:
![enter image description here](https://istack.dev59.com/3F1hk.webp)
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.WriteTo.Elasticsearch....
.Enrich.FromLogContext()
.CreateLogger();
因为我也想检索方法名等信息,所以我添加了这个类来调用我的作业中的Here方法。
LoggerExtensions.cs:
public static class LoggerExtensions
{
public static ILogger Here(this ILogger logger,
[CallerMemberName] string memberName = "",
[CallerFilePath] string sourceFilePath = "",
[CallerLineNumber] int sourceLineNumber = 0
)
{
return logger
.ForContext("MemberName", memberName)
.ForContext("FilePath", sourceFilePath)
.ForContext("LineNumber", sourceLineNumber)
;
}
}
RunTestTask:
public void RunTestTask(PerformContext context)
{
// instead of having to write this line in all methods
// LogContext.PushProperty("JobId", context.BackgroundJob.Id);
Log.Logger.Here().Information(string.Format("Serilog: Hanfire Job {1} Scheduled at {0}", DateTime.Now, context.BackgroundJob.Id));
}
任务队列:
BackgroundJob.Enqueue(() => new Jobs.Test.TestService().RunTestTask(null));
希望能获得技巧和窍门。
谢谢。