合作联系微信: w6668263      合作联系电话:177-9238-7426     

ASP.NET 配置log4net启用写错误日志功能

首先我们到apche的官网下载log4net的项目编译得到log4net也可以下载编译好的.dll文件直接引用。下载主页地址http://logging.apache.org/log4net/download_log4net.cgi

log4net-1.2.11-bin-newkey.zip[编译好的dll文件]

log4net-1.2.11-src.zip[这是log4net项目,下载自己编译就得到了loge4net.dll]

然后我们在webconfig里面开始配置log4net

<?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>

第三步

在文件AssemblyInfo.cs加入一句代码,AssemblyInfo.cs的路径所在Properties目录下

加入的代码如下:

// 日志组件配置

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Web.config", ConfigFileExtension = "config", Watch = true)]

然后我们开始写一个公共类来封装方法来调用log4net

using System;
using System.Collections.Generic;
using System.Web;
using log4net;
using System.Configuration;


namespace testWeb
{

    /// <summary>
    ///错误记录日志 
    /// </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创建一个测试aspx页面log4netTest.aspx

<%@ 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">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
</head>
<body>
    <form ID="_aspNetForm" runat="server">

    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />

    </form>
</body>
</html>
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 Button1_Click(object sender, EventArgs e)
        {
            MyLog4NetInfo.LogInfo("错误日志test");
            MyLog4NetInfo.LogInfo("错误日志test");
            MyLog4NetInfo.DebugInfo("错误日志test");
        }


    }
}

测试结果已经完毕

 

 

感谢

http://www.okajax.com/a/200912/log4Net.html

http://www.cnblogs.com/chencidi/archive/2010/01/12/1645291.html

给予我很大的帮助。

如果这篇文章对您有帮助,您可以打赏我

 

posted on 2013-05-21 17:55  草率的龙果果  阅读(7831)  评论(1编辑  收藏  举报

导航