ASP. NET configure log4net to enable the write error log function

Source: Internet
Author: User

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="&quot;Logs_&quot;yyyyMMdd&quot;.txt&quot;" />      <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="&#13;&#10;----------------------header--------------------------&#13;&#10;" />        <param name="Footer" value="&#13;&#10;----------------------footer--------------------------&#13;&#10;" />      </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.

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.