|
Using Log4Net - Very quick start |
|
|
|
Friday, 18 August 2006 |
|
This is a very practical guide to start using log4net quickly. Log4net is the most common and standard tool to be used to log C# applications. This tool can write logs in log files, database log table or xml files. (There is some other ways to write logs too) - 1- Download log4net from http://logging.apache.org/log4net/
- 2- In Visual Studio->WebSite->AddReference add a new reference do log4net.dll that you downloaded.
- 3- Put this in the beginning of web.config file:
<configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/> </configSections> ...
- 4- Now put this in the end of web.config file:
<log4net> <logger name="default"> <level value="DEBUG"/> </logger> <root> <level value="WARN" /> <appender-ref ref="LogFileAppender" /> </root> <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" > <param name="File" value="log/log.txt" /> <param name="AppendToFile" value="true" /> <rollingStyle value="Size" /> <maxSizeRollBackups value="10" /> <maximumFileSize value="10MB" /> <staticLogFileName value="true" /> <layout type="log4net.Layout.PatternLayout"> <param name="ConversionPattern" value="%-5p %d{yyyy-MM-dd hh:mm:ss} - %m%n" /> </layout> </appender> </log4net> this configuration creates a log file in ~/log/log.txt. - 5- To use log4net put this as a local class variable:
protected static readonly ILog log = LogManager.GetLogger("default"); - 6- And do this to write messages in the log file.
log.Debug("this text will be in log file"); - 7- A very good program to read log files is called BareTail, you can find it easily.
- 8- This is an example class - Example.cs
- 9- This is all you need to start using log4net
|
|
Last Updated ( Wednesday, 15 November 2006 )
|