NLog日誌外掛配置
阿新 • • 發佈:2020-11-05
1.NuGet中引用NLog外掛安裝
2、建立一個NLogHelper類例項化日誌函式
public class NLogHelper { private readonly Logger logger = LogManager.GetCurrentClassLogger(); public void Debug(object message) { logger.Debug(message); } public void Debug(object message, Exception exception) { logger.Debug(exception, message.ToString()); } public void Error(object message) { logger.Error(message); } public void Error(object message, Exception exception) { logger.Error(exception, message.ToString()); } public void Fatal(object message) { logger.Fatal(message); } public void Fatal(object message, Exception exception) { logger.Fatal(exception, message.ToString()); } public void Info(object message) { logger.Info(message); } public void Info(object message, Exception exception) { logger.Info(exception, message.ToString()); } public void Warn(object message) { logger.Warn(message); } public void Warn(object message, Exception exception) { logger.Warn(exception, message.ToString()); } }
3、建立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" xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd" autoReload="true" throwExceptions="false" internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log"> <!-- optional, add some variables https://github.com/nlog/NLog/wiki/Configuration-file#variables --> <variable name="myvar" value="myvalue"/> <!-- See https://github.com/nlog/nlog/wiki/Configuration-file for information on customizing logging rules and outputs. --> <targets> <!-- add your targets here See https://github.com/nlog/NLog/wiki/Targets for possible targets. See https://github.com/nlog/NLog/wiki/Layout-Renderers for the possible layout renderers. --> <!-- Write events to a file with the date in the filename. <target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log" layout="${longdate} ${uppercase:${level}} ${message}" /> --> <!-- info檔案配置 --> <target name="info" xsi:type="File" fileName="${basedir}/Logs/Info/${shortdate}/info.txt" maxArchiveFiles="30" layout="${longdate} | ${level:uppercase=false} | ${message} ${onexception:${exception:format=tostring} ${newline} ${stacktrace} ${newline}" /> <!-- error檔案配置 --> <target name="error" xsi:type="File" fileName="${basedir}/Logs/Error/${shortdate}/error.txt" maxArchiveFiles="30" layout="${longdate} | ${level:uppercase=false} | ${message} ${onexception:${exception:format=tostring} ${newline} ${stacktrace} ${newline}" /> <!-- nlog檔案配置 --> <target name="debug" xsi:type="File" fileName="${basedir}/Logs/debug/${shortdate}/debug.txt" maxArchiveFiles="30" layout="${longdate} | ${level:uppercase=false} | ${message} ${onexception:${exception:format=tostring} ${newline} ${stacktrace} ${newline}" /> </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" /> --> <!-- log檔案配置 --> <logger name="*" writeTo="DatabaseFile" /> <logger name="*" minlevel="Info" writeTo="info" /> <logger name="*" minlevel="Error" writeTo="error" /> <logger name="*" minlevel="Debug" writeTo="debug" /> <logger name="*" writeTo="console" /> </rules> </nlog>