新聞中心
本文轉(zhuǎn)載自微信公眾號(hào)「后端Q」,作者conan。轉(zhuǎn)載本文請(qǐng)聯(lián)系后端Q公眾號(hào)。

為沙坡頭等地區(qū)用戶提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及沙坡頭網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為成都網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站、沙坡頭網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!
1、創(chuàng)建一個(gè)新的ASP.NET Core項(xiàng)目
在Visual Studio 2019中。需要版本16.8+
2、手動(dòng)或使用NuGet在csproj中添加依賴項(xiàng)
安裝最新版本:
- NLog.Web.AspNetCore 4.9+
- 如有可能,更新NLog軟件包
在csproj中:
3、創(chuàng)建一個(gè)nlog.config文件。
在項(xiàng)目的根目錄中創(chuàng)建nlog.config(全部小寫)文件。
我們使用以下示例:
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- autoReload="true"
- internalLogLevel="Info"
- internalLogFile="c:\temp\internal-nlog.txt">
- layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" />
- layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" />
請(qǐng)注意,如果刪除所有其他LoggingProviders(如控制臺(tái))并且僅使用NLog,則可能必須特別注意Hosting Lifetime Startup Messages。因?yàn)檫@可能導(dǎo)致托管環(huán)境(Visual Studio / Docker / Azure容器)看不到已啟動(dòng)的應(yīng)用程序。
4、更新program.cs
更新program.cs
- using Microsoft.AspNetCore.Hosting;
- using Microsoft.Extensions.Hosting;
- using Microsoft.Extensions.Logging;
- using System;
- using NLog.Web;
- namespace ASP.NET_Core_5_NLog_Example
- {
- public class Program
- {
- public static void Main(string[] args)
- {
- var logger = NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
- try
- {
- logger.Debug("init main");
- CreateHostBuilder(args).Build().Run();
- }
- catch (Exception exception)
- {
- //NLog: catch setup errors
- logger.Error(exception, "Stopped program because of exception");
- throw;
- }
- finally
- {
- // Ensure to flush and stop internal timers/threads before application-exit (Avoid segmentation fault on Linux)
- NLog.LogManager.Shutdown();
- }
- }
- public static IHostBuilder CreateHostBuilder(string[] args) =>
- Host.CreateDefaultBuilder(args)
- .ConfigureWebHostDefaults(webBuilder =>
- {
- webBuilder.UseStartup
(); - })
- .ConfigureLogging(logging =>
- {
- logging.ClearProviders();
- logging.SetMinimumLevel(LogLevel.Trace);
- })
- .UseNLog(); // NLog: Setup NLog for Dependency injection
- }
- }
5、配置appsettings.json / appsettings.Development.json
中指定的日志記錄配置appsettings.json會(huì)覆蓋對(duì)的任何調(diào)用SetMinimumLevel。因此"Default":,請(qǐng)根據(jù)您的需要?jiǎng)h除或正確調(diào)整它。
- {
- "Logging": {
- "IncludeScopes": false,
- "LogLevel": {
- "Default": "Trace",
- "Microsoft": "Warning",
- "Microsoft.Hosting.Lifetime": "Information"
- }
- },
- "AllowedHosts": "*"
- }
切記還要更新任何特定于環(huán)境的配置,以免引起任何意外。前任appsettings.Development.json
6、寫日志
將ILogger注入您的控制器中:
- using Microsoft.Extensions.Logging;
- public class HomeController : Controller
- {
- private readonly ILogger
_logger; - public HomeController(ILogger
logger) - {
- _logger = logger;
- _logger.LogDebug(1, "NLog injected into HomeController");
- }
- public IActionResult Index()
- {
- _logger.LogInformation("Hello, this is the index!");
- return View();
- }
7、示例輸出
啟動(dòng)ASP.NET Core網(wǎng)站時(shí),我們得到兩個(gè)文件:
- 2020-12-29 16:47:02.5291||DEBUG|ASP.NET_Core_5_NLog_Example.Program|init main |url: |action:
- 2020-12-29 16:47:03.5943|1|DEBUG|ASP.NET_Core_5_NLog_Example.Controllers.HomeController|NLog injected into HomeController |url: https://localhost/|action: Index
- 2020-12-29 16:47:03.5943||INFO|ASP.NET_Core_5_NLog_Example.Controllers.HomeController|Hello, this is the index! |url: https://localhost/|action: Index
- 2020-12-29 16:47:02.5291||DEBUG|ASP.NET_Core_5_NLog_Example.Program|init main
- 2020-12-29 16:47:03.5260||INFO|Microsoft.Hosting.Lifetime|Application started. Press Ctrl+C to shut down.
- 2020-12-29 16:47:03.5260||INFO|Microsoft.Hosting.Lifetime|Hosting environment: Development
- 2020-12-29 16:47:03.5260||INFO|Microsoft.Hosting.Lifetime|Content root path: D:\nlog\NLog.Web\examples\ASP.NET Core 5\ASP.NET Core 5 NLog Example
- 2020-12-29 16:47:03.5943|1|DEBUG|ASP.NET_Core_5_NLog_Example.Controllers.HomeController|NLog injected into HomeController
- 2020-12-29 16:47:03.5943||INFO|ASP.NET_Core_5_NLog_Example.Controllers.HomeController|Hello, this is the index!
文章標(biāo)題:一篇關(guān)于NLog-ASP.NET Core 5入門
文章起源:http://fisionsoft.com.cn/article/cdooocs.html


咨詢
建站咨詢
