在asp.net core中使用NLog

第一步:nuget  引入  NLog.Web.AspNetCore 4.5+web

 

第二步:放入nlog.configmvc

<?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"
autoReload="true"
internalLogLevel="Warn"
internalLogFile="c:\temp\internal-nlog.txt">

<!-- 加載ASP.NET Core插件 -->
<extensions>
<add assembly="NLog.Web.AspNetCore"/>
</extensions>

<!-- 輸出目的地 -->
<targets>
<!-- 輸出到文件,這個文件記錄全部日誌 -->
<target xsi:type="File" name="allfile" fileName="c:\temp\nlog-all-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}|${message} ${exception}" />

<!-- 另一個日誌記錄文件,戶口也跳過Microsoft開頭相關日誌信息 -->
<target xsi:type="File" name="ownFile-web" fileName="c:\temp\nlog-own-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}| ${message} ${exception}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" />

<!-- write to the void aka just remove -->
<target xsi:type="Null" name="blackhole" />
</targets>

<!-- 寫入目的地的規則 -->
<rules>
<!--所有記錄,包括Microsoft開頭的相關日誌信息-->
<logger name="*" minlevel="Trace" writeTo="allfile" />

<!--跳過Microsoft開頭的相關日誌信息-->
<logger name="Microsoft.*" minlevel="Trace" writeTo="blackhole" final="true" />
<logger name="*" minlevel="Trace" writeTo="ownFile-web" />
</rules>
</nlog>
第三步:寫代碼ui

NLog.Logger logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
public IActionResult Index()
{
logger.Info("普通訊息日誌-----------");
logger.Debug("調試日誌-----------");
//Logger.Error("錯誤日誌-----------");
//Logger.Fatal("異常日誌-----------");
//Logger.Warn("警告日誌-----------");
//Logger.Trace("跟蹤日誌-----------");
//Logger.Log(NLog.LogLevel.Warn, "Log日誌------------------");
return View();
}
第四步:查看日誌url

根據nlog配置的地址查看,很重要,我放在c盤了.net

 

總結:就是這麼簡單,輕鬆愉快的搞定,祝家族的程序猿(程序媛)們開心快樂!!!
————————————————
版權聲明:本文爲CSDN博主「劉聯其」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處連接及本聲明。
原文連接:https://blog.csdn.net/qqqqqqqqqq198968/article/details/81458657插件

相關文章
相關標籤/搜索