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?
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.