log4g使用《轉》

來源:互聯網
上載者:User
 
log4net:http://logging.apache.org/log4net/download.html
下載 incubating-log4net-1.2.10.zip
在webform開發環境中,log4net的配置必須放在網站根目錄下的web.config中,
添加以下配置
<configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
上邊的配置必須緊挨著<configuration>,否則會提示錯誤
"Error    49    D:\wwwroot\Default.aspx: ASP.NET runtime error: 每個設定檔中只允許存在一個 <configSections> 元素,並且,如果存在該元素,它還必須是根 <configuration> 元素的第一個子項目。 (D:\wwwroot\web.config line 18)    D:\wwwroot\Default.aspx    1    1    D:\wwwroot\
"

然後添加
<!--
log4net配置聲明
-->
<log4net>
    <appender name="LogFileAppender" type="log4net.Appender.FileAppender">
      <!-- Please make shure the ..\\Logs directory exists! -->
      <param name="File" value="Logs\\Log4Net.log"/>
      <!--<param name="AppendToFile" value="true"/>-->
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%d [%t] %-5p %c %m%n"/>
      </layout>
    </appender>
    <appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
      <to value="" />
      <from value="" />
      <subject value="" />
      <smtpHost value="" />
      <bufferSize value="512" />
      <lossy value="true" />
      <evaluator type="log4net.Core.LevelEvaluator">
        <threshold value="WARN"/>
      </evaluator>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%newline%date [%thread] %-5level %logger [%property] - %message%newline%newline%newline" />
      </layout>
    </appender>

    <logger name="File">
      <level value="All" />
      <appender-ref ref="LogFileAppender" />
    </logger>
    <logger name="EmailLog">
      <level value="All" />
      <appender-ref ref="SmtpAppender" />
    </logger>
</log4net>

需要注意的是,多數網上的教程都提到,需要在AssemblyInfo.cs檔案中,
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Web.config", Watch = true)]

按照要求做了可是寫不到檔案裡?怎麼回事?
後來在CSDN中找到了答案,大意就是說,VS2005(我使用的VS2008)中,不會產生固定名稱的DLL
不保證上行代碼總被執行,建議款封裝成靜態類

按此說法,經實驗表明是可取的!~以下為封裝好了的Log類,在保證上述要求後,記得添加DLL引用,
然後直接Copy下邊的代碼就可以了.

using System;
using System.Collections.Generic;
using System.Text;

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Web.config", Watch = true)]
namespace ChinaConstruct
{
    #region log類
    /// <summary>
    /// 公用類
    /// </summary>
    public class Log
    {
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger("File");

        #region Debug
        /// <summary>
        /// 調試
        /// </summary>
        /// <param name="message"></param>
        public static void Debug(object message)
        {
            log.Debug(message);
        }

        public static void Debug(object message, Exception exp)
        {
            log.Debug(message, exp);
        }

        public static void DebugFormat(string format, object arg0)
        {
            log.DebugFormat(format, arg0);
        }

        public static void DebugFormat(string format, params object[] args)
        {
            log.DebugFormat(format, args);
        }

        public static void DebugFormat(IFormatProvider provider, string format, params object[] args)
        {
            log.DebugFormat(provider, format, args);
        }

        public static void DebugFormat(string format, object arg0, object arg1)
        {
            log.DebugFormat(format, arg0, arg1);
        }

        public static void DebugFormat(string format, object arg0, object arg1, object arg2)
        {
            log.DebugFormat(format, arg0, arg1, arg2);
        }
        #endregion

        #region Error
        /// <summary>
        /// 錯誤
        /// </summary>
        /// <param name="message"></param>
        public static void Error(object message)
        {
            log.Error(message);
        }

        public static void Error(object message, Exception exception)
        {
            log.Error(message, exception);
        }

        public static void ErrorFormat(string format, object arg0)
        {
            log.ErrorFormat(format, arg0);
        }

        public static void ErrorFormat(string format, params object[] args)
        {
            log.ErrorFormat(format, args);
        }

        public static void ErrorFormat(IFormatProvider provider, string format, params object[] args)
        {
            log.ErrorFormat(provider, format, args);
        }

        public static void ErrorFormat(string format, object arg0, object arg1)
        {
            log.ErrorFormat(format, arg0, arg1);
        }

        public static void ErrorFormat(string format, object arg0, object arg1, object arg2)
        {
            log.ErrorFormat(format, arg0, arg1, arg2);
        }
        #endregion

        #region Fatal
        /// <summary>
        /// 致命的,毀滅性的
        /// </summary>
        /// <param name="message"></param>
        public static void Fatal(object message)
        {
            log.Fatal(message);
        }

        public static void Fatal(object message, Exception exception)
        {
            log.Fatal(message, exception);
        }

        public static void FatalFormat(string format, object arg0)
        {
            log.FatalFormat(format, arg0);
        }

        public static void FatalFormat(string format, params object[] args)
        {
            log.FatalFormat(format, args);
        }

        public static void FatalFormat(IFormatProvider provider, string format, params object[] args)
        {
            log.FatalFormat(provider, format, args);
        }

        public static void FatalFormat(string format, object arg0, object arg1)
        {
            log.FatalFormat(format, arg0, arg1);
        }

        public static void FatalFormat(string format, object arg0, object arg1, object arg2)
        {
            log.FatalFormat(format, arg0, arg1, arg2);
        }
        #endregion

        #region Info

        /// <summary>
        /// 資訊
        /// </summary>
        /// <param name="message"></param>
        public static void Info(object message)
        {
            log.Info(message);
        }

        public static void Info(object message, Exception exception)
        {
            log.Info(message, exception);
        }

        public static void InfoFormat(string format, object arg0)
        {
            log.InfoFormat(format, arg0);
        }

        public static void InfoFormat(string format, params object[] args)
        {
            log.InfoFormat(format, args);
        }

        public static void InfoFormat(IFormatProvider provider, string format, params object[] args)
        {
            log.InfoFormat(provider, format, args);
        }

        public static void InfoFormat(string format, object arg0, object arg1)
        {
            log.InfoFormat(format, arg0, arg1);
        }

        public static void InfoFormat(string format, object arg0, object arg1, object arg2)
        {
            log.InfoFormat(format, arg0, arg1, arg2);
        }
        #endregion

        #region Warn

        /// <summary>
        /// 警告,注意,通知
        /// </summary>
        /// <param name="message"></param>
        public static void Warn(object message)
        {
            log.Warn(message);
        }

        public static void Warn(object message, Exception exception)
        {
            log.Warn(message, exception);
        }

        public static void WarnFormat(string format, object arg0)
        {
            log.WarnFormat(format, arg0);
        }

        public static void WarnFormat(string format, params object[] args)
        {
            log.WarnFormat(format, args);
        }

        public static void WarnFormat(IFormatProvider provider, string format, params object[] args)
        {
            log.WarnFormat(provider, format, args);
        }

        public static void WarnFormat(string format, object arg0, object arg1)
        {
            log.WarnFormat(format, arg0, arg1);
        }

        public static void WarnFormat(string format, object arg0, object arg1, object arg2)
        {
            log.WarnFormat(format, arg0, arg1, arg2);
        }
        #endregion
    }
    #endregion
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.