C # Use/configure Log4Net,

Source: Internet
Author: User

C # Use/configure Log4Net,

1. First add the Nuget package in the project...

2. Search for Log4Net in the NuGet form and click Install. <The installation process may take several minutes. Please wait.>

3. Add a Config file to the project. If App. config already exists, add the following content directly:

The XML Code configured in is as follows:

 

<Log4net> <logger name = "logerror"> <level value = "ERROR"/> <appender-ref = "ErrorAppender"/> </logger> <logger name =" loginfo "> <level value =" INFO "/> <appender-ref =" InfoAppender "/> </logger> <appender name =" ErrorAppender "type =" log4net. appender. rollingFileAppender "> <param name =" File "value =" Log \ LogError \ "/> <param name =" AppendToFile "value =" true "/> <param name = "MaxSizeRollBackups" valu E = "100"/> <param name = "MaxFileSize" value = "10240"/> <param name = "StaticLogFileName" value = "false"/> <param name =" datePattern "value =" yyyymmdd&quot=.txt & quot; "/> <param name =" RollingStyle "value =" Date "/> <layout type =" log4net. layout. patternLayout "> <param name =" ConversionPattern "value =" % n exception time: % d % n exception level: %-5 p % n exception content: % m % n "/> </layout> <! -- & Lt; & gt ;=<>% n = press enter --> </appender> <appender name = "InfoAppender" type = "log4net. appender. rollingFileAppender "> <param name =" File "value =" Log \ LogInfo \ "/> <param name =" AppendToFile "value =" true "/> <param name = "MaxFileSize" value = "10240"/> <param name = "MaxSizeRollBackups" value = "100"/> <param name = "StaticLogFileName" value = "false"/> <param name = "DatePattern" value = "yyyyMMdd&quot;.txt & quot; "/> <param name =" RollingStyle "value =" Date "/> <layout type =" log4net. layout. patternLayout "> <param name =" ConversionPattern "value =" log time: % d % n log level: %-5 p % n log Content: % m % n "/> </layout> </appender> </log4net>

 

Finally, register the Config file in the AssemblyInfo. cs file of the project, as shown below:

 

// Register the Log4Net. config configuration file for the project [assembly: log4net. Config. XmlConfigurator (ConfigFileExtension = "config", Watch = true)]

Finally, add the log class:

 

    public class LogHelper    {        private LogHelper()        {        }        public static readonly log4net.ILog loginfo = log4net.LogManager.GetLogger("loginfo");        public static readonly log4net.ILog logerror = log4net.LogManager.GetLogger("logerror");        public static void SetConfig()        {            log4net.Config.XmlConfigurator.Configure();        }        public static void SetConfig(FileInfo configFile)        {            log4net.Config.XmlConfigurator.Configure(configFile);        }        public static void WriteLog(string info)        {            if (loginfo.IsInfoEnabled)            {                loginfo.Info(info);            }        }        public static void WriteLog(string info, Exception se)        {            if (logerror.IsErrorEnabled)            {                logerror.Error(info, se);            }        }    }

 

Now, the preparation is complete. Let's test it.

 

By default, log files are recorded in the root directory. You can change the path in config.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.