1. Download or install log4net in NuGet
2. Web. config (app. config)
<configuration> <configSections> <section name= "log4net" type= "log4net. Config.log4netconfigurationsectionhandler, log4net "/> </configSections> <log4net> < Appender name= "Rollinglogfileappender" type= "log4net. Appender.rollingfileappender "> <file type=" log4net. Util.patternstring "Value=" C:\M1\EDASLOG\%DATE{DD. Mm.yyyy}.txt "/> <appendtofile value=" true "/> <rollingstyle value=" Date "/> < Datepattern value= "YyyyMMdd"/> <layout type= "log4net. Layout.patternlayout "> <conversionpattern value=" [[%date]]-%message%newline "/> </layout ></appender><root><level value= "DEBUG"/><appender-ref ref= "Rollinglogfileappender"/> </root> </log4net></configuration>
For a log file every day. To do this configuration (the file node type is patternstring):
<file type= "log4net. Util.patternstring "Value=" C:\M1\EDASLOG\%DATE{DD. Mm.yyyy}.txt "/>
3. Apply the configuration. In Application_Start (for Web Apps), configure ():
Log4net. Config.XmlConfigurator.Configure ();
4. Do a simple wrapper to use:
public static class Edaslogger { private static log4net. ILog Log {get; set;} Static Edaslogger () { Log = log4net. Logmanager.getlogger (typeof (Logger)); } public static void Error (Object msg) { log.error (msg); } public static void Error (Object msg, Exception ex) { log.error (msg, ex); } public static void Error (Exception ex) { log.error (ex). Message, ex); } public static void Info (Object msg) { log.info (msg); } }
Log4net using and configuring a log file per day