如何将不同的日志级别记录到log4net中的不同日志附加程序

发布于 2024-11-15 12:58:30 字数 4071 浏览 2 评论 0原文

我有一个 WinForm,它显示日志信息,但我希望它仅显示 INFO 级别,但我希望日志文件中的日志也包含 DEBUG 级别。

下面是我的配置:

<?xml version="1.0" encoding="utf-8" ?>
<!-- This section contains the log4net configuration settings -->
<log4net>
  <!-- Define some output appenders -->

  <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
    <file value="${ALLUSERSPROFILE}\\Application Data\\logs\\Gateway" />
    <appendToFile value="true" />
    <rollingStyle value="Date" />
    <param name="StaticLogFileName" value="false" />
    <datePattern value=".yyyyMMdd.lo\g" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date | [%thread] | %-5level | %logger | %message %newline" />
    </layout>
  </appender>

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

  <appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
    <mapping>
      <level value="DEBUG" />
      <backColor value="Blue" />
    </mapping>
    <mapping>
      <level value="INFO" />
      <backColor value="Green" />
    </mapping>
    <mapping>
      <level value="WARN" />
      <backColor value="Yellow" />
    </mapping>
    <mapping>
      <level value="ERROR" />
      <backColor value="Red" />
    </mapping>
    <mapping>
      <level value="FATAL" />
      <backColor value="Red, HighIntensity" />
    </mapping>
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
    </layout>
  </appender>

  <appender name="RichTextBoxAppender" type="Gateway.RichTextBoxAppender,Gateway" >
    <layout type="log4net.Layout.PatternLayout">
      <param name="Header" value="[Header]\r\n"/>
      <param name="Footer" value="[Footer]\r\n"/>
      <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n" />
    </layout>
    <mapping>
      <level value="DEBUG" />
      <textColorName value="DarkGreen" />
    </mapping>
    <mapping>
      <level value="INFO" />
      <textColorName value="ControlText" />
    </mapping>
    <mapping>
      <level value="WARN" />
      <textColorName value="Blue" />
    </mapping>
    <mapping>
      <level value="ERROR" />
      <textColorName value="Red" />
      <bold value="true" />
      <pointSize value="10" />
    </mapping>
    <mapping>
      <level value="FATAL" />
      <textColorName value="Black" />
      <backColorName value="Red" />
      <bold value="true" />
      <pointSize value="12" />
      <fontFamilyName value="Lucida Console" />
    </mapping>
  </appender>


  <!-- Setup the root category, add the appenders and set the default level -->
  <root>
    <level value="WARN" />
    <appender-ref ref="LogFileAppender" />
    <appender-ref ref="ColoredConsoleAppender" />
    <appender-ref ref="RichTextBoxAppender" />
  </root>

  <logger name ="Gateway" additivity="false">
    <level value="INFO" />
    <appender-ref ref="LogFileAppender" />
    <appender-ref ref="ColoredConsoleAppender" />
    <appender-ref ref="RichTextBoxAppender" />
  </logger>



</log4net>

现在 LogFileAppender 和 RichTextBoxAppender 都设置为 INFO。我尝试取出网关记录器中的 LogFileAppender 并移动到另一个记录器,并将这个新记录器设置为“DEBUG”级别,但它仍在执行其中之一。如何配置它以使 LogFileAppender 具有 DEBUG 级别而 RichTextBoxAppender 具有 INFO 级别?

I have a WinForm that it display log information but I want it to display to only INFO level but I want the log in the log file to contain DEBUG level as well.

Below is my configuration:

<?xml version="1.0" encoding="utf-8" ?>
<!-- This section contains the log4net configuration settings -->
<log4net>
  <!-- Define some output appenders -->

  <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
    <file value="${ALLUSERSPROFILE}\\Application Data\\logs\\Gateway" />
    <appendToFile value="true" />
    <rollingStyle value="Date" />
    <param name="StaticLogFileName" value="false" />
    <datePattern value=".yyyyMMdd.lo\g" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date | [%thread] | %-5level | %logger | %message %newline" />
    </layout>
  </appender>

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

  <appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
    <mapping>
      <level value="DEBUG" />
      <backColor value="Blue" />
    </mapping>
    <mapping>
      <level value="INFO" />
      <backColor value="Green" />
    </mapping>
    <mapping>
      <level value="WARN" />
      <backColor value="Yellow" />
    </mapping>
    <mapping>
      <level value="ERROR" />
      <backColor value="Red" />
    </mapping>
    <mapping>
      <level value="FATAL" />
      <backColor value="Red, HighIntensity" />
    </mapping>
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
    </layout>
  </appender>

  <appender name="RichTextBoxAppender" type="Gateway.RichTextBoxAppender,Gateway" >
    <layout type="log4net.Layout.PatternLayout">
      <param name="Header" value="[Header]\r\n"/>
      <param name="Footer" value="[Footer]\r\n"/>
      <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n" />
    </layout>
    <mapping>
      <level value="DEBUG" />
      <textColorName value="DarkGreen" />
    </mapping>
    <mapping>
      <level value="INFO" />
      <textColorName value="ControlText" />
    </mapping>
    <mapping>
      <level value="WARN" />
      <textColorName value="Blue" />
    </mapping>
    <mapping>
      <level value="ERROR" />
      <textColorName value="Red" />
      <bold value="true" />
      <pointSize value="10" />
    </mapping>
    <mapping>
      <level value="FATAL" />
      <textColorName value="Black" />
      <backColorName value="Red" />
      <bold value="true" />
      <pointSize value="12" />
      <fontFamilyName value="Lucida Console" />
    </mapping>
  </appender>


  <!-- Setup the root category, add the appenders and set the default level -->
  <root>
    <level value="WARN" />
    <appender-ref ref="LogFileAppender" />
    <appender-ref ref="ColoredConsoleAppender" />
    <appender-ref ref="RichTextBoxAppender" />
  </root>

  <logger name ="Gateway" additivity="false">
    <level value="INFO" />
    <appender-ref ref="LogFileAppender" />
    <appender-ref ref="ColoredConsoleAppender" />
    <appender-ref ref="RichTextBoxAppender" />
  </logger>



</log4net>

Right now it is set to INFO for both LogFileAppender and RichTextBoxAppender. I tried taking out the LogFileAppender in Gateway logger and move to another logger and set to "DEBUG" level for this new logger but it is still doing one or the other. How do I configure it so that the LogFileAppender has DEBUG level and RichTextBoxAppender has INFO level?

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

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

发布评论

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

评论(2

沙沙粒小 2024-11-22 12:58:30

您应该能够向两个附加程序添加过滤器。

<filter type="log4net.Filter.LevelRangeFilter">
         <levelMin value="INFO" />
         <levelMax value="FATAL" />
</filter>

这样,一个附加程序将仅记录到某一级别,而另一个附加程序将记录到不同的级别,即使它们是由同一记录器定义的。

You should be able to add a filter to both of your appenders.

<filter type="log4net.Filter.LevelRangeFilter">
         <levelMin value="INFO" />
         <levelMax value="FATAL" />
</filter>

This way one appender will only log to a certain level while the other to a different level even though they are defined by the same logger.

落花浅忆 2024-11-22 12:58:30

我同意@gwhitake 的观点,您可以使用级别范围过滤器。不过,我还想补充一点,如果您只想选择一个级别,则可以使用 LevelMatch 过滤器。此过滤器还允许您在其末尾添加另一个过滤器,以便您可以创建具有两个或多个级别的过滤器,即使它们在顺序上彼此不相邻。

例如,以下过滤器将仅捕获 DEBUG 和 ERROR 消息(仅作为示例):

<filter type="log4net.Filter.LevelMatchFilter">
  <levelToMatch value="DEBUG"/>
</filter>
<filter type="log4net.Filter.LevelMatchFilter">
  <levelToMatch value="ERROR"/>
</filter>
<filter type="log4net.Filter.DenyAllFilter" />

这里的一个关键是我们需要在末尾添加 DenyAllFilter。这告诉记录器如果到达此行,则不要记录它。这样它将记录 DEBUG 消息、ERROR 消息,而不记录任何其他消息。

有很多方法可以操纵过滤器以获得您想要的结果。这是一篇文章(完全免责声明:我写的),它向您展示了如何使用过滤器执行除级别范围之外的许多不同操作:

http://www.codeproject.com/KB/dotnet/Log4net_Tutorial.aspx

I agree with @gwhitake that you can use the level range filter. I also want to add, however, that you can use the LevelMatch filter if you just want to select one level. This filter allows you to add another filter at the end of it as well so that you could create a filter that had two or more levels even if they aren't next to each other in the order.

For example, the following filter will capture only DEBUG and ERROR messages (just as an example):

<filter type="log4net.Filter.LevelMatchFilter">
  <levelToMatch value="DEBUG"/>
</filter>
<filter type="log4net.Filter.LevelMatchFilter">
  <levelToMatch value="ERROR"/>
</filter>
<filter type="log4net.Filter.DenyAllFilter" />

The one key here is that we need to have the DenyAllFilter at the end. This tells the logger that if it reaches this line, don't log it. That way it will log DEBUG messages, ERROR messages, and nothing else.

There are a lot of ways to manipulate the filter to get exactly what you want. Here is an article (full disclaimer: I wrote it) that shows you how to do a number of different things with the filter besides just level range:

http://www.codeproject.com/KB/dotnet/Log4net_Tutorial.aspx

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