2014-11-24 107 views
1

我目前使用Hangfire在Windows服务中运行一些作业。这些工作是从WebAPI发起的。登录到NLog登录Hangfire.io

当所有作业都成功时,我的系统目前工作正常,但是当发生异常时我没有记录日志。有没有人有使用自定义记录器接收来自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服务,像这样:

SqlServerStorage storage = new SqlServerStorage("myConnectionString"); 
BackgroundJobServerOptions options = new BackgroundJobServerOptions(); 
m_server = new BackgroundJobServer(options, storage); 
GlobalJobFilters.Filters.Add(new AutomaticRetryAttribute { Attempts = 0 }); 

基础上hangfire documentation,我只是试图跟随它通过添加所需的参考,并在我的作业服务器设置后添加以下内容,但是仍然没有生成日志:

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()); 
} 

我所寻找的,是如果任何人有在得到正确的日志记录配置为与迟发型使用任何经验。

+0

迟发型登录表HangFire.State所有异常。你确定你需要更多日志吗? – 2014-11-24 11:08:07

+0

@KirillBestemyanov是的,我知道这存在,我可以看到在hangfire web界面中的异常,但我希望能够在我们自己的日志中看到异常,以便它可以通过web应用程序和通过记录什么时候发生的事情 – Thewads 2014-11-24 11:09:18

+0

如果'AutomaticRetryAttribute'没有产生任何异常的日志,在我看来,日志并不完全工作。你能说我使用Common.Logging的哪些版本,以及你使用Common.Logging NLog适配器的软件包和版本吗? – odinserj 2014-11-25 09:06:36

回答

0

我已成功使用的NuGet包以下版本来解决这个问题:

NLog - 3.1 
Common.Logging - 2.2.0 
JVW.Logging.CommonLoggingNLogAdapter - 1.0.0.1 

,然后配置日志这样

var properties = new NameValueCollection(); 
properties["configType"] = "INLINE"; 
Common.Logging.LogManager.Adapter = new NLogFactoryAdapter(properties);