Log4net is a .NET component from Apache Foundation, this provides easy mechanism for logging messages. Log4net provides the following seven levels of logging:
1. ALL
2. DEBUG
3. INFO
4. WARN
5. ERROR
6. FATAL
7. OFF
1) Add the Reference for log4net.dll in your project.
2) Add the following line to AssemblyInfo.cs
- If specified, this is the extension for the configuration file.
- The assembly file name is used as the base name with this extension appended.
- If the assembly is loaded from the a file TestApp.exe and the ConfigFileExtension property is set to log4net then the configuration file name is TestApp.exe.log4net. This is equivalent to setting the ConfigFile property to TestApp.exe.log4net.
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="InfoLogFileAppender" type="log4net.Appender.RollingFileAppender,log4net">
<param name="File" value="Logs\SampleApp_Debug" />
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Date" />
<param name="datePattern" value="_yyyyMMdd'.txt'" />
<param name="StaticLogFileName" value="false" />
<param name="Threshold" value="DEBUG" />
<layout type="log4net.Layout.PatternLayout,log4net">
<conversionPattern value="%date - [%thread] - %level - %message%newline" />
<footer value="
" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="DEBUG" />
<levelMax value="DEBUG" />
</filter>
<filter class="log4net.Filter.DenyAllFilter" />
</appender>
<appender name="ErrorLogFileAppender" type="log4net.Appender.RollingFileAppender,log4net">
<param name="File" value="Logs\ SampleApp _Error" />
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Date" />
<param name="datePattern" value="_yyyyMMdd'.txt'" />
<param name="StaticLogFileName" value="false" />
<param name="Threshold" value="ERROR" />
<layout type="log4net.Layout.PatternLayout,log4net">
<conversionPattern value="%date - [%thread] - %level - %message%newline" />
<footer value="
" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="WARN" />
<levelMax value="ERROR" />
</filter>
</appender>
<root>
<level value="debug" />
<appender-ref ref="InfoLogFileAppender" />
<appender-ref ref="ErrorLogFileAppender" />
</root>
</log4net>