自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

一篇關(guān)于NLog-ASP.NET Core 5入門

開(kāi)發(fā) 后端
如果刪除所有其他LoggingProviders(如控制臺(tái))并且僅使用NLog,則可能必須特別注意Hosting Lifetime Startup Messages。因?yàn)檫@可能導(dǎo)致托管環(huán)境(Visual Studio / Docker / Azure容器)看不到已啟動(dòng)的應(yīng)用程序。

[[402836]]

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

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中:

  1. <ItemGroup> 
  2.   <PackageReference Include="NLog.Web.AspNetCore" Version="4.9.3" /> 
  3.   <PackageReference Include="NLog" Version="4.7.6" /> 
  4. </ItemGroup> 

 

3、創(chuàng)建一個(gè)nlog.config文件。

在項(xiàng)目的根目錄中創(chuàng)建nlog.config(全部小寫(xiě))文件。

我們使用以下示例:

  1. <?xml version="1.0" encoding="utf-8" ?> 
  2. <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" 
  3.       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  4.       autoReload="true" 
  5.       internalLogLevel="Info" 
  6.       internalLogFile="c:\temp\internal-nlog.txt"
  7.  
  8.   <!-- enable asp.net core layout renderers --> 
  9.   <extensions> 
  10.     <add assembly="NLog.Web.AspNetCore"/> 
  11.   </extensions> 
  12.  
  13.   <!-- the targets to write to --> 
  14.   <targets> 
  15.     <!-- write logs to file  --> 
  16.     <target xsi:type="File" name="allfile" fileName="c:\temp\nlog-all-${shortdate}.log" 
  17.             layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" /> 
  18.  
  19.     <!-- another file log, only own logs. Uses some ASP.NET core renderers --> 
  20.     <target xsi:type="File" name="ownFile-web" fileName="c:\temp\nlog-own-${shortdate}.log" 
  21.             layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" /> 
  22.   </targets> 
  23.  
  24.   <!-- rules to map from logger name to target --> 
  25.   <rules> 
  26.     <!--All logs, including from Microsoft--> 
  27.     <logger name="*" minlevel="Trace" writeTo="allfile" /> 
  28.  
  29.     <!--Skip non-critical Microsoft logs and so log only own logs--> 
  30.     <logger name="Microsoft.*" maxlevel="Info" final="true" /> <!-- BlackHole without writeTo --> 
  31.     <logger name="System.Net.Http.*" maxlevel="Info" final="true" /> <!-- BlackHole without writeTo --> 
  32.  
  33.     <logger name="*" minlevel="Trace" writeTo="ownFile-web" /> 
  34.   </rules> 
  35. </nlog> 

 

 

 

請(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

  1. using Microsoft.AspNetCore.Hosting; 
  2. using Microsoft.Extensions.Hosting; 
  3. using Microsoft.Extensions.Logging; 
  4. using System; 
  5. using NLog.Web; 
  6.  
  7. namespace ASP.NET_Core_5_NLog_Example 
  8.     public class Program 
  9.     { 
  10.         public static void Main(string[] args) 
  11.         { 
  12.             var logger = NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger(); 
  13.             try 
  14.             { 
  15.                 logger.Debug("init main"); 
  16.                 CreateHostBuilder(args).Build().Run(); 
  17.             } 
  18.             catch (Exception exception) 
  19.             { 
  20.                 //NLog: catch setup errors 
  21.                 logger.Error(exception, "Stopped program because of exception"); 
  22.                 throw; 
  23.             } 
  24.             finally 
  25.             { 
  26.                 // Ensure to flush and stop internal timers/threads before application-exit (Avoid segmentation fault on Linux) 
  27.                 NLog.LogManager.Shutdown(); 
  28.             } 
  29.         } 
  30.  
  31.         public static IHostBuilder CreateHostBuilder(string[] args) => 
  32.             Host.CreateDefaultBuilder(args) 
  33.                 .ConfigureWebHostDefaults(webBuilder => 
  34.                 { 
  35.                     webBuilder.UseStartup<Startup>(); 
  36.                 }) 
  37.                 .ConfigureLogging(logging => 
  38.                 { 
  39.                     logging.ClearProviders(); 
  40.                     logging.SetMinimumLevel(LogLevel.Trace); 
  41.                 }) 
  42.                 .UseNLog();  // NLog: Setup NLog for Dependency injection 
  43.     } 

5、配置appsettings.json / appsettings.Development.json

中指定的日志記錄配置appsettings.json會(huì)覆蓋對(duì)的任何調(diào)用SetMinimumLevel。因此"Default":,請(qǐng)根據(jù)您的需要?jiǎng)h除或正確調(diào)整它。

  1.   "Logging": { 
  2.     "IncludeScopes"false
  3.     "LogLevel": { 
  4.       "Default""Trace"
  5.       "Microsoft""Warning"
  6.       "Microsoft.Hosting.Lifetime""Information" 
  7.     } 
  8.   }, 
  9.   "AllowedHosts""*" 

切記還要更新任何特定于環(huán)境的配置,以免引起任何意外。前任appsettings.Development.json

6、寫(xiě)日志

將ILogger注入您的控制器中:

  1. using Microsoft.Extensions.Logging; 
  2.  
  3. public class HomeController : Controller 
  4.     private readonly ILogger<HomeController> _logger; 
  5.  
  6.     public HomeController(ILogger<HomeController> logger) 
  7.     { 
  8.         _logger = logger; 
  9.         _logger.LogDebug(1, "NLog injected into HomeController"); 
  10.     } 
  11.  
  12.     public IActionResult Index() 
  13.     { 
  14.         _logger.LogInformation("Hello, this is the index!"); 
  15.         return View(); 
  16.     } 

7、示例輸出

啟動(dòng)ASP.NET Core網(wǎng)站時(shí),我們得到兩個(gè)文件:

  1. 2020-12-29 16:47:02.5291||DEBUG|ASP.NET_Core_5_NLog_Example.Program|init main |url: |action:  
  2. 2020-12-29 16:47:03.5943|1|DEBUG|ASP.NET_Core_5_NLog_Example.Controllers.HomeController|NLog injected into HomeController |url: https://localhost/|actionIndex 
  3. 2020-12-29 16:47:03.5943||INFO|ASP.NET_Core_5_NLog_Example.Controllers.HomeController|Hello, this is the index! |url: https://localhost/|actionIndex 
  1. 2020-12-29 16:47:02.5291||DEBUG|ASP.NET_Core_5_NLog_Example.Program|init main  
  2. 2020-12-29 16:47:03.5260||INFO|Microsoft.Hosting.Lifetime|Application started. Press Ctrl+C to shut down.  
  3. 2020-12-29 16:47:03.5260||INFO|Microsoft.Hosting.Lifetime|Hosting environment: Development  
  4. 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  
  5. 2020-12-29 16:47:03.5943|1|DEBUG|ASP.NET_Core_5_NLog_Example.Controllers.HomeController|NLog injected into HomeController  
  6. 2020-12-29 16:47:03.5943||INFO|ASP.NET_Core_5_NLog_Example.Controllers.HomeController|Hello, this is the index

 

責(zé)任編輯:武曉燕 來(lái)源: 后端Q
相關(guān)推薦

2021-07-12 10:36:36

Blazor組件入門

2021-01-04 05:44:54

框架日志

2023-04-20 08:00:00

ES搜索引擎MySQL

2021-09-15 19:05:16

數(shù)據(jù)開(kāi)源項(xiàng)目

2015-01-07 09:32:50

ASP.NET MVC路由

2021-03-18 07:33:54

PDF DinkToPdfC++

2022-07-06 07:57:37

Zookeeper分布式服務(wù)框架

2021-10-11 11:08:33

HDFS快照系統(tǒng)

2022-11-08 10:52:25

Flowable節(jié)點(diǎn)表單

2021-03-08 00:09:47

日志分布式管理

2024-11-04 08:54:30

2022-02-21 09:44:45

Git開(kāi)源分布式

2021-07-21 09:48:20

etcd-wal模塊解析數(shù)據(jù)庫(kù)

2021-01-28 08:55:48

Elasticsear數(shù)據(jù)庫(kù)數(shù)據(jù)存儲(chǔ)

2022-08-26 10:32:21

MongoDB數(shù)據(jù)庫(kù)

2024-12-04 08:17:49

日志框架NLog

2025-01-15 00:01:00

開(kāi)發(fā)應(yīng)用界面

2021-05-14 16:34:12

Semaphore原理

2024-06-11 09:00:00

異步編程代碼

2024-09-09 07:37:51

AspJWT權(quán)限
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)