Log4Net - 仅针对某些文件注销异常堆栈跟踪

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3659271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 14:43:00  来源:igfitidea点击:

Log4Net - Logging out the Exception stacktrace only for certain files

.netlog4netlog4net-configuration

提问by Rob

I currently have multiple log files in my application using log4net.

我目前使用 log4net 在我的应用程序中有多个日志文件。

I have a top level log file which contains every type of message. I also have an error log file which contains only error information. I am trying to configure it so the specific exception details and stack trace only appear in the error log file.

我有一个顶级日志文件,其中包含各种类型的消息。我还有一个只包含错误信息的错误日志文件。我正在尝试对其进行配置,以便特定异常详细信息和堆栈跟踪仅出现在错误日志文件中。

The call i am using is Log.Error(myMessage, myException);

我正在使用的电话是 Log.Error(myMessage, myException);

My config can be seen below:

我的配置可以在下面看到:

<configuration>
  <log4net>

    <root>
     <level value="ALL"/>
     <appender-ref ref="GeneralTextLog"/>
     <appender-ref ref="ErrorTextLog"/>
    </root>

<!-- The general appender rolls by date -->
<appender name="GeneralTextLog" type="log4net.Appender.RollingFileAppender">
  <filter type="log4net.Filter.LevelRangeFilter">
    <level value="ALL"/>
  </filter>
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%d{HH:mm:ss.fff} [%type] %-5p %message%n"/>
  </layout>
  <rollingStyle value="Date"/>
  <file value="C:/Logs/General_"/>
  <datePattern value="yyyy_MM_dd'.log'" />
  <appendToFile value="true"/>
  <staticLogFileName value="false"/>
</appender>

<!-- The Error appender rolls by date -->
<appender name="ErrorTextLog" type="log4net.Appender.RollingFileAppender">
  <filter type="log4net.Filter.LevelRangeFilter">
    <levelMin value="WARN"/>
    <levelMax value="FATAL"/>
  </filter>
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%d{HH:mm:ss.fff} [%type] %-5p %message%newline%exception"/>
  </layout>
  <rollingStyle value="Date"/>
  <file value="C:/Logs/Error_"/>
  <datePattern value="yyyy_MM_dd'.log'" />
  <appendToFile value="true"/>
  <staticLogFileName value="false"/>
</appender>

<!-- Loggers -->
<logger name="DefaultLogger">
  <appender-ref ref="GeneralTextLog"/>
  <level value="ALL"/>
</logger>

<logger name="ErrorLogger">
  <appender-ref ref="ErrorTextLog"/>
  <levelMin value="WARN"/>
  <levelMax value="FATAL"/>
</logger>

Despite the fact that i have only included %exception in the conversionPattern for the error log, the stacktrace appears in both logs. Does anyone know how i can stop this from happening?

尽管我只在错误日志的转换模式中包含了 %exception,但堆栈跟踪出现在两个日志中。有谁知道我怎样才能阻止这种情况发生?

回答by Stefan Egli

Configure the layout like this (GeneralTextLog Appender):

像这样配置布局(GeneralTextLog Appender):

<layout type="log4net.Layout.PatternLayout">
    <IgnoresException value="False" />
    ...

Setting IgnoresExceptionto false tells the appender that the layout will take care of the exception. Thus you can choose not to print the stack trace.

设置IgnoresException为 false 告诉 appender 布局将处理异常。因此,您可以选择不打印堆栈跟踪。

回答by Dave

From your configuration, it looks like your stack trace was included as part of your messageparameter.

从您的配置来看,您的堆栈跟踪似乎已包含在您的message参数中。

EDIT-- I should have been clearer. My guess is that when you called log4net's logging methods, your first parameter (which is message) contained the stack trace, but that's just a guess. It's also the only parameter in both of your appenders. If you don't have a parameter in your appender format, it shouldn't appear in both logs... unless of course your appender with the exception is the primary appender.

编辑- 我应该更清楚。我的猜测是,当您调用 log4net 的日志记录方法时,您的第一个参数(即message)包含堆栈跟踪,但这只是一个猜测。它也是两个 appender 中唯一的参数。如果您的 appender 格式中没有参数,则它不应出现在两个日志中……当然,除非您的 appender 是主要 appender,但例外情况除外。

Try this instead and see what happens:

试试这个,看看会发生什么:

<!-- Loggers -->
<root>
  <appender-ref ref="GeneralTextLog"/>
  <level value="ALL"/>
</root>

<logger name="ErrorLogger">
  <appender-ref ref="ErrorTextLog"/>
  <levelMin value="WARN"/>
  <levelMax value="FATAL"/>
</logger>