First, download the log4net project on the official apche website to compile log4net. You can also download the compiled. dll file for reference directly. Download home address http://logging.apache.org/log4net/download_log4net.cgi
Log4net-1.2.11-bin-newkey.zip [compiled dll files]
Log4net-1.2.11-src.zip [This is the log4net project, download your own compilation to get the loge4net. dll]
Then we start configuring log4net in webconfig.
<?xml version="1.0"?><configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" /> </configSections> <log4net> <!-- OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL --> <!-- Set root logger level to ERROR and its appenders --> <root> <level value="ALL"/> <appender-ref ref="SysAppender"/> </root> <!-- Print only messages of level DEBUG or above in the packages --> <logger name="WebLogger"> <level value="ALL"/> </logger> <appender name="SysAppender" type="log4net.Appender.RollingFileAppender,log4net" > <param name="File" value="Logger/" /> <param name="AppendToFile" value="true" /> <param name="RollingStyle" value="Date" /> <param name="DatePattern" value=""Logs_"yyyyMMdd".txt"" /> <param name="StaticLogFileName" value="false" /> <layout type="log4net.Layout.PatternLayout,log4net"> <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" /> <param name="Header" value=" ----------------------header-------------------------- " /> <param name="Footer" value=" ----------------------footer-------------------------- " /> </layout> </appender> </log4net></configuration>
Step 3
Add a code to the AssemblyInfo. cs file. The path of AssemblyInfo. cs is located in the Properties directory.
The added code is as follows:
// Log component configuration
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Web.config", ConfigFileExtension = "config", Watch = true)]
Then we start to write a public class to encapsulate the method to call log4net.
Using System; using System. collections. generic; using System. web; using log4net; using System. configuration; namespace testWeb {// <summary> // Error Log // </summary> public class MyLog4NetInfo {private static readonly log4net. ILog log = log4net. logManager. getLogger ("WebLogger"); public MyLog4NetInfo () {} private static void SetConfig () {object o = ConfigurationManager. getSection ("log4net"); log4net. Config. XmlConfigurator. Configure (o as System. Xml. XmlElement);} public static void LogInfo (string Message) {if (! Log. IsInfoEnabled) SetConfig (); log. Info (Message);} public static void LogInfo (string Message, Exception ex) {if (! Log. IsInfoEnabled) SetConfig (); log. Info (Message, ex);} public static void ErrorInfo (string Message) {if (! Log. IsInfoEnabled) SetConfig (); log. Error (Message);} public static void DebugInfo (string Message) {if (! Log. IsInfoEnabled) SetConfig (); log. Debug (Message );}}}
5. Create a log4netTest. aspx test page.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="log4netTest.aspx.cs" Inherits="testWeb.log4netTest" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Using System; using System. collections. generic; using System. web; using System. web. UI; using System. web. UI. webControls; namespace testWeb {public partial class log4netTest: System. web. UI. page {protected void Page_Load (object sender, EventArgs e) {if (! IsPostBack) {}} protected void button#click (object sender, EventArgs e) {MyLog4NetInfo. logInfo ("Error Log test"); MyLog4NetInfo. logInfo ("Error Log test"); MyLog4NetInfo. debugInfo ("Error Log test ");}}}
The test result has been completed.
Thanks
Http://www.okajax.com/a/200912/log4Net.html
Http://www.cnblogs.com/chencidi/archive/2010/01/12/1645291.html
I am very helpful.