我目前正在使用 Hangfire 在 Windows 服务中运行一些作业。这些作业是从 WebAPI 触发的。
当所有作业都成功时,我的系统目前工作正常,但是当发生异常时我没有得到任何日志记录。有没有人有使用自定义记录器从 Hangfire 接收消息的经验?
我的记录器是一个基本的 NLog 接口:
public class Logger : ILogger
{
public readonly NLog.Logger logger;
public Logger(string name)
{
if (LogManager.Configuration == null)
{
FallbackInitialisation();
}
logger = LogManager.GetLogger(name);
}
public Trace(string message)
//etc.
}
我正在我的 Windows 服务中配置我的 Hangfire 作业服务器,如下所示:
SqlServerStorage storage = new SqlServerStorage("myConnectionString");
BackgroundJobServerOptions options = new BackgroundJobServerOptions();
m_server = new BackgroundJobServer(options, storage);
GlobalJobFilters.Filters.Add(new AutomaticRetryAttribute { Attempts = 0 });
根据hangfire文档,我只是尝试通过添加所需的引用来遵循它,并在我的作业服务器设置后添加以下内容,但是仍然没有产生日志:
var properties = new NameValueCollection();
properties["configType"] = "INLINE";
LogManager.Adapter = new NLogLoggerFactoryAdapter(properties);
我只是throw new Exception()
在作业调用的方法中模拟异常。该作业是在 webApi 调用中创建的,如下所示:
[HttpGet]
public void TestStartJob()
{
m_logger.Trace("TestStartJob");
BackgroundJob.Enqueue(() => m_service.TestStartJob());
}
我正在寻找的是,是否有人在正确配置日志记录以与 Hangfire 一起使用方面有任何经验。