log4net和nlog應該是一般工程師比較常使用的log機制,這兩個log套件雖然類似,但各自有各自的優點,如何讓它們能夠輕鬆的轉換呢?那就要使用Common.Logging
Common Logging是一個Log的介面,支援log4net、nlog、enterprise library logging...等,可透過log設定的方式,輕鬆的轉換不同的log機制,詳細請看請點我
設定方式也十分簡單,首先打開NuGet
1. 先安裝Common.Logging
今天小艾打算要配合NLog來使用
2.安裝Common.Logging.NLogXX (XX是版本號)
加入NLog的,為了方便使用
3.安裝NLog.Configuration
安裝完後就會自動產生NLog.config檔了,接下來只要設定好config就完成囉
4.在web.config的<configuration></configuration>裡加入
<configSections>
<sectionGroup name="common">
<section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
</sectionGroup>
</configSections>
<common>
<logging>
<factoryAdapter type="Common.Logging.NLog.NLogLoggerFactoryAdapter, Common.Logging.NLog31">
<arg key="configType" value="FILE" />
<arg key="configFile" value="~/NLog.config" />
</factoryAdapter>
</logging>
</common>
其中的Common.Logging.NLog31的31記得要改成第2步驟安裝的版號,這樣web.config的設定就完成了,接下來只要設定好NLog.config就大功告成了
5. 設定NLog.config
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<!--
See https://github.com/nlog/nlog/wiki/Configuration-file
for information on customizing logging rules and outputs.
-->
<targets>
<target xsi:type="File" name="debug"
fileName="${basedir}/logs/debug/logfile.log"
layout="${longdate} ${uppercase:${level}} ${message}"
archiveEvery="Day"
archiveNumbering="Date"
maxArchiveFiles="30"
keepFileOpen="false"
archiveFileName="${basedir}/logs/debug/log.{#}.txt" />
<target xsi:type="File" name="info"
fileName="${basedir}/logs/info/logfile.log"
layout="${longdate} ${uppercase:${level}} ${message}"
archiveEvery="Day"
archiveNumbering="Date"
maxArchiveFiles="30"
keepFileOpen="false"
archiveFileName="${basedir}/logs/info/log.{#}.txt" />
<target xsi:type="File" name="error"
fileName="${basedir}/logs/error/logfile.log"
layout="${longdate} - ${onexception:inner=${exception:format=ToString}${newline}}"
archiveEvery="Day"
archiveNumbering="Date"
maxArchiveFiles="30"
keepFileOpen="false"
archiveFileName="${basedir}/logs/error/log.{#}.txt" />
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="debug" />
<logger name="*" level="Info" writeTo="info" />
<logger name="*" level="Error" writeTo="error" />
</rules>
</nlog>
target裡的詳細設定
fileName:設定log要放的位置
layout:log寫入的格式
archiveEvery="Day" : 每天產生一個log檔
archiveNumbering="Date" : 檔名的產生方式 {#} 就會以日期來替換
maxArchiveFiles="30" : 只保存30個檔案
archiveFileName : 自動產生出來檔案的檔名,照這裡的設定,當天的log檔名是logfile.log
前一天的是log.日期.txt,以此類推
rules裡的詳細設定
minlevel :只定要記到哪個層級的log,在nlog裡共有Trace, Debug, Info, Warn, Error, Fatal這幾個層級
witeTo :對應到target裡的name,設定記錄要記到哪個檔案
6.以上的設定就全部完成了,接下來只要在code裡寫上log就會成功將log寫進file裡了
private static readonly ILog Log = LogManager.GetCurrentClassLogger();
Log.Info("Application Start");
這樣就會在Info寫進Appliction Start的訊息了,只要把Info改成Debug, Error...就能夠偵對Log分層了,相當簡單實用,log寫的好,debug也會輕鬆許多~~
關於NLog的一些設定,可以參考這裡請點我
2015年1月8日 星期四
訂閱:
文章 (Atom)
[泰國] 芭達雅大城八日遊 Day7、8 - 瑪哈泰寺、Wat Ratchaburana、帕蒙空博碧寺、帕席桑碧寺、Hua Ro market、Chantharakasem National Museum、Amporn Department Store
第七天行程,早上吃飯店早餐,算不錯吃,只是有螞蟻,但吃完沒拉,所以應該還好 吃飽後飽嘟嘟車,一小時 200 泰銖,比自己叫還便宜,終於要來世界遺產之旅了 第一站:瑪哈泰寺,應該是大城最有名的景點了,入門票大人 120 ,小孩免費,其實不便宜 大城指標性景點...
-
遣入日本國境,深入了解日本主婦?日本主婦的一天,今天小艾要帶大家去看日本傳統家庭主婦的一天 ~~( 謎:為什麼不是日本正妹的一天或是日本高校生的一天呢?謎 2 :我就是喜歡介紹大家不介紹的 ~~) 首先呢,大約每天凌晨的三、四點,送報員就會送上報紙與一大疊的廣告傳單 ...
-
一、 貸款、房價 因為來到了日本工作,發現日本租屋實在貴的可以,買比租還要便宜,只是需要注意的是 ” 日本的房子賣掉基本上一定是賠錢 ” ,不會像台灣這樣可以抄房賺錢,所以購買錢要先想清楚這間房子要怎麼處理,是租人呢?或是賣掉,不同的決定,要選擇的房子也不同囉。 ...
-
沒錯,大阪…我又來了,這次是小艾我第三次到大阪了,這一次就要把之前沒去過的地方一次補完 ~~ 第一個瞬間馬上就想到了《 祭り 》,來了大阪這麼多趟竟然還沒有體驗過日本的祭典文化,剛好這次的時間趕上了具有 1000 多年歷史的天神祭號稱是《日本三大節慶活動》之一,聽起來夠厲害...