前言
log歷程記錄對於工程師來說是不可或缺的存在,平常開發階段還可以使用debug mode來除錯,但一旦系統上線之後,user碰到系統錯誤只會丟一句"有問題",但細節若user無法描述清楚、怎樣操作才造成的問題,在線索有限的情況下,log就是非常重要的東西了,把錯誤訊息、執行了那些程式、甚至是SQL語法都給記錄下來留存,絕對比user一句"有問題"能解決快上數十倍!
而nuget上有非常多處理log紀錄的套件,這邊我是使用NLog,網路上有相當多的資源,大家也可以查找看看。
安裝Nlog
管理nuget套件
搜尋Nlog並且安裝
專案的參考會加入nlog,並且會有nlog的config檔
到目前為止就安裝成功
前置準備
寫了一個BaseLog的類別主要處理log相關的工作,以logType列舉型別來區分寫入log類型,主要分為五大類型的log
-Trace
-Info
-Warn
-Debug
-Error
public class BaseLog
{
protected static readonly Logger logger = LogManager.GetCurrentClassLogger();
/// <summary>
/// 紀錄Log
/// </summary>
/// <param name="message"></param>
/// <param name="logType"></param>
public void SetLogger(string message, LogType logType)
{
switch (logType)
{
case LogType.Trace:
logger.Trace(message);
break;
case LogType.Debug:
logger.Debug(message);
break;
case LogType.Info:
logger.Info(message);
break;
case LogType.Warn:
logger.Warn(message);
break;
case LogType.Error:
logger.Error(message);
break;
default:
break;
}
}
/// <summary>
/// 撰寫追蹤LOG為主
/// </summary>
/// <param name="ex"></param>
public void WriteTraceLog(string msg)
{
SetLogger(msg, LogType.Trace);
}
/// <summary>
/// 撰寫SQLLOG為主
/// </summary>
public void WriteSqlLog(string msg)
{
SetLogger(msg, LogType.Debug);
}
/// <summary>
/// 撰寫錯誤LOG為主
/// </summary>
/// <param name="ex"></param>
public void WriteErrorLog(string ex)
{
SetLogger(ex.ToString(), LogType.Error);
}
nlog如下設定
<target xsi:type="File" name="ErrorLog" fileName="D://Logs/TEST/${shortdate}/ErrorLog.txt"
layout="${newline}[${uppercase:${level}}]${longdate}${newline}[${uppercase:${level}}]${message}"
archiveFileName="D://Logs/TEST/${shortdate}/ErrorLog_{###}.txt"
archiveAboveSize="5242880" maxArchiveFiles="100"/>
<target xsi:type="File" name="SqlLog" fileName="D://Logs/MOST_WST/${shortdate}/Sql.txt"
layout="[${uppercase:${level}}]${longdate}${newline}[${uppercase:${level}}]${message}"
archiveFileName="D://Logs/TEST/${shortdate}/Sql_{###}.txt"
archiveAboveSize="5242880" maxArchiveFiles="100"/>
<target xsi:type="File" name="InfoLog" fileName="D://Logs/TEST/${shortdate}/LoginInfoLog.txt"
layout="${newline}[${uppercase:${level}}]${longdate}${newline}[${uppercase:${level}}]${message}" />
<target xsi:type="File" name="WarnLog" fileName="D://Logs/TEST/${shortdate}/AdminLoginLog.txt"
layout="[${uppercase:${level}}]${longdate}${newline}[${uppercase:${level}}]${message}" />
<target xsi:type="File" name="ExecLog" fileName="D://Logs/TEST/${shortdate}/ExecProgramLog.txt"
layout="${newline}[${uppercase:${level}}]${longdate}${newline}[${uppercase:${level}}]${message}" />
</targets>
<rules>
<!-- add your logging rules here -->
<!--
Write all events with minimal level of Debug (So Debug, Info, Warn, Error and Fatal, but not Trace) to "f"
<logger name="*" minlevel="Debug" writeTo="f" />
-->
<logger name="*" level="Error" writeTo="ErrorLog" />
<logger name="*" level="Warn" writeTo="WarnLog" />
<logger name="*" level="Info" writeTo="InfoLog" />
<logger name="*" level="Debug" writeTo="SqlLog" />
<logger name="*" level="Trace" writeTo="ExecLog" />
</rules>
最後會產生文字檔如下,並以日期去區分