[Assembly:log4net. Config.xmlconfigurator (configfile = "Log4net.config", configfileextension = "config", Watch = True)]
- 3. Add the Log4net.config configuration file:
<?xml version= "1.0" encoding= "Utf-8"?><configuration> <configSections> <section name= "Log4net" Type= "System.Configuration.IgnoreSectionHandler"/> </configSections> <log4net> <appender name= " Rollinglogfileappender "Type=" log4net. Appender.rollingfileappender "> <file value=" log\logfile.log "/> <appendtofile value=" true "/> <rollingstyle value= "Composite"/> <datepattern value= "yyyyMMdd"/> <maxsizerollbackups value= "10"/ > <maximumfilesize value= "1MB"/> <layout type= "log4net. Layout.patternlayout "> <conversionpattern value="%date [%thread]%-5level%logger [%PROPERTY{NDC}]-%message %newline "/> </layout> </appender> <root> <level value=" All "/> <appender -ref ref= "Rollinglogfileappender"/> </root> </log4net> <startup> <supportedruntime version = "v4.0" sku= ". Netframework,version=v4.5 "/> </startup></configuration>
- 4. Add a public log management class AppLog.cs
Using system;using system.collections.generic;using system.text;using log4net;using log4net. Config;using system.io;namespace log4net{///<summary>//Log Log object using the Log4net plugin///</summary> Pub LIC Static class Applog {private static ILog log; Static Applog () {Xmlconfigurator.configureandwatch (new FileInfo (AppDomain.CurrentDomain.SetupInformatio N.configurationfile)); Log = Logmanager.getlogger (typeof (Applog)); } public static void Debug (object message) {log. Debug (message); public static void debugformatted (string format, params object[] args) {log. Debugformat (format, args); } public static void Info (object message) {log. Info (message); public static void infoformatted (string format, params object[] args) {log. Infoformat (format, args); } public static void Warn (Object MessaGE) {log. Warn (message); public static void Warn (Object message, Exception Exception) {log. Warn (message, exception); public static void warnformatted (string format, params object[] args) {log. Warnformat (format, args); } public static void Error (object message) {log. Error (message); The public static void Error (Object message, Exception Exception) {log. Error (message, exception); public static void errorformatted (string format, params object[] args) {log. Errorformat (format, args); } public static void Fatal (object message) {log. Fatal (message); public static void Fatal (Object message, Exception Exception) {log. Fatal (message, exception); public static void fatalformatted (string format, params object[] args) { Log. Fatalformat (format, args); } }}
- 5. Use in any place where you want to write a log, for example:
Applog.info ("Info log"); Applog.error ("Error log");
- 6. Experience: Do not put the project name as log4net, or any number in the middle of the name, or the introduction of Log4net.dll will be directly error
- 7. When the log file is not created properly or log = Logmanager.getlogger (typeof (Applog)) When the Log object has a field value of False, right-click the log4net.config Select Property --Advanced change the value copied to the project to always copy
- This print log function does not seem to be implemented in Class 8 library projects.
WPF uses Log4net.dll library demo (reprint plus personal point of view)