NLog 介紹及使用 - part3
這是小弟參加 twMVC 研討會(順便幫打一下廣告) 學到的配置 還有參考一些文章 如mrkt大大的部落格
先設置好 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">
<!--nlog debug start-->
<!--<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
throwExceptions="true" internalLogFile="c:\nlog.txt" internalLogLevel="Debug" autoReload="true">-->
<!--nlog debug end-->
<!--
See http://nlog-project.org/wiki/Configuration_file
for information on customizing logging rules and outputs.
-->
<targets>
<!-- add your targets here -->
<!--<target xsi:type="File" name="f" fileName="${basedir}/App_Data/logs/${shortdate}.log"
layout="${longdate} ${uppercase:${level}} ${message}" />-->
<target xsi:type="File" name="f" fileName="${basedir}/App_Data/logs/${shortdate}.log"
layout="${longdate} ${uppercase:${level}} ${loginuser} ${message} ${machinename} ${exception:format=type} ${callsite:className=true} ${logger} ${exception:stacktrace} ${exception:format=tostring}" />
<target xsi:type="Database" name="db" dbProvider="mssql" commandText="Insert into NLog_Record(time_stamp, level, host, url, type, source, logger, message, stacktrace, detail, loginuser) Values(@time_stamp, @level, @host, @url, @type, @source, @logger, @message, @stacktrace, @detail, @loginuser);" connectionString="Data Source=localhost;Initial Catalog=Log_DB;Persist Security Info=True;User ID=資料庫登入帳號;Password=資料庫登入密碼;">
<!--<parameter name="@time_stamp" layout="${longdate}" />-->
<parameter name="@time_stamp" layout="${date:format=yyyy\-MM\-dd HH\:mm\:ss.fff} " />
<parameter name="@level" layout="${level}" />
<parameter name="@host" layout="${machinename}" />
<parameter name="@url" layout="${aspnet-request:serverVariable=url}" />
<parameter name="@type" layout="${exception:format=type}" />
<parameter name="@source" layout="${callsite:className=true}" />
<parameter name="@logger" layout="${logger}" />
<parameter name="@message" layout="${message}" />
<parameter name="@stacktrace" layout="${exception:stacktrace}" />
<parameter name="@detail" layout="${exception:format=tostring}" />
<parameter name="@loginuser" layout="${loginuser}" />
</target>
<target xsi:type="Mail" name ="FatalMail"
smtpServer="smtp.gmail.com"
smtpPort="587"
smtpAuthentication="Basic"
smtpUsername="你要寄出的Email帳號"
smtpPassword="寄出Email的密碼"
enableSsl="true"
addNewLines="true"
from="寄出的Email"
to="收信的Email,收信的Email2"
subject="${machinename} 於 ${shortdate} ${time} Log級別:${level} 於 ${callsite:className=true}, 出現 ${exception:format=type}!"
header="========================================================================="
body="${newline}
發生時間:${longdate} ${newline}${newline}
Log等級:${level:uppercase=true} ${newline}${newline}
Logger:${logger} ${newline}${newline}
Source:${callsite:className=true} ${newline}${newline}
使用者:${loginuser} ${newline}${newline}
Exception類別:${exception:format=type} ${newline}${newline}
錯誤訊息:${message} ${newline}${newline}"
footer="========================================================================="
/>
<target bufferSize="5" name="ErrorMail" xsi:type="BufferingWrapper">
<target xsi:type="Mail"
smtpServer="smtp.gmail.com"
smtpPort="587"
smtpAuthentication="Basic"
smtpUsername="你要寄出的Email帳號"
smtpPassword="寄出Email的密碼"
enableSsl="true"
addNewLines="true"
from="寄出的Email"
to="收信的Email,收信的Email2"
subject="${machinename} 於 ${shortdate} ${time} Log級別:${level} 出現錯誤!"
header="========================================================================="
body="${newline}
發生時間:${longdate} ${newline}${newline}
Log等級:${level:uppercase=true} ${newline}${newline}
Logger:${logger} ${newline}${newline}
Source:${callsite:className=true} ${newline}${newline}
使用者:${loginuser} ${newline}${newline}
Exception類別:${exception:format=type} ${newline}${newline}
錯誤訊息:${message} ${newline}${newline}"
footer="========================================================================="
/>
</target>
</targets>
<rules>
<!-- add your logging rules here -->
<logger name="*" maxlevel="Info" writeTo="f" />
<logger name="*" minlevel="Warn" writeTo="db" />
<logger name="*" level="Fatal" writeTo="FatalMail" />
<logger name="*" level="Error" writeTo="ErrorMail" />
</rules>
</nlog>
1. 先來講解一下怎麼對nlog做debug
轉自NLog官網
Troubleshooting logging
Sometimes our application doesn’t write anything to the log files, even though we have supposedly configured logging properly. There can be many reasons for logs not being written. The most common problems are permissions issues, usually in an ASP.NET process, where the aspnet_wp.exe or w3wp.exe process may not have write access to the directory where we want to store logs.
NLog is designed to swallow run-time exceptions that may result from logging. The following settings can change this behavior and/or redirect these messages.
- <nlog throwExceptions="true" /> - adding the throwExceptions attribute in the config file causes NLog to stop masking exceptions and pass them to the calling application instead. This attribute is useful at deployment time to quickly locate any problems. It’s recommended to set throwExceptions to "false" as soon as the application is properly configured to run, so that any accidental logging problems won’t crash the application.
- <nlog internalLogFile="file.txt" /> - adding internalLogFile causes NLog to write its internal debugging messages to the specified file. This includes any exceptions that may be thrown during logging.
- <nlog internalLogLevel="Trace|Debug|Info|Warn|Error|Fatal" /> – determines the internal log level. The higher the level, the less verbose the internal log output.
- <nlog internalLogToConsole="false|true" /> – determines whether internal logging messages are sent to the console.
- <nlog internalLogToConsoleError="false|true" /> – determines whether internal logging messages are sent to the console error output (stderr).
2. target 設定
1). File
2). Database
3). Mail
4). Mail buffersize (集滿五個log送出一封信)
關於 layout 及 layout renders 請參考:
官網 Layouts_and_layout_renderers
3. rules 設定
關於 rules 請參考:
官網 rules
還有個重點就是自訂layout renders…這個就讓小弟暗扛起來了…((其實是不大會,敬請期待下一篇吧。