Log4Net - Logging out the Exception stacktrace onl

2019-01-13 13:56发布

I currently have multiple log files in my application using 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);

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?

2条回答
何必那么认真
2楼-- · 2019-01-13 14:24

Configure the layout like this (GeneralTextLog Appender):

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

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

查看更多
贪生不怕死
3楼-- · 2019-01-13 14:29

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

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.

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>
查看更多
登录 后发表回答