Step 1:
Add and apply Log4net. dll. Add the following configuration board to the Web. config file.
Copy codeThe Code is as follows: <configSections>
<Section name = "log4net" type = "log4net. Config. Log4NetConfigurationSectionHandler, log4net"/>
</ConfigSections>
Step 2:
Create a Log4Net. config configuration file and add the following configuration information:
Copy codeThe Code is as follows: <? 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 = http://www.cnblogs.com/huanghai223/archive/2012/02/21/ "true"/>
<! -- Minimum lock model to allow multiple processes to write to the same file -->
<Param name = "LockingModel" value = http://www.cnblogs.com/huanghai223/archive/2012/02/21/ "log4net. Appender. FileAppender. MinimalLock"/>
<Param name = "StaticLogFileName" value = http://www.cnblogs.com/huanghai223/archive/2012/02/21/ "true"/>
<! -- Save path -->
<Param name = "File" value = http://www.cnblogs.com/huanghai223/archive/2012/02/21/ "d: \ Log \"/>
<Param name = "DatePattern" value = http://www.cnblogs.com/huanghai223/archive/2012/02/21/ "yyyy-MM-dd.LOG"/>
<Param name = "StaticLogFileName" value = http://www.cnblogs.com/huanghai223/archive/2012/02/21/ "false"/>
<Param name = "RollingStyle" value = http://www.cnblogs.com/huanghai223/archive/2012/02/21/ "Date"/>
<Layout type = "log4net. Layout. PatternLayout">
<Param name = "ConversionPattern" value = http://www.cnblogs.com/huanghai223/archive/2012/02/21/ "Time: % d % n level: % level % n class name: % c % n file: % F % L line % n log Content: % m % n --------------------------------------------- % n "/>
</Layout>
</Appender>
<! -- Split a log file into 10 KB by log capacity -->
<Appender name = "LogFileAppenderBySize" type = "log4net. Appender. RollingFileAppender">
<! -- Whether to continue writing -->
<Param name = "AppendToFile" value = http://www.cnblogs.com/huanghai223/archive/2012/02/21/ "true"/>
<! -- Minimum lock model to allow multiple processes to write to the same file -->
<Param name = "LockingModel" value = http://www.cnblogs.com/huanghai223/archive/2012/02/21/ "log4net. Appender. FileAppender. MinimalLock"/>
<Param name = "StaticLogFileName" value = http://www.cnblogs.com/huanghai223/archive/2012/02/21/ "true"/>
<! -- Change the log file according to the file size -->
<Param name = "RollingStyle" value = http://www.cnblogs.com/huanghai223/archive/2012/02/21/ "Size"/>
<Param name = "File" value = http://www.cnblogs.com/huanghai223/archive/2012/02/21/ "log.txt"/>
<! -- The maximum number of files is valid only when files are separated by Size. -->
<Param name = "MaximumFileSize" value = http://www.cnblogs.com/huanghai223/archive/2012/02/21/ "200KB"/>
<! -- 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 = http://www.cnblogs.com/huanghai223/archive/2012/02/21/ "2"/>
<Param name = "StaticLogFileName" value = http://www.cnblogs.com/huanghai223/archive/2012/02/21/ "false"/>
<Layout type = "log4net. Layout. PatternLayout">
<Param name = "ConversionPattern" value = http://www.cnblogs.com/huanghai223/archive/2012/02/21/ "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>
<! -- Record logs to the database -->
<Appender name = "AdoNetAppender" type = "log4net. Appender. AdoNetAppender">
<BufferSize value = http://www.cnblogs.com/huanghai223/archive/2012/02/21/ "1"/>
<! -- Buffer size -->
<ConnectionType value = http://www.cnblogs.com/huanghai223/archive/2012/02/21/ "System. Data. SqlClient. SqlConnection, System. Data, Version = 1.0.3300.0, Culture = neutral, PublicKeyToken = b77a5c561934e089"/>
<ConnectionString value = http://www.cnblogs.com/huanghai223/archive/2012/02/21/ "Data Source =. \ SQLEXPRESS; Initial Catalog = CVDS; User ID = sa; Password = sasa"/>
<CommandText value = http://www.cnblogs.com/huanghai223/archive/2012/02/21/ "insert into Log ([Date], [Thread], [Level], [Logger], [Message], [Exception]) VALUES (@ log_date, @ thread, @ log_level, @ logger, @ message, @ exception) "/>
<Parameter>
<ParameterName value = http://www.cnblogs.com/huanghai223/archive/2012/02/21/ "@ log_date"/>
<DbType value = http://www.cnblogs.com/huanghai223/archive/2012/02/21/ "DateTime"/>
<Layout type = "log4net. Layout. RawTimeStampLayout"/>
</Parameter>
<Parameter>
<ParameterName value = http://www.cnblogs.com/huanghai223/archive/2012/02/21/ "@ thread"/>
<DbType value = http://www.cnblogs.com/huanghai223/archive/2012/02/21/ "String"/>
<Size value = http://www.cnblogs.com/huanghai223/archive/2012/02/21/ "255"/>
<Layout type = "log4net. Layout. PatternLayout">
<ConversionPattern value = http://www.cnblogs.com/huanghai223/archive/2012/02/21/ "% thread"/>
</Layout>
</Parameter>
<Parameter>
<ParameterName value = http://www.cnblogs.com/huanghai223/archive/2012/02/21/ "@ log_level"/>
<DbType value = http://www.cnblogs.com/huanghai223/archive/2012/02/21/ "String"/>
<Size value = http://www.cnblogs.com/huanghai223/archive/2012/02/21/ "50"/>
<Layout type = "log4net. Layout. PatternLayout">
<ConversionPattern value = http://www.cnblogs.com/huanghai223/archive/2012/02/21/ "% level"/>
</Layout>
</Parameter>
<Parameter>
<ParameterName value = http://www.cnblogs.com/huanghai223/archive/2012/02/21/ "@ logger"/>
<DbType value = http://www.cnblogs.com/huanghai223/archive/2012/02/21/ "String"/>
<Size value = http://www.cnblogs.com/huanghai223/archive/2012/02/21/ "255"/>
<Layout type = "log4net. Layout. PatternLayout">
<ConversionPattern value = http://www.cnblogs.com/huanghai223/archive/2012/02/21/ "% logger"/>
</Layout>
</Parameter>
<Parameter>
<ParameterName value = http://www.cnblogs.com/huanghai223/archive/2012/02/21/ "@ message"/>
<DbType value = http://www.cnblogs.com/huanghai223/archive/2012/02/21/ "String"/>
<Size value = http://www.cnblogs.com/huanghai223/archive/2012/02/21/ "4000"/>
<Layout type = "log4net. Layout. PatternLayout">
<ConversionPattern value = http://www.cnblogs.com/huanghai223/archive/2012/02/21/ "% message"/>
</Layout>
</Parameter>
<Parameter>
<ParameterName value = http://www.cnblogs.com/huanghai223/archive/2012/02/21/ "@ exception"/>
<DbType value = http://www.cnblogs.com/huanghai223/archive/2012/02/21/ "String"/>
<Size value = http://www.cnblogs.com/huanghai223/archive/2012/02/21/ "2000"/>
<Layout type = "log4net. Layout. ExceptionLayout"/>
</Parameter>
</Appender>
<Root>
<Level value = http://www.cnblogs.com/huanghai223/archive/2012/02/21/ "INFO"/>
<! -- Enable split by date -->
<Appender-ref = "LogFileAppenderByDate"/>
<! -- Enable split by capacity -->
<! -- <Appender-ref = "LogFileAppenderBySize"/> -->
<! -- Enable saving to database -->
<! -- <Appender-ref = "AdoNetAppender"/> -->
</Root>
</Log4net>
Step 3:
Add the following code to the Application_Start event in the Global. asax file:
Copy codeThe Code is as follows: protected void Application_Start (object sender, EventArgs e)
{
// Code that runs on application startup
Log4net. Config. XmlConfigurator. ConfigureAndWatch (new FileInfo (Server. MapPath ("Log4Net. config ")));
}
Step 4: Call LOG4net to write logs
Copy codeThe Code is as follows: protected void Button2_Click (object sender, EventArgs e)
{
ILog logs = LogManager. GetLogger (typeof (TEST ));
Logs. Fatal ("Excption: Here is the LOG information to be prompted ");
}
You can also write a LogHelper. cs class for encapsulation:
Copy codeThe Code is as follows: using System;
Using System. Collections. Generic;
Using System. Web;
Using log4net;
Namespace SBIT. Web. Class
{
/// <Summary>
/// Log auxiliary class
/// </Summary>
Public class LogHelper
{
Private static ILog log;
Private static LogHelper logHelper = null;
/// <Summary>
/// Initialization
/// </Summary>
/// <Returns> </returns>
Public static ILog GetInstance ()
{
LogHelper = new LogHelper (null );
Return log;
}
/// <Summary>
/// Initialization
/// </Summary>
/// <Param name = "configPath"> </param>
/// <Returns> </returns>
Public static ILog GetInstance (string configPath)
{
LogHelper = new LogHelper (configPath );
Return log;
}
/// <Summary>
/// Constructor
/// </Summary>
/// <Param name = "configPath"> </param>
Private LogHelper (string configPath)
{
If (! String. IsNullOrEmpty (configPath ))
{
Log = log4net. LogManager. GetLogger (System. Reflection. MethodBase. GetCurrentMethod (). DeclaringType );
Log4net. Config. XmlConfigurator. Configure (new System. IO. FileInfo (configPath ));
}
Else
{
Log = log4net. LogManager. GetLogger (System. Reflection. MethodBase. GetCurrentMethod (). DeclaringType );
}
}
}
}
The method for writing logs by encapsulating classes is as follows:
Copy codeThe Code is as follows: private static readonly ILog logs = LogHelper. GetInstance (); // LogManager. GetLogger (typeof (TEST ));
Protected void Button2_Click (object sender, EventArgs e)
{
Logs. Fatal ("Excption: Here is the LOG information to be prompted ");
}
Log4net: http://vzidc.jb51.net: 81/tools/201302/tools/log4net-1.2.11-src.zip