Table of Contents
Log4net configuration
web.config - add section:
<configSections> ... <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/> </configSections>
web.config - log4net configuration:
<log4net debug="true"> <appender name="FileAppender_Global" type="log4net.Appender.FileAppender"> <file value="./Logs/Global.log"/> <appendToFile value="true"/> <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/> <maximumFileSize value="2MB"/> <layout type="log4net.Layout.PatternLayout"> <ConversionPattern value="[%d] %-5p (%5rms) %-30.30c{1} %-25.25M - %m%n"/> </layout> </appender> <appender name="FileAppender_Error" type="log4net.Appender.FileAppender"> <file value="./Logs/Error.log"/> <appendToFile value="true"/> <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/> <maximumFileSize value="2MB"/> <layout type="log4net.Layout.PatternLayout"> <ConversionPattern value="[%d] %-5p (%5rms) %-30.30c{1} %-25.25M - %m%n"/> </layout> </appender> <logger name="Global"> <level value="ALL"/> <appender-ref ref="FileAppender_Global"/> </logger> <logger name="Error"> <level value="ALL"/> <appender-ref ref="FileAppender_Error"/> </logger> <root> <appender-ref ref="FileAppender_Global"/> <level value="ALL"/>" </root> </log4net>
Tell application where is config - AssemblyInfo.cs:
// log4net [assembly: log4net.Config.XmlConfigurator(ConfigFile = "Web.config", Watch = true)]
How use it in code:
public readonly ILog log_Global = LogManager.GetLogger("Global"); public readonly ILog log_Error = LogManager.GetLogger("Error"); ... log_Global.Info("Application started."); ... try { ... } catch (Exception ex) { log_Error.Fatal("Fatal Error when ..." , ex); }
Debuging
Allow internal debugging in appsettings
<appSettings> <add key="log4net.Internal.Debug" value="true"/> </appSettings>
Add listener <code>
<system.diagnostics> <trace autoflush="true"> <listeners> <add name="textWriterTraceListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="C:\temp\log4net.txt" /> </listeners> </trace> </system.diagnostics>
</code