For developers who donβt know exactly how to get started, it may help
Configuration in app.config
Remember to tell your application that the library represents the custom configuration section that you are going to use, I'm not quite sure if this is required or not, but I always use it as the first section in the root <configuration> .
<configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> </configSections>
log4net config in app.config
There are many different applications in log4net, but I usually use RollingFileAppender, so I use the same one in this example, you can find the rest here .
<log4net> <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender"> <file value="C:/logs/my_log_file.log"/> <appendToFile value="true"/> <rollingStyle value="Date"/> <maxSizeRollBackups value="30"/> <datePattern value=".yyyy-MM-dd"/> <staticLogFileName value="true"/> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger - %message%newline"/> </layout> </appender> <root> <level value="DEBUG"/> <appender-ref ref="RollingFileAppender"/> </root> </log4net>
Update AssemblyInfo.cs file
I always skip this step when I need to create a new project. Therefore, remember that you must tell your application to monitor the XMLConfigurator to complete the log4net configuration, so the following line goes at the end of the AssemblyInfo.cs file:
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
Beginning of work
Remember to include the log4net.dll link, then use the following line of code to initialize the log in the class
private static ILog log = LogManager.GetLogger(typeof(MyClass));
And in the end allows you to use it, as after
log.Info("Hello log4net");
Happy magazine :)
Mubashar ahmad
source share