运行以发布模式构建的 .Net 4.0 Windows 应用程序时,log4net 不记录日志

发布于 2024-09-29 04:47:46 字数 2759 浏览 6 评论 0原文

我有一个使用 log4net 的 .Net 4.0 Windows 服务应用程序。

这是我的 log4net 配置。

<log4net>
  <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger [%property{Context}] - %message%newline" />
    </layout>
  </appender>

  <appender name="ColorConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
    <mapping>
      <level value="ERROR" />
      <foreColor value="Red, HighIntensity" />
    </mapping>
    <mapping>
      <level value="FATAL" />
      <foreColor value="Red, HighIntensity" />
    </mapping>
    <mapping>
      <level value="WARN" />
      <foreColor value="Blue, HighIntensity" />
    </mapping>
    <mapping>
      <level value="INFO" />
      <forecolor value="White, HighIntensity" />
    </mapping>
    <mapping>
      <level value="DEBUG" />
      <forecolor value="Green, HighIntensity" />
    </mapping>
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger [%property{Context}] - %message%newline" />
    </layout>
  </appender>

  <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
    <appendToFile value="true" />
    <datePattern value="yyyyMMdd" />
    <file value="Logs/Server.log" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger [%property{Context}] - %message%newline" />
    </layout>
    <maximumFileSize value="10MB" />
    <rollingStyle value="Composite" />
    <staticLogFileName value="true" />
  </appender>

  <appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger [%property{Context}] - %message%newline" />
    </layout>
  </appender>

  <root>
    <level value="ALL" />
    <appender-ref ref="ColorConsoleAppender" />
    <appender-ref ref="RollingFileAppender" />
  </root>

</log4net>

当服务在使用调试模式下构建的 exe 启动时(日志文件在正确的文件路径中创建),日志记录会按预期工作,但在使用发布模式下构建的 exe 时,日志记录不起作用。

我尝试按照本博客中提到的步骤使用重新编译的 .Net 4.0 log4net dll。 http://tseonet.blogspot.com/2010/07/making-log4net-run-on-net-40.html

但是,这也不起作用。

非常感谢任何帮助。

I have a .Net 4.0 windows service application that uses log4net.

Here's my log4net config.

<log4net>
  <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger [%property{Context}] - %message%newline" />
    </layout>
  </appender>

  <appender name="ColorConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
    <mapping>
      <level value="ERROR" />
      <foreColor value="Red, HighIntensity" />
    </mapping>
    <mapping>
      <level value="FATAL" />
      <foreColor value="Red, HighIntensity" />
    </mapping>
    <mapping>
      <level value="WARN" />
      <foreColor value="Blue, HighIntensity" />
    </mapping>
    <mapping>
      <level value="INFO" />
      <forecolor value="White, HighIntensity" />
    </mapping>
    <mapping>
      <level value="DEBUG" />
      <forecolor value="Green, HighIntensity" />
    </mapping>
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger [%property{Context}] - %message%newline" />
    </layout>
  </appender>

  <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
    <appendToFile value="true" />
    <datePattern value="yyyyMMdd" />
    <file value="Logs/Server.log" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger [%property{Context}] - %message%newline" />
    </layout>
    <maximumFileSize value="10MB" />
    <rollingStyle value="Composite" />
    <staticLogFileName value="true" />
  </appender>

  <appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger [%property{Context}] - %message%newline" />
    </layout>
  </appender>

  <root>
    <level value="ALL" />
    <appender-ref ref="ColorConsoleAppender" />
    <appender-ref ref="RollingFileAppender" />
  </root>

</log4net>

Logging works as expected when the service starts up while using the exe that was built in debug mode (log files are created at the right file path), but doesn't work when it is using the exe built in release mode.

I've tried using a recompiled log4net dll for .Net 4.0 following the steps mentioned at this blog.. http://tseonet.blogspot.com/2010/07/making-log4net-run-on-net-40.html.

But, that didn't work either.

Any help is greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深居我梦 2024-10-06 04:47:46

我建议打开内部调试,如下所述:

log4net - Appenders无法在 IIS7.5 中工作

I suggest to turn on internal debugging as explained here:

log4net - Appenders not working in IIS7.5

情绪操控生活 2024-10-06 04:47:46

感谢 Deepu

将 [程序集:log4net.Config.XmlConfigurator(Watch = true)] 添加到对我有用的 assemblyInfo 文件中

Thanks Deepu

adding [assembly: log4net.Config.XmlConfigurator(Watch = true)] to the assemblyInfo file worked for me

我是有多爱你 2024-10-06 04:47:46

我今天偶然发现了同样的问题,在浏览了 log4net 文档 后,我做了以下对我有用。

log4net.Config.XmlConfigurator.Configure();

认为这值得分享。下面是使用 log4net 作为提供程序的记录器类,

/// <summary>
/// Logger class using log4net library
/// </summary>
public class Log4NetLogger : ILogger
{
    public Log4NetLogger(Type configType)
    {
        log4net.Config.XmlConfigurator.Configure();
        this._logger = LogManager.GetLogger(configType);
    }

}

对我来说,程序集属性不是一个选项,因为我将日志记录功能包装到一个单独的程序集中,其中 log4net 是提供程序之一。

I stumbled upon the same issue today and after going through log4net docs did the following and it worked for me.

log4net.Config.XmlConfigurator.Configure();

Thought it will be worth sharing. Below is the logger class which uses log4net as provider

/// <summary>
/// Logger class using log4net library
/// </summary>
public class Log4NetLogger : ILogger
{
    public Log4NetLogger(Type configType)
    {
        log4net.Config.XmlConfigurator.Configure();
        this._logger = LogManager.GetLogger(configType);
    }

}

For me assembly attribute was not an option as I wrapped logging functionality into a separate assembly where log4net was one of the providers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文