Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
675 views
in Technique[技术] by (71.8m points)

.net - Log4Net - Logging out the Exception stacktrace only for certain files

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...