【问题标题】:NServiceBus.Host.exe as windows service log4net issueNServiceBus.Host.exe 作为 Windows 服务 log4net 问题
【发布时间】:2012-07-03 12:28:41
【问题描述】:

我使用 NServiceBus 2.6。

我使用 log4net.config 文件并通过端点中的代码初始化日志记录工具:

SetLoggingLibrary.Log4Net(() => 
       XmlConfigurator.ConfigureAndWatch(new FileInfo("log4net.config")));

当我正常运行 NServiceBus.Host.exe 时,它​​记录良好,但当我使用 /install 将其部署为 Windows 服务时,不再记录任何内容。

如果我使用基于代码的方式,它会起作用:

SetLoggingLibrary.Log4Net<RollingFileAppender>(...)

任何想法为什么...?

【问题讨论】:

  • 运行该服务的帐户是否对该文件具有适当的权限?还有代码在哪里实现(你实现了什么接口)?
  • 嗨,亚当,抱歉耽搁了我。正如我在下面所说,只需添加“AppDomain.CurrentDomain.BaseDirectory”就可以了。无论如何,看来我的示例在 Debug 中没有使用该技巧,但在 Release 中却没有。我将使用更简单的端点从头开始,以确定哪些有效,哪些无效,之后我将重新发布。无论如何,感谢您的关注并继续您的精彩博客!

标签: windows-services log4net nservicebus


【解决方案1】:

将进程作为 Windows 服务运行时,默认情况下不会将当前目录设置为二进制文件所在的位置。这意味着当您查找“log4net.config”时,它实际上会在 c:\windows\system32 中搜索。

试试这个(在调用 SetLoggingLibrary 之前):

System.IO.Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);

或者,将您的代码修改为以下内容:

SetLoggingLibrary.Log4Net(() => 
   XmlConfigurator.ConfigureAndWatch(new FileInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log4net.config"))));

【讨论】:

    猜你喜欢
    • 2011-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-26
    • 1970-01-01
    • 1970-01-01
    • 2010-10-19
    相关资源
    最近更新 更多