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