C#快速配置NLog日志的教程詳解
首先我們需要在Nuget中安裝Nlog和Nlog-Schema。

添加配置文件: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="d:\nlog\nlog-internal.log">
<!-- optional, add some variables
https://github.com/nlog/NLog/wiki/Configuration-file#variables
-->
<!--<variable name="myvar" value="myvalue"/>-->
<variable name="logDir" value="${basedir}/nlog"/>
<variable name="logFileName" value="${date:format=yyyyMMdd}.txt"/>
<variable name="logArchiveFileName" value="${date:format=yyyyMMdd}_{#}.txt"/>
<variable name="logLayout" value="${date:format=yyyy-MM-dd HH\:mm\:ss.fff} [${level}] ${message}"/>
<!--
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}" />
-->
<target xsi:type="File" name="info"
layout="${logLayout}"
fileName="${logDir}/info/${logFileName}"
archiveFileName="${logDir}/info/${logArchiveFileName}"
archiveAboveSize="10485760"
archiveNumbering="Sequence"
maxArchiveFiles="100"
concurrentWrites="true"
keepFileOpen="true"
openFileCacheTimeout="30"
encoding="UTF-8" />
<target xsi:type="File" name="debug"
layout="${logLayout}"
fileName="${logDir}/debug/${logFileName}"
archiveFileName="${logDir}/debug/${logArchiveFileName}"
archiveAboveSize="10485760"
archiveNumbering="Sequence"
maxArchiveFiles="100"
concurrentWrites="true"
keepFileOpen="true"
openFileCacheTimeout="30"
encoding="UTF-8" />
<target xsi:type="File" name="error"
layout="${logLayout}"
fileName="${logDir}/error/${logFileName}"
archiveFileName="${logDir}/error/${logArchiveFileName}"
archiveAboveSize="10485760"
archiveNumbering="Sequence"
maxArchiveFiles="100"
concurrentWrites="true"
keepFileOpen="true"
openFileCacheTimeout="30"
encoding="UTF-8" />
<target xsi:type="File" name="warn"
layout="${logLayout}"
fileName="${logDir}/warn/${logFileName}"
archiveFileName="${logDir}/warn/${logArchiveFileName}"
archiveAboveSize="10485760"
archiveNumbering="Sequence"
maxArchiveFiles="100"
concurrentWrites="true"
keepFileOpen="true"
openFileCacheTimeout="30"
encoding="UTF-8" />
</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="*" minlevel="Info" maxlevel="Info" writeTo="info" />
<logger name="*" minlevel="Debug" maxlevel="Debug" writeTo="debug" />
<logger name="*" minlevel="Error" maxlevel="Error" writeTo="error" />
<logger name="*" minlevel="Warn" maxlevel="Warn" writeTo="warn" />
</rules>
</nlog>
定義了5種log類型Debug,Info,Debug,Error,Warn,詳細(xì)配置可以自己定義。
定義一個(gè)常用的log類
public class NLogHelper
{
private static Logger _log = NLog.LogManager.GetCurrentClassLogger();
/// <summary>
/// Debug日志
/// </summary>
/// <param name="log"></param>
public static void Debug(string log)
{
_log.Debug(log);
}
/// <summary>
/// Error日志
/// </summary>
/// <param name="log"></param>
public static void Error(string log)
{
_log.Error(log);
}
/// <summary>
/// Warn日志
/// </summary>
/// <param name="log"></param>
public static void Warn(string log)
{
_log.Warn(log);
}
/// <summary>
/// Info日志
/// </summary>
/// <param name="log"></param>
public static void Info(string log)
{
_log.Info(log);
}
/// <summary>
/// 詳細(xì)異常日志
/// </summary>
/// <param name="ex"></param>
public static void Exception_Error(Exception ex)
{
try
{
if (ex != null)
{
StringBuilder strBuilder = new StringBuilder();
strBuilder.Append("【異常日志消息】");
strBuilder.AppendLine(ex.Message);
strBuilder.Append("【異常日志Trace】");
strBuilder.AppendLine(ex.StackTrace);
strBuilder.Append("【異常日志全部】");
strBuilder.Append(ex.ToString());
_log.Error(strBuilder.ToString());
}
}
catch (Exception)
{
}
}
}
調(diào)用代碼
NLogHelper.Info($"系統(tǒng)正在運(yùn)行中...");
到此這篇關(guān)于C#快速配置NLog日志的教程詳解的文章就介紹到這了,更多相關(guān)C#配置NLog日志內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#實(shí)現(xiàn)將一個(gè)字符串進(jìn)行翻轉(zhuǎn)顯示的6種方法
下面小編就為大家分享一篇C#實(shí)現(xiàn)將一個(gè)字符串進(jìn)行翻轉(zhuǎn)顯示的6種方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
C#中GraphicsPath的Widen方法用法實(shí)例
這篇文章主要介紹了C#中GraphicsPath的Widen方法用法,實(shí)例分析了Widen方法的使用技巧,需要的朋友可以參考下2015-06-06
C#?EF?Core可視化工具的使用及EF?Core入門語(yǔ)句操作代碼
EF?Core?可用作對(duì)象關(guān)系映射程序?(O/RM),以便于?.NET?開(kāi)發(fā)人員能夠使用?.NET?對(duì)象來(lái)處理數(shù)據(jù)庫(kù),這樣就不必經(jīng)常編寫大部分?jǐn)?shù)據(jù)訪問(wèn)代碼了,接下來(lái)通過(guò)本文給大家介紹C#?EF?Core可視化工具的使用及EF?Core入門語(yǔ)句,感興趣的朋友一起看看吧2022-02-02
C# 使用 Castle 實(shí)現(xiàn) AOP及如何用 Autofac 集成 Castle
這篇文章主要介紹了C# 使用 Castle 實(shí)現(xiàn) AOP及如何用 Autofac 集成 Castle,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-02-02
C#動(dòng)態(tài)生成PictureBox并指定圖片的方法
這篇文章主要介紹了C#動(dòng)態(tài)生成PictureBox并指定圖片的方法,實(shí)例分析了C#圖形控件的動(dòng)態(tài)生成及使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07
互斥量mutex的簡(jiǎn)單使用(實(shí)例講解)
本篇文章主要是對(duì)互斥量mutex的簡(jiǎn)單使用進(jìn)行了介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-01-01

