web項目中的 log4net的配置

來源:互聯網
上載者:User

標籤:

最近用log4net,網上查了很多資料,照著網上的配置大多都不管用,可能我還是有什麼地方配置的不對。看出來的朋友平指出。下面是我自己親測的,可以用!

1.web項目中的web.config 配置log4net

<?xml version="1.0" encoding="utf-8"?><!--  有關如何配置 ASP.NET 應用程式的詳細訊息,請訪問  http://go.microsoft.com/fwlink/?LinkId=169433  --><configuration>  <configSections>    <section name="log4net" type="System.Configuration.IgnoreSectionHandler"/>  </configSections>    <system.web>        <compilation debug="true" targetFramework="4.0" />    </system.web>  <log4net>    <!--定義輸出到檔案中-->    <appender name="LogFileAppender" type="log4net.Appender.FileAppender">      <!--定義檔案存放位置:相對路徑-->      <!--<file value="./log/log.txt" />-->      <!--定義檔案存放位置:絕對路徑-->      <!--<file value="e:/log/log.text" />-->      <!--定義檔案存放位置:相對路徑但是檔案名稱日期-->      <file type="log4net.Util.PatternString" value="./log/%date{yyyy-MM-dd}.log" />            <appendToFile value="true" />      <!--得到-->      <!--按照檔案的大小進行變換記錄檔-->      <!--<param name="RollingStyle" value="Size" />-->      <!--按照日期進行變換記錄檔 下面兩種配置都可以,作用是一樣的-->      <!--<param name="RollingStyle" value="Date" />-->      <rollingStyle value="Date" />      <datePattern value="yyyyMMdd-HH:mm:ss" />        <!--關掉固定檔案名稱-->      <!--<staticLogFileName value="false" />-->            <layout type="log4net.Layout.PatternLayout">        <!--每條日誌末尾的文字說明 這個地方有時候管用-->        <footer value="一條完整日誌" />        <!--輸出格式-->        <!--範例:2008-03-26 13:42:32,111 [10] INFO  loglog.WebForm1 [(null)] 檔案:E:\text\log4Net\loglog\loglog\WebForm1.aspx.cs 行號:41 - info-->        <conversionPattern value="%newline記錄時間:%date 線程ID:[%thread] 記錄層級:%-5level 出錯類:%logger property:[%property{NDC}] - 檔案:%F-- 行號:%L 錯誤描述:%message%newline" />      </layout>    </appender>    <!--定義日誌的輸出媒介,下面定義日誌以四種方式輸出。也可以下面的按照一種類型或其他類型輸出。-->    <root>      <!--檔案形式記錄日誌-->      <appender-ref ref="LogFileAppender" />         <!--Windows事件記錄-->      <!--<appender-ref ref="EventLogAppender" />-->      <!-- 如果不啟用相應的日誌記錄,可以通過這種方式注釋掉      <appender-ref ref="AdoNetAppender_Access" />      -->    </root>  </log4net></configuration>

 

 關於使用log4net中可能會使用到的一些參數
%m(message):輸出的日誌訊息,如ILog.Debug(…)輸出的一條訊息
%n(new line):換行
%d(datetime):輸出當前語句啟動並執行時刻
%r(run time):輸出程式從運行到執行到當前語句時消耗的毫秒數
%t(thread id):當前語句所在的線程ID
%p(priority): 日誌的當前優先順序別,即DEBUG、INFO、WARN…等
%c(class):當前日誌對象的名稱,例如:
%f(file):輸出語句所在的檔案名稱。
%l(line):輸出語句所在的行號。
%數字:表示該項的最小長度,如果不夠,則用空格填充,如“%-5level”表示level的最小寬度是5個字元,如果實際長度不夠5個字元則以空格填充

<!--每條日誌末尾的文字說明 這個地方有時候管用-->
        <footer value="一條完整日誌" />

有時候會有,有時候沒有。??

 

2.配置完以後在Global檔案中加點東西

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Security;using System.Web.SessionState;//方式1:這個可以用//[assembly: log4net.Config.DOMConfigurator(ConfigFile = "Web.config", Watch = true)] namespace loglog{    public class Global : System.Web.HttpApplication    {        protected void Application_Start(object sender, EventArgs e)        {            //方式2:網上有這樣寫的,我式了能用。有知道的請告訴我怎麼用。            //log4net.Config.XmlConfigurator.Configure();            //方式3:這個可以用            log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(System.Web.HttpContext.Current.Server.MapPath("web.config")));         }        protected void Session_Start(object sender, EventArgs e)        {        }        protected void Application_BeginRequest(object sender, EventArgs e)        {        }        protected void Application_AuthenticateRequest(object sender, EventArgs e)        {        }        protected void Application_Error(object sender, EventArgs e)        {        }        protected void Session_End(object sender, EventArgs e)        {        }        protected void Application_End(object sender, EventArgs e)        {        }    }}


上面的三種方式,第二種不管用,我看網上其他人有這樣寫的,不知道我這為什麼不能用!!

3. 在想加日誌的的地方加下面的代碼

            //建立日誌記錄組件執行個體            ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);            //記錄錯誤記錄檔            log.Error("error", new Exception("發生了一個異常"));            //記錄嚴重錯誤            log.Fatal("fatal", new Exception("發生了一個致命錯誤"));            //記錄一般資訊            log.Info("info");            //記錄調試資訊            log.Debug("debug");            //記錄警告資訊            log.Warn("warn");            //Console.WriteLine("日誌記錄完畢。");            //Console.Read();            System.Diagnostics.Debug.WriteLine("線程id:" + Thread.CurrentThread.ToString());

這樣值日就記錄在下來了。

 

web項目中的 log4net的配置

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.