Log4net 配置注意事項

來源:互聯網
上載者:User

標籤:

1. 首先引入Log4net程式集

2.修改webconfig設定檔

在 configuration 節點下面添加如下節點

<configSections><section name="log4net" type="System.Configuration.IgnoreSectionHandler"/></configSections><log4net><!--定義輸出到檔案中--><appender name="FileAppender" type="log4net.Appender.FileAppender"><!--定義檔案存放位置--><file type="log4net.Util.PatternString" value="Logs\%date{yyyMMddHH}.log"/><appendToFile value="true"/><!--日期格式--><lockingModel type="log4net.Appender.FileAppender+MinimalLock"/><layout type="log4net.Layout.PatternLayout"><!--輸出格式--><conversionPattern value="%date [%thread] %-5level %logger - %message%newline"/></layout></appender><root><!--檔案形式記錄日誌--><appender-ref ref="FileAppender"/></root></log4net> 

3.建立 Log4net 協助類 

注意需要在

//注意下面的語句一定要加上,指定log4net使用.config檔案來讀取配置資訊//如果是WinForm(假定程式為MyDemo.exe,則需要一個MyDemo.exe.config檔案)//如果是WebForm,則從web.config中讀取相關資訊[assembly: log4net.Config.XmlConfigurator(Watch = true)]namespace qqzssl.Util{    public class Log4NetExport    {        private static ILog _Ilog = null;        private Log4NetExport(Type t)        {            // 通常情況下,我們通過 LogManager.GetLogger() 來擷取一個記錄器。            // LogManager 內部維護一個 hashtable,儲存新建立 Logger 引用,下次需要時直接從 hashtable 擷取其執行個體。            _Ilog = log4net.LogManager.GetLogger(t);        }        public static Log4NetExport Create(Type t)        {            return new Log4NetExport(t);        }        public void Info(object message)        {            _Ilog.Info(message);        }        public void Info(object message, Exception ex)        {            _Ilog.Info(message, ex);        }        public void Error(object message)        {            _Ilog.Error(message);        }        public void Error(object message, Exception ex)        {            _Ilog.Error(message, ex);        }    }}

  

 

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.