1. 程式人生 > >asp.net core 日誌

asp.net core 日誌

警告 https -c extension art img tco emp ica

ASP.NET Core 中的日誌記錄
https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/logging/?view=aspnetcore-2.1

日誌級別:Trace -》Debug-》 Information -》Warning-》 Error-》 Critical
級別包含範圍由大到小 ,如 Trace 就包含了所有信息。

基礎用法

public class HomeController : Controller
    {
        private readonly ILogger _logger;

        public HomeController(ILogger<HomeController> logger)
        {
            _logger = logger;
        }
        public IActionResult Index()
        {
            _logger.LogInformation("你訪問了首頁");
            _logger.LogWarning("警告信息");
            _logger.LogError("錯誤信息");
            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);

結果:
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>

復制到輸出目錄:始終復制
技術分享圖片

Update program.cs
技術分享圖片

這裏 nlog-all-.log 是記錄所有日誌,nlog-own-.log 記錄跳過Microsoft 開頭的類庫輸出的相關信息,剩下的信息。

參考:https://www.cnblogs.com/linezero/p/Logging.html

asp.net core 日誌