Contents
Download
* Down from Apache log4net download page.
– I downloaded log4net-1.2.13-bin-newkey.zip
* Unzip into a local directory
Use in Visual Studio Console App
Add log4net to Project References
* Right click your project > References folder and select Add Reference…
* Select Browse tab and then select log4net-1.2.13\bin\net\4.0\release\log4net.dll file
Add log4net Entry to AssemblyInfo.cs
* Open your project > Properties > AssemblyInfo.cs
* Add to the end of the file:
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
Add log4net Configuration to App.config
* Add App.config file for your project if not already exist:
– Right click your project folder and select Add > New Item… > Application Configuration File
* Add log4net configuration to App.config file. (For web application, add to project Web.config file)
– You can find Version and PublicKeyToken values from the log4net.xml file located in the same folder as log4net.dll file.
– You can find sample configurations from log4net manual configuration section:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net, Version=1.2.13.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> </configSections> <log4net> <!-- A1 is set to be a ConsoleAppender --> <appender name="A1" type="log4net.Appender.ConsoleAppender"> <!-- A1 uses PatternLayout --> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%-4timestamp [%thread] %-5level %logger %ndc - %message%newline" /> </layout> </appender> <!-- Set root logger level to DEBUG and its only appender to A1 --> <root> <level value="DEBUG" /> <appender-ref ref="A1" /> </root> </log4net> </configuration>
* A working example:
<?xml version="1.0"?> <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net, Version=1.2.13.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/> </configSections> <log4net> <appender name="Console" type="log4net.Appender.ConsoleAppender"> <layout type="log4net.Layout.PatternLayout"> <!-- Pattern to output the caller's file name and line number --> <conversionPattern value="%5level [%thread] (%file:%line) - %message%newline" /> </layout> </appender> <appender name="RollingFile" type="log4net.Appender.RollingFileAppender"> <file value="C:\\LOG\\my.log" /> <appendToFile value="true" /> <rollingStyle value="Size" /> <maximumFileSize value="100MB" /> <maxSizeRollBackups value="26" /> <staticLogFileName value="true" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date %level %thread %logger - %message%newline" /> </layout> </appender> <root> <level value="INFO" /> <appender-ref ref="Console" /> <appender-ref ref="RollingFile" /> </root> </log4net> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/> </startup> <runtime> <enforceFIPSPolicy enabled="false"/> </runtime> </configuration>
Use in Application
using Com.Foo; // Import log4net classes. using log4net; using log4net.Config; public class MyApp { // Define a static logger variable so that it references the // Logger instance named "MyApp". private static readonly ILog log = LogManager.GetLogger(typeof(MyApp)); static void Main(string[] args) { // Set up a simple configuration that logs on the console. BasicConfigurator.Configure(); log.Info("Entering application."); Bar bar = new Bar(); bar.DoIt(); log.Info("Exiting application."); } }
References
* log4net home
* Log4Net basics with a Console Application (c#)