Use of log4net

Source: Internet
Author: User

1. Add a configuration file Log4Net. config to the project.
<? Xml version = "1.0" encoding = "UTF-8"?>
<Log4net debug = "false">

<! -- Split a log file by date one day -->
<Appender name = "LogFileAppenderByDate" type = "log4net. Appender. RollingFileAppender">

<! -- Whether to continue writing -->
<Param name = "AppendToFile" value = "true"/>
<! -- Minimum lock model to allow multiple processes to write to the same file -->
<Param name = "LockingModel" value = "log4net. Appender. FileAppender. MinimalLock"/>
<Param name = "StaticLogFileName" value = "true"/>

<Param name = "File" value = "Log \"/>
<Param name = "DatePattern" value = "yyyy-MM-dd.LOG"/>
<Param name = "StaticLogFileName" value = "false"/>
<Param name = "RollingStyle" value = "Date"/>
<Layout type = "log4net. Layout. PatternLayout">
<Param name = "ConversionPattern" value = "occurrence time: % d % n event level: % level % n related Class name: % c % n program file: % F % L line % n log Content: % m % n --------------------------------------------- % n "/>
</Layout>
</Appender>
 
<! -- Split a log file by log capacity by kb -->
<Appender name = "LogFileAppenderBySize" type = "log4net. Appender. RollingFileAppender">

<! -- Whether to continue writing -->
<Param name = "AppendToFile" value = "true"/>
<! -- Minimum lock model to allow multiple processes to write to the same file -->
<Param name = "LockingModel" value = "log4net. Appender. FileAppender. MinimalLock"/>

<Param name = "StaticLogFileName" value = "true"/>

<! -- Change the log file according to the file size -->
<Param name = "RollingStyle" value = "Size"/>
<Param name = "File" value = "Log \ log.txt"/>
<! -- The maximum number of files is valid only when files are separated by Size. -->
<Param name = "MaximumFileSize" value = "5000KB"/>
<! -- If the number of retained log files exceeds this limit, it is valid only when the log files are separated by Size. -->
<Param name = "MaxSizeRollBackups" value = "500"/>

<Param name = "StaticLogFileName" value = "false"/>
<Layout type = "log4net. Layout. PatternLayout">
<Param name = "ConversionPattern" value = "occurrence time: % d % n event level: % level % n related Class name: % c % n program file: % F % L line % n log Content: % m % n --------------------------------------------- % n "/>
</Layout>
</Appender>
 
<Root>
<Level value = "INFO"/>
<! -- Enable split by date -->

<! -- <Appender-ref = "LogFileAppenderByDate"/>->
<! -- Enable split by capacity -->
<Appender-ref = "LogFileAppenderBySize"/>
</Root>
</Log4net>

 
2. Add in AssemblyInfo. cs
[Assembly: log4net. Config. DOMConfigurator (ConfigFile = "Log4Net. config", Watch = true)]
 
 
3. In Global. asax
Protected void Application_Start (object sender, EventArgs e)
{
// Code that runs when the application starts
// Configure log4net
Log4net. Config. DOMConfigurator. Configure ();
}

 
4. Call
Public static readonly ILog Log = LogManager. GetLogger (System. Reflection. MethodBase. GetCurrentMethod (). DeclaringType );

Log. Warn ("error! ", Ex );


 

From Viky

Related Article

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.