Log4net using and configuring a log file per day

Source: Internet
Author: User
Tags log4net


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

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.