This is my log4net config file
For logging with diferent colors when showing on console depending on the message level (debug, info, error or fatal). The second appender is to save it on a file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
<log4net debug="false">
<appender name="trace" type="log4net.Appender.TraceAppender, log4net">
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
<appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender,log4net">
<mapping>
<level value="ERROR" />
<foreColor value="White" />
<backColor value="Red" />
</mapping>
<mapping>
<level value="DEBUG" />
<foreColor value="White" />
</mapping>
<mapping>
<level value="INFO" />
<foreColor value="Green" />
</mapping>
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender,log4net" >
<param name="File" value="Logs/NSCurrent.log" />
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Date" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<datePattern value="yyyy-MM-dd" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
<root>
<priority value="DEBUG" />
<appender-ref ref="RollingFile" />
<appender-ref ref="ColoredConsoleAppender" />
</root>
</log4net>
</configuration>
I run a pretty minimal log4net configuration and prefer to use the XML format, but one thing I'd suggest adding (for web apps) is the ASP.NET trace appender. You simply have to add the following:
<appender name="AspNetTraceAppender" type="log4net.Appender.AspNetTraceAppender" >
<layout type="log4net.Layout.PatternLayout" />
</appender>
Then down at your root logger, or wherever you decide to activate it:
<root>
<level value="WARN" />
<appender-ref ref="XmlSchemaFileAppender" />
<appender-ref ref="AspNetTraceAppender" />
</root>
This allows me to get all of my pertinent debug data together on a single page when I'm debugging some minute ASP.NET details. Most of the time I have the ASP.NET trace appender commented out to disable it, but has really helped out in a few instances, especially when you're trying to figure out what particular server post back method is causing the hangup in your app.
One suggestion for the RollingLogFileAppender is to use the minimal lockingModel. With this setting the log file isn't locked, rather it is opened and then closed for each write.
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
This may help a little, depending on how similar log4j and log4net are, I would think they are based on the same specification, but I don't know for sure.
Well I can't tell you what I have in my log4net config, but I can tell you what I have in my log4j config if that's good enough...
<?xml version="1.0" encoding="UTF-8"?>
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!--
<appender name="CHAINSAW_APPENDER"
class="org.apache.log4j.net.SocketAppender">
<param name="RemoteHost" value="localhost" />
<param name="Port" value="4445" />
<param name="LocationInfo" value="true"/>
<param name="ReconnectionDelay" value="10000"/>
</appender>
-->
<appender name="FILE_APPENDER"
class="org.apache.log4j.RollingFileAppender">
<param name="File" value="c:/dev/logs/OSB.log" />
<param name="Append" value="true" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p [[[[%c]]]] (((%F:%L))) ___%C___ - %m%n"/>
</layout>
</appender>
<logger name="org.apache">
<level value="WARN"/>
</logger>
<!--
<logger name="com.conciliarpress.webapp.filter.GZIPFilte">
<level value="WARN"/>
</logger>
<logger name="com.conciliarpress">
<level value="DEBUG"/>
</logger>
<logger name="BibleFormControllerTest">
<level value="DEBUG"/>
</logger>
<logger name="com.conciliarpress.webapp.action.sample.jmesa.JMesaSampleController">
<level value="DEBUG"/>
</logger>
<logger name="com.conciliarpress.webapp.action.binding">
<level value="DEBUG"/>
</logger>
<logger name="org.springframework.web.servlet.mvc">
<level value="DEBUG"/>
</logger>
<logger name="org.directwebremoting">
<level value="DEBUG"/>
</logger>
<logger name="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<level value="DEBUG"/>
</logger>
-->
<logger name="com.conciliarpress.dao.hibernate">
<level value="DEBUG"/>
</logger>
<logger name="com.conciliarpress.service.impl">
<level value="DEBUG"/>
</logger>
<logger name="com.conciliarpress.service.dwr">
<level value="DEBUG"/>
</logger>
<logger name="com.conciliarpress.webapp.action">
<level value="DEBUG" />
</logger>
<root>
<level value="WARN" />
<appender-ref ref="FILE_APPENDER"/>
</root>
</log4j:configuration>
Also I have a corresponding chainsaw configuration file:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration PUBLIC "-//log4j/log4j Configuration//EN" "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="true">
<plugin name="logFileReceiver" class="org.apache.log4j.varia.LogFilePatternReceiver">
<param name="fileURL" value="file:///c:/dev/logs/OSB.log" />
<param name="timestampFormat" value="yyyy-MM-d HH:mm:ss,SSS"/>
<param name="logFormat" value="TIMESTAMP [THREAD] LEVEL [[[[LOGGER]]]] (((FILE:LINE))) ___CLASS___ - MESSAGE"/>
<param name="name" value="DLA_M_LogFile" />
<param name="tailing" value="true" />
</plugin>
<root>
<level value="debug"/>
</root>
</log4j:configuration>
Also here is some information that may help you decifer the latter file: http://wiki.apache.org/logging-log4j/Configuring_a_log4j_RollingFileAppender_with_Chainsaw
To reduce log file size, you could try adjusting the ConversionPattern:
here's the format I use:
<layout type="log4net.Layout.PatternLayout,log4net">
<!-- a reduced format, saves bit of log space: -->
<param name="ConversionPattern" value="%date{MM/dd HH:mm:ss,fff}-{%level}-%message%newline"/>
</layout>
BEFORE:
2012-06-05 10:58:40,819 [1] INFO My App [(null)] - My App - ===star ting....
AFTER:
06/05 11:17:29,151-{INFO}-My App - ===starting...
note: the documentation from Apache seems to be incorrect for log4net - as it says to use SSS for milliseconds, whereas log4net uses fff
A small saving per line could make a big difference overall :)
Nobody mentioned encoding. It's pretty important if you are logging messages in Chinese, Japanese, Arabic or any double-byte language. Here is an example:
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
<encoding value="utf-8" />
<!-- etc. -->
</appender>