views:

97

answers:

2

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?

A: 

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>
Dave
The stack trace is not contained in the first paramater 'message'. The message is a simple text string of "Error", with the exception passed in as the second parameter. Changing it to the config above made it so the log entry only appeared in one of the files (the generalTextLog), still with the stack trace.
Rob Newman
@Rob okay, thanks for letting me know. I haven't used "ALL" before in my configurations, only "DEBUG" or a specific range. The only thing I can guess is that ALL might be responsible, but there isn't any documentation for this parameter value. Try DEBUG instead. By the way, what method are you calling to do this logging? I am guessing it's logger.Debug() since it's not appearing in the ErrorLogger?
Dave
@Rob sorry, reading comprehension has failed me. I see you're using Error().
Dave
All is simply another Level that has a value of -2147483648.00 so all logging Levels are included.
Rob Newman
Yes im using log.Error(), and the log entry does appear in both logs, its just that its including the statck trace in both logs, despite my conversionPattern saying otherwise. Its like whenever you pass an Exception to the Log.Error() method, it will add the stacktrace into the message by default, so you cannot have the stacktrace output in one of the log files but not the other
Rob Newman
@Rob I see what you're saying. I'll have to grep all of my logs to see if any of them log the stack trace. I don't recall this being the case, but I could certainly be mistaken.
Dave
@Rob yikes, I see it in my file appender log... okay, I'll have to look into this more. At least I have a way of working on it on my end.
Dave
+1  A: 

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.

Stefan Egli
@Stefan This has worked. Thank you.
Rob Newman
@Stefan Thanks, you have saved me as well. :)
Dave