VS2012 C # Use/configure Log4net

Source: Internet
Author: User

Recently realized a truth, here to share to everyone: education represents your past, the ability to represent your present, study represents your future.

10 years Hedong 10 years hexi, MO bullying Juvenile poor

Lifelong Learning, Excellence

This section explores how to use log4net in VS2012

1. First add NuGet packages to your project ...

2. Then search for log4net in the NuGet form, then click Install < Install the process may last for a few minutes, please be patient >

3. Add a config file to the project and name it: Log4net.config

The XML code in the configuration is as follows:

<log4net> <logger name= "Logerror" > <level value= "ERROR"/> <appender-ref ref= "Errorappender"/ > </logger> <logger name= "Loginfo" > <level value= "INFO"/> <appender-ref ref= "Infoappender "/> </logger> <appender name=" Errorappender "type=" log4net. Appender.rollingfileappender "> <param name=" File "value=" log\\logerror\\ "/> <param name=" AppendToFile " Value= "true"/> <param name= "maxsizerollbackups" value= "10240"/> <param name= "MaxFileSize" value= /> <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=" &lt;hr color=red&gt;%n exception Time:%d [%t] &   Lt;br&gt;%n exception Level:%-5p & #xD;& #xA; &lt;br&gt;%n Exception class:%c [%x] &lt;br& gt;%n%m &lt;br&gt;%n &lt;hr size=1&gt; "/> </layout> </appender> <appender name=" I Nfoappender "Type=" log4net. Appender.rollingfileappender "> <param name=" File "value=" log\\loginfo\\ "/> <param name=" AppendToFile "V Alue= "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=" &lt;hr color=blue&gt;%n Log Time:%d [%t] &amp   ; lt;br&gt;%n log level:%-5p & #xD;& #xA; &lt;br&gt;%n Log class:%c [%x] &lt;br&gt;%n%m &lt;br&gt;%n &lt;hr size=1&gt; "/> </lay Out> </appender></log4net>

Finally, register the config file in the project's AssemblyInfo.cs file as follows:

Register the Log4net.config configuration file for the project [Assembly:log4net. Config.domconfigurator (configfile = "Log4net.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.DOMConfigurator.Configure ();        }        public static void Setconfig (FileInfo configfile)        {            log4net. Config.DOMConfigurator.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);}}}    

Okay, here we go, the preparation is done, let's start the test. < for simplicity, write a sentence directly in the log file >

First quote: Using Log4net;

And then:

    public partial class Index:System.Web.UI.Page    {        protected void Page_Load (object sender, EventArgs e)        {            loghelper.writelog ("Chen Wolong is a big villain");        }    }

Finally, we show all the files in the project and you will find a log folder, as follows:

We open the Loginfo folder and 20161220.txt will see the information we printed.

Of course, your code can also be written like this:

        protected void Page_Load (object sender, EventArgs e)        {            try            {                //todo            }            catch (Exception Ex)            {                Loghelper.writelog ("The divisor is zero, uh, uh, elementary math is not good!") ", ex);            }        }

When an exception occurs during the TODO process, the catch () segment is executed, and the log is written to the files in the Logerror folder.

Well, there are some usages of log4net, not for example here!

        protected void Page_Load (object sender, EventArgs e)        {            try            {                //todo                LogHelper.loginfo.Warn (" Warning message ");                LogHelper.logerror.Warn ("Error warning message");            }            catch (Exception ex)            {                Loghelper.writelog ("The divisor is zero, uh, uh, elementary math is not good!") ", ex);            }        }

Wait a minute!

VS2012 C # Use/configure Log4net

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.