asp.net mvc使用log4gNetz

來源:互聯網
上載者:User

標籤:XML   exce   rac   filename   catch   pre   tor   static   pattern   

1. 下載安裝log4gNet2. 將 \bin\net\4.0\release\log4net.dll 複製到你的項目中 。3. 將log4net.dll 添加引用到你的項目中。4. 添加如下內容到 assemblyinfo.cs。[assembly: log4net.Config.XmlConfigurator(ConfigFile = @"Config\Log4Net.config", Watch = true)]5.在項目中建立一個新的log4net的設定檔Log4Net.config。  
<?xml version="1.0" encoding="utf-8"?><configuration>  <configSections>    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />  </configSections>  <log4net>    <!--資料日誌-->    <appender name="InfoRollingFileAppender" type="log4net.Appender.RollingFileAppender">      <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />      <param name="File" value="D:\wwwroot\admin_citssh_com\logfiles\Info\" />      <param name="AppendToFile" value="true" />      <param name="rollingStyle" value="Date" />      <param name="datePattern" value="yyyy-MM-dd.‘Info.log‘" />      <param name="staticLogFileName" value="false" />      <layout type="log4net.Layout.PatternLayout">        <conversionPattern value="記錄時間:%date %n記錄層級:  %-5level %n類型:%logger  %n描述:%message %newline%newline" />      </layout>      <filter type="log4net.Filter.LevelRangeFilter">        <param name="LevelMin" value="INFO" />        <param name="LevelMax" value="INFO" />      </filter>    </appender>    <!--錯誤記錄檔-->    <appender name="ErrorRollingFileAppender" type="log4net.Appender.RollingFileAppender">      <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />      <param name="File" value="D:\wwwroot\admin_citssh_com\logfiles\Error\" />      <param name="AppendToFile" value="true" />      <param name="rollingStyle" value="Date" />      <param name="datePattern" value="yyyy-MM-dd.‘error.log‘" />      <param name="staticLogFileName" value="false" />      <layout type="log4net.Layout.PatternLayout">        <conversionPattern value="記錄時間:%date %n記錄層級:  %-5level %n類型:%logger  %n描述:%message %newline%newline" />      </layout>      <filter type="log4net.Filter.LevelRangeFilter">        <param name="LevelMin" value="ERROR" />        <param name="LevelMax" value="ERROR" />      </filter>    </appender>    <!--調試日誌-->    <appender name="DebugRollingFileAppender" type="log4net.Appender.RollingFileAppender">      <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />      <param name="File" value="D:\wwwroot\admin_citssh_com\logfiles\Debug\" />      <param name="AppendToFile" value="true" />      <param name="rollingStyle" value="Date" />      <param name="datePattern" value="yyyy-MM-dd.‘debug.log‘" />      <param name="staticLogFileName" value="false" />      <layout type="log4net.Layout.PatternLayout">        <conversionPattern value="記錄時間:%date %n記錄層級:  %-5level %n類型:%logger  %n描述:%message %newline%newline" />      </layout>      <filter type="log4net.Filter.LevelRangeFilter">        <param name="LevelMin" value="DEBUG" />        <param name="LevelMax" value="DEBUG" />      </filter>    </appender>    <root>      <level value="DEBUG" />      <!--檔案形式記錄日誌-->      <appender-ref ref="ErrorRollingFileAppender" />      <appender-ref ref="DebugRollingFileAppender" />      <appender-ref ref="InfoRollingFileAppender" />    </root>  </log4net></configuration>
View Code
6.在全域配置中添加如下代碼:log4net.Config.XmlConfigurator.Configure();

7.添加log操作類:
    public static class Log    {        public static void Debug(object message)        {            LogManager.GetLogger(GetCurrentMethodFullName()).Debug(message);        }        public static void Debug(object message, Exception ex)        {            LogManager.GetLogger(GetCurrentMethodFullName()).Debug(message, ex);        }        public static void Error(object message)        {            LogManager.GetLogger(GetCurrentMethodFullName()).Error(message);        }        public static void Error(object message, Exception exception)        {            LogManager.GetLogger(GetCurrentMethodFullName()).Error(message, exception);        }        private static string GetCurrentMethodFullName()        {            StackFrame frame;            string str;            string str1;            bool flag;            try            {                int num = 2;                StackTrace stackTrace = new StackTrace();                int length = stackTrace.GetFrames().Length;                do                {                    int num1 = num;                    num = num1 + 1;                    frame = stackTrace.GetFrame(num1);                    str = frame.GetMethod().DeclaringType.ToString();                    flag = (!str.EndsWith("Exception") ? false : num < length);                }                while (flag);                string name = frame.GetMethod().Name;                str1 = string.Concat(str, ".", name);            }            catch            {                str1 = null;            }            return str1;        }        public static void Info(object message)        {            LogManager.GetLogger(GetCurrentMethodFullName()).Info(message);        }        public static void Info(object message, Exception ex)        {            LogManager.GetLogger(GetCurrentMethodFullName()).Info(message, ex);        }        public static void Warn(object message)        {            LogManager.GetLogger(GetCurrentMethodFullName()).Warn(message);        }        public static void Warn(object message, Exception ex)        {            LogManager.GetLogger(GetCurrentMethodFullName()).Warn(message, ex);        }    }
View Code

 

asp.net mvc使用log4gNetz

聯繫我們

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