How to configure Log4Net for your application
28 March 2022
|
Viewed 2165 times
The Apache log4net library is a tool to help the programmer output log statements to a variety of output targets. In case of problems with an application, it is helpful to enable logging so that the problem can be located. With log4net it is possible to enable logging at runtime without modifying the application binary.
Follow below steps to configure and use Log4Net
1. Add log4net.dll to application references
2. Add configuration to web .cofig
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" requirePermission="false"/>
</configSections>
<log4net>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value=".\.\log\AppName-%property{LogName}.log" type="log4net.Util.PatternString"/>
<appendToFile value="true"/>
<rollingStyle value="Date"/>
<datePattern value="-yyyy-MM-dd"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p %C{1} - %m%n"/>
</layout>
</appender>
<root>
<level value="ALL"/>
<appender-ref ref="RollingLogFileAppender"/>
</root>
</log4net>
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(
System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
log4net.Config.XmlConfigurator.Configure();
log.Error(exp);
Hope this helps!
PreviousNext