ASP.NET Core使用NLog輸出日志記錄
ASP.NET Core 中的日志記錄
https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/logging/?view=aspnetcore-2.1
日志級(jí)別:Trace -》Debug-》 Information -》Warning-》 Error-》 Critical
級(jí)別包含范圍由大到小 ,如 Trace 就包含了所有信息。
基礎(chǔ)用法
public class HomeController : Controller
{
private readonly ILogger _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
public IActionResult Index()
{
_logger.LogInformation("你訪(fǎng)問(wèn)了首頁(yè)");
_logger.LogWarning("警告信息");
_logger.LogError("錯(cuò)誤信息");
return View();
}
}日志事件 ID
public class LoggingEvents
{
public const int GenerateItems = 1000;
public const int ListItems = 1001;
public const int GetItem = 1002;
public const int InsertItem = 1003;
public const int UpdateItem = 1004;
public const int DeleteItem = 1005;
public const int GetItemNotFound = 4000;
public const int UpdateItemNotFound = 4001;
} _logger.LogWarning(LoggingEvents.GetItemNotFound, "GetById({ID}) NOT FOUND", 100);結(jié)果:
warn: TodoApi.Controllers.TodoController[4000]
GetById(100) NOT FOUND
NLog 輸出到文件
https://github.com/NLog/NLog.web/wiki
asp.net core 2
https://github.com/NLog/NLog.Web/wiki/Getting-started-with-ASP.NET-Core-2
Create a nlog.config file.
<?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="info"
internalLogFile="c:\temp\internal-nlog.txt">
<!-- enable asp.net core layout renderers -->
<extensions>
<add assembly="NLog.Web.AspNetCore"/>
</extensions>
<!-- the targets to write to -->
<targets>
<!-- write logs to file -->
<target xsi:type="File" name="allfile" fileName="c:\temp\nlog-all-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" />
<!-- another file log, only own logs. Uses some ASP.NET core renderers -->
<target xsi:type="File" name="ownFile-web" fileName="c:\temp\nlog-own-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" />
</targets>
<!-- rules to map from logger name to target -->
<rules>
<!--All logs, including from Microsoft-->
<logger name="*" minlevel="Trace" writeTo="allfile" />
<!--Skip non-critical Microsoft logs and so log only own logs-->
<logger name="Microsoft.*" maxLevel="Info" final="true" /> <!-- BlackHole without writeTo -->
<logger name="*" minlevel="Trace" writeTo="ownFile-web" />
</rules>
</nlog>
復(fù)制到輸出目錄:始終復(fù)制

Update program.cs

這里 nlog-all-.log 是記錄所有日志,nlog-own-.log 記錄跳過(guò)Microsoft 開(kāi)頭的類(lèi)庫(kù)輸出的相關(guān)信息,剩下的信息。
參考:http://m.fzitv.net/article/142992.htm
到此這篇關(guān)于ASP.NET Core使用NLog輸出日志記錄的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
ASP.NET實(shí)現(xiàn)電影票信息的增刪查改功能
這篇文章主要介紹了ASP.NET實(shí)現(xiàn)電影票信息的增刪查改功能的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-12-12
.NET?Core?使用委托實(shí)現(xiàn)動(dòng)態(tài)流程組裝的思路詳解
模擬管道模型中間件(Middleware)部分,運(yùn)用委托,進(jìn)行動(dòng)態(tài)流程組裝,本次代碼實(shí)現(xiàn)就直接我之前寫(xiě)的動(dòng)態(tài)代理實(shí)現(xiàn)AOP的基礎(chǔ)上改的,就不另起爐灶了,主要思路就是運(yùn)用委托,具體實(shí)現(xiàn)過(guò)程跟隨小編一起看看吧2022-01-01
C# 生成驗(yàn)證碼取隨機(jī)數(shù)字加字母(改進(jìn)版)
生成驗(yàn)證碼取4位數(shù)字沒(méi)有問(wèn)題,結(jié)果加上字母效率非常低本人改進(jìn)了一下,感興趣的朋友可以參考下哈2013-03-03
CKEditor與dotnetcore實(shí)現(xiàn)圖片上傳功能
這篇文章主要為大家詳細(xì)介紹了CKEditor與dotnetcore實(shí)現(xiàn)圖片上傳功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09
.NET C#創(chuàng)建WebService服務(wù)簡(jiǎn)單實(shí)例
這篇文章主要為大家詳細(xì)介紹了.NET C# 創(chuàng)建WebService服務(wù)簡(jiǎn)單實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
ASP.NET(C#)中操作SQLite數(shù)據(jù)庫(kù)實(shí)例
最近項(xiàng)目中有使用到SQLite數(shù)據(jù)庫(kù),于是查找資料,編寫(xiě)了一個(gè)ASP.NET基于C#語(yǔ)言的SQLite數(shù)據(jù)庫(kù)操作實(shí)例.大家看代碼就可以看懂了,和以往使用ADO.NET操作SQL數(shù)據(jù)庫(kù)類(lèi)似.2009-12-12
EntityFramework 6.x學(xué)習(xí)之多個(gè)上下文遷移實(shí)現(xiàn)分布式事務(wù)詳解
這篇文章主要給大家介紹了關(guān)于EntityFramework 6.x學(xué)習(xí)之多個(gè)上下文遷移實(shí)現(xiàn)分布式事務(wù)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-10-10
彈出窗口,點(diǎn)擊確定在刪除數(shù)據(jù)的實(shí)現(xiàn)方法
彈出窗口,點(diǎn)擊確定在刪除數(shù)據(jù)的實(shí)現(xiàn)方法,需要的朋友可以參考一下2013-04-04
利用ASP.NET MVC+Bootstrap搭建個(gè)人博客之打造清新分頁(yè)Helper(三)
這篇文章主要介紹了利用ASP.NET MVC+Bootstrap搭建個(gè)人博客之打造清新分頁(yè)Helper(三)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06

