ASP.NET Core之NLog使用

原创 Laughing  2019-02-26 16:38  阅读 59 次 评论 0 条

添加NLog插件

修改NLog配置文件

1、可以通过Nuget安装,NLogConfigure
2、手工添加NLog.Configure文件,如下
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Diagnostics;  
  4. using System.Linq;  
  5. using System.Threading.Tasks;  
  6. using Microsoft.AspNetCore.Mvc;  
  7. using Microsoft.Extensions.Logging;  
  8. using NLog;  
  9. using Web.Models;  
  10.   
  11. namespace Web.Controllers  
  12. {  
  13.     public class HomeController : Controller  
  14.     {  
  15.         private readonly ILogger<HomeController> _log;  
  16.   
  17.         public HomeController(ILogger<HomeController> log) => _log = log;  
  18.         static Logger logger = LogManager.GetCurrentClassLogger();  
  19.   
  20.         public IActionResult Index()  
  21.         {  
  22.             logger.Info("测试日志乱码");  
  23.             return View();  
  24.         }  
  25.   
  26.         public IActionResult Privacy()  
  27.         {  
  28.             return View();  
  29.         }  
  30.   
  31.         [ResponseCache(Duration = 0Location = ResponseCacheLocation.None, NoStore = true)]  
  32.         public IActionResult Error()  
  33.         {  
  34.             return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });  
  35.         }  
  36.     }  
  37. }  

修改StartUp.cs文件

  1. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)  
  2.         {  
  3.             app.UseRequestIP();  
  4.             loggerFactory.AddNLog();  
  5.             env.ConfigureNLog("NLog.configure");  
  6.             app.Use(async (context, next) =>  
  7.             {  
  8.                 context.Items["IsVerified"] = true;  
  9.                 await next.Invoke();  
  10.             });  
  11.   
  12.             if (env.IsDevelopment())  
  13.             {  
  14.                 app.UseDeveloperExceptionPage();  
  15.                 app.UseDatabaseErrorPage();  
  16.             }  
  17.             else  
  18.             {  
  19.                 app.UseExceptionHandler("/Error");  
  20.             }  
  21.             app.UseStaticFiles();  
  22.             //app.UseDirectoryBrowser();  
  23.             app.UseAuthentication();  
  24.             app.UseSession();  
  25.             app.UseMvc(routes =>  
  26.                                     {  
  27.                                         routes.MapRoute(  
  28.                                             name:"area",  
  29.                                             template: "{area:exists}/{controller=Home}/{action=Index}/{id?}"  
  30.                                             );  
  31.                                         routes.MapRoute(  
  32.                                             name: "default",  
  33.                                             template: "{controller=Home}/{action=Index}/{id?}");  
  34.                                     });  
  35.         }  

使用NLog

  1. public class HomeController : Controller  
  2.     {  
  3.         private readonly ILogger<HomeController> _log;  
  4.   
  5.         public HomeController(ILogger<HomeController> log) => _log = log;  
  6.         static Logger logger = LogManager.GetCurrentClassLogger();  
  7.   
  8.         public IActionResult Index()  
  9.         {  
  10.             logger.Info("测试日志乱码");  
  11.             return View();  
  12.         }  
  13. }  
本文地址:https://www.lisen.me/asp-net-core-using-nlog.html
版权声明:本文为原创文章,版权归 木子网 所有,欢迎分享本文,转载请保留出处!

发表评论


表情