为什么 log4net 仅在我的时记录错误级别是INFO?

发布于 2024-09-14 07:56:19 字数 533 浏览 3 评论 0原文

我显然不明白log4net。我的根日志级别配置如下:

<root>
   <level value="ERROR"/>
   <appender-ref ref="FileAppender" />
</root>

我的日志初始化如下所示:

private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

我的实际日志记录调用如下所示:

LOG.Error("Error submitting Registration.", exc);

这是有趣的部分。仅当我将根日志级别设置为 INFO 时,此日志语句才有效。为什么日志级别 ERROR 不起作用(我只想要错误,而不想要信息、调试等...)?

I obviously don't understand log4net. My root log level is configured as follows:

<root>
   <level value="ERROR"/>
   <appender-ref ref="FileAppender" />
</root>

My log initialization looks as follows:

private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

And my actual logging call looks as follows:

LOG.Error("Error submitting Registration.", exc);

Here's the funny part. This log statement ONLY works if I set the root log level to INFO. Why does the log level ERROR not work (and I only want errors, not Info, Debug etc...)?

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

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

发布评论

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

评论(2

一萌ing 2024-09-21 07:56:20

在我们聪明的架构师的帮助下,我解决了这个问题...

我曾经在 assemblyInfo.cs 中进行日志初始化(但这是错误的)。

我将日志初始化移至 app_startup 中的 global.asax:

    protected void Application_Start(Object sender, EventArgs e) 
    {
        string configFilePath = Server.MapPath("~/Config/Log4Net.config");
        log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo(configFilePath));
    }

server.MapPath 位非常重要,否则 log4net 会尝试从 system32\inetsrv 文件夹中读取其配置。

我认为我以前的日志记录有时会由于程序集加载的顺序或类似的原因而起作用。不管怎样,这个解决方案都是有效的。

With the help of our clever architect I figured this one out...

I used to have my log initialization in assemblyInfo.cs (but this was wrong).

I moved the log initialization to my global.asax in app_startup:

    protected void Application_Start(Object sender, EventArgs e) 
    {
        string configFilePath = Server.MapPath("~/Config/Log4Net.config");
        log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo(configFilePath));
    }

The server.MapPath bit is very important, otherwise log4net tries to read its configuration from the system32\inetsrv folder.

I think my previous logging worked from time to time due to the order in which the assemblies were loaded, or something like that. Either way, this solution works.

蹲墙角沉默 2024-09-21 07:56:19
<root>
  <level value="ALL" />
...
</root>

我的 log4net-Configfile 中的某人:

<appender name="FileAppender_FullLog" type="log4net.Appender.FileAppender">
  <file value="Log_Full.txt" />
  <appendToFile value="true" />
  <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date{yyyy-MM-dd HH:mm:ss} [%-5level] [%logger{2}] - %message%newline" />
  </layout>
</appender>

<appender name="FileAppender_SmallLog" type="log4net.Appender.FileAppender">
  <file value="Log.txt" />
  <appendToFile value="true" />
  <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date{yyyy-MM-dd HH:mm:ss} [%-5level] [%logger{2}] - %message%newline" />
  </layout>
  <filter type="log4net.Filter.LevelRangeFilter">
    <levelMin value="INFO" />
    <levelMax value="FATAL" />
  </filter>
  <filter type="log4net.Filter.DenyAllFilter" />
</appender>

<appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
  ..
</appender>

<!--LogLevel: OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL -->
<root>
  <level value="ALL" />
  <appender-ref ref="FileAppender_FullLog" />
  <appender-ref ref="FileAppender_SmallLog" />
  <appender-ref ref="SmtpAppender" />
</root>

编辑:

<appender name="FileAppender_SmallLog" type="log4net.Appender.FileAppender">
  <file value="Log.txt" />
  <appendToFile value="true" />
  <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date{yyyy-MM-dd HH:mm:ss} [%-5level] [%logger{2}] - %message%newline" />
  </layout>
  <filter type="log4net.Filter.LevelRangeFilter">
    <levelMin value="ERROR" />
    <levelMax value="FATAL" />
  </filter>
  <filter type="log4net.Filter.DenyAllFilter" />
</appender>

<!--LogLevel: OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL -->
<root>
  <level value="ALL" />
  <appender-ref ref="FileAppender_SmallLog" />
</root>

更好的方法是使用“...”,因为如果您想要此功能,您可以为每个附加程序更改此设置。

<root>
  <level value="ALL" />
...
</root>

Someone of my log4net-Configfile:

<appender name="FileAppender_FullLog" type="log4net.Appender.FileAppender">
  <file value="Log_Full.txt" />
  <appendToFile value="true" />
  <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date{yyyy-MM-dd HH:mm:ss} [%-5level] [%logger{2}] - %message%newline" />
  </layout>
</appender>

<appender name="FileAppender_SmallLog" type="log4net.Appender.FileAppender">
  <file value="Log.txt" />
  <appendToFile value="true" />
  <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date{yyyy-MM-dd HH:mm:ss} [%-5level] [%logger{2}] - %message%newline" />
  </layout>
  <filter type="log4net.Filter.LevelRangeFilter">
    <levelMin value="INFO" />
    <levelMax value="FATAL" />
  </filter>
  <filter type="log4net.Filter.DenyAllFilter" />
</appender>

<appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
  ..
</appender>

<!--LogLevel: OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL -->
<root>
  <level value="ALL" />
  <appender-ref ref="FileAppender_FullLog" />
  <appender-ref ref="FileAppender_SmallLog" />
  <appender-ref ref="SmtpAppender" />
</root>

Edit:

<appender name="FileAppender_SmallLog" type="log4net.Appender.FileAppender">
  <file value="Log.txt" />
  <appendToFile value="true" />
  <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date{yyyy-MM-dd HH:mm:ss} [%-5level] [%logger{2}] - %message%newline" />
  </layout>
  <filter type="log4net.Filter.LevelRangeFilter">
    <levelMin value="ERROR" />
    <levelMax value="FATAL" />
  </filter>
  <filter type="log4net.Filter.DenyAllFilter" />
</appender>

<!--LogLevel: OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL -->
<root>
  <level value="ALL" />
  <appender-ref ref="FileAppender_SmallLog" />
</root>

The better way ist to use "< filter type="log4net.Filter.LevelRangeFilter">..." because you can change this setting for every appender if you want this in feature.

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