本文转载自微信公众号「后端Q」,作者conan。转载本文请联系后端Q公众号。
为沙坡头等地区用户提供了全套网页设计制作服务,及沙坡头网站建设行业解决方案。主营业务为成都网站设计、成都做网站、沙坡头网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!
在Visual Studio 2019中。需要版本16.8+
安装最新版本:
在csproj中:
在项目的根目录中创建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}" />
请注意,如果删除所有其他LoggingProviders(如控制台)并且仅使用NLog,则可能必须特别注意Hosting Lifetime Startup Messages。因为这可能导致托管环境(Visual Studio / Docker / Azure容器)看不到已启动的应用程序。
更新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
- }
- }
中指定的日志记录配置appsettings.json会覆盖对的任何调用SetMinimumLevel。因此"Default":,请根据您的需要删除或正确调整它。
- {
- "Logging": {
- "IncludeScopes": false,
- "LogLevel": {
- "Default": "Trace",
- "Microsoft": "Warning",
- "Microsoft.Hosting.Lifetime": "Information"
- }
- },
- "AllowedHosts": "*"
- }
切记还要更新任何特定于环境的配置,以免引起任何意外。前任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();
- }
启动ASP.NET Core网站时,我们得到两个文件:
- 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!
当前标题:一篇关于NLog-ASP.NET Core 5入门
文章分享:http://www.shufengxianlan.com/qtweb/news27/62977.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联