ASP.NET CORE下日志NLOG使用 摘自《深入浅出ASP.NET CORE》 NUGET:NLog.Web.AspNetCore 4.9.0 根目录下nlog.config
<?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"> <targets> <target name="allfile" xsi:type="File" fileName="Logs/${shortdate}.log"></target> </targets> <rules> <logger name="*" minlevel="Trace" writeTo="allfile"></logger> </rules> </nlog>.net core 3.1项目中的program
public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureLogging((hostingContext, logging) => { logging.AddNLog(); //加入nlog }) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });net7项目中的program 只需要加一句:builder.Logging.AddNLog(); 就行,如果要测试把下面的 if (!app.Environment.IsDevelopment())注释掉就行 /home/error方法里的代码:
private readonly ILogger<HomeController> _logger; [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Error() { var ex = HttpContext.Features.Get<IExceptionHandlerPathFeature>(); ViewBag.ExceptionPath = ex.Path; ViewBag.ExceptionMessage = ex.Error.Message; ViewBag.StackTrace = ex.Error.StackTrace; _logger.LogError($"异常路径:{ex.Path}\r\n异常信息:{ex.Error.Message}\r\n堆栈跟踪:{ex.Error.StackTrace}\r\n\r\n\r\n"); return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); }
标签:CORE,ASP,StackTrace,NLOG,ex,Error,NET From: https://www.cnblogs.com/niunan/p/17596024.html