Simple use of log4net and log4net
Use log4net to record system logs and exceptions... it is very convenient. You can define the file storage path, file size, content format, and so on. There are also a lot of instructions on the use of log4net on the Internet. You can go to http://logging.apache.org/log4net/to learn more about log4net, I will not understand. writing this article is just a note, so that you can copy it when using it)
Let's see how to use the trio.
- Configuration of the first play
I generally use log4net to record some user operations and exceptions, so I only use INFO and ERROR
1 <configSections> 2 <section name = "log4net" type = "log4net. config. log4NetConfigurationSectionHandler, log4net "/> 3 </configSections> 4 <log4net> 5 <root> 6 <level value =" All "/> 7 <appender-ref =" LogFileAppender "/> 8 <appender-ref = "ErrorFileAppender"/> 9 </root> 10 11 <appender name = "LogFileAppender" type = "log4net. appender. rollingFileAppender "LEVEL =" INFO "> 12 <param name =" File "value =" Logs \ info \ "/> 13 <param name =" AppendToFile "value =" true "/> 14 <param name = "MaxSizeRollBackups" value = "-1"/> 15 <param name = "MaximumFileSize" value = "10 MB"/> 16 <param name = "DatePattern "value =" yyyy-MM-dd.'.txt '"/> 17 <param name =" RollingStyle "value =" Date "/> 18 <param name =" StaticLogFileName "value =" false "/> 19 <layout type = "log4net. layout. patternLayout "> 20 <param name =" ConversionPattern "value =" record time: % date thread ID: [% thread] Log level: %-5 level class: % logger property: [% property {NDC}]-Description: % message % newline "/> 21 </layout> 22 <filter type =" log4net. filter. levelRangeFilter "> 23 <param name =" LevelMin "value =" INFO "/> 24 <param name =" LevelMax "value =" INFO "/> 25 </filter> 26 </appender> 27 <appender name = "ErrorFileAppender" type = "log4net. appender. rollingFileAppender "LEVEL =" ERROR "> 28 <param name =" File "value =" Logs \ error \ "/> 29 <param name =" AppendToFile "value =" true "/> 30 <param name = "MaxSizeRollBackups" value = "-1"/> 31 <param name = "MaximumFileSize" value = "10 MB"/> 32 <param name = "DatePattern "value =" yyyy-MM-dd.'.txt '"/> 33 <param name =" RollingStyle "value =" Date "/> 34 <param name =" StaticLogFileName "value =" false "/> 35 <layout type = "log4net. layout. patternLayout "> 36 <param name =" ConversionPattern "value =" record time: % date thread ID: [% thread] Log level: %-5 level class: % logger property: [% property {NDC}]-Description: % message % newline "/> 37 </layout> 38 <filter type =" log4net. filter. levelRangeFilter "> 39 <param name =" LevelMin "value =" ERROR "/> 40 <param name =" LevelMax "value =" ERROR "/> 41 </filter> 42 </appender> 43 </log4net>
1 [assembly: log4net.Config.XmlConfigurator(Watch = true)]
To save trouble, I created a log4net help class.
1 public sealed class Log4netHelper<T> where T : class 2 { 3 private static log4net.ILog logger; 4 private static readonly object padlock = new object(); 5 6 Log4netHelper() 7 { 8 9 }10 11 public static log4net.ILog Logger12 {13 get14 {15 if (logger == null)16 {17 lock (padlock)18 {19 if (logger == null)20 {21 logger = log4net.LogManager.GetLogger(typeof(T));22 }23 }24 }25 return logger;26 }27 }28 29 }
After the configuration is complete, you can call it now.
Please use the above Code with caution to avoid misleading. If any errors (Code habits, specifications, syntax, etc.) occur, I would like to listen to your suggestions and guidance.