当前位置:首页 > Web开发 > 正文

记录器记录结果并编写摘要日志消息

2024-03-31 Web开发

这是该系列的第一篇文章:在ASP.NET Core 3.0中使用Serilog.AspNetCore。

第1部分-使用Serilog RequestLogging来简化ASP.NET Core的日志输出(本篇文章)

第2部分-使用Serilog记录所选的端点名称[敬请等候]

第3部分-使用Serilog.AspNetCore记录MVC属性[敬请等候]

作者:依乐祝

译文地点:https://www.cnblogs.com/yilezhu/p/12215934.html

原文地点:https://andrewlock.net/using-serilog-aspnetcore-in-asp-net-core-3-reducing-log-verbosity/

众所周知,ASP.NET Core的重要转变之一是把日志记录内置于框架中。这意味着您可以(如果需要)从本身的标准日志根本设施访谒所有深层根本设施日志。错误谬误是有时您会收到太多的日志。

在这个简短的系列文章中,我将介绍如何使用。在第一篇文章中,我将讲述如何将Serilog的RequestLoggingMiddleware添加到您的应用措施,以及它供给的好处。在后续文章中,我将描述如何进一步自界说行为。

我已经将这些帖子起草了一段时间。从那时起,Serilog的创建者Nicholas Blumhardt就在ASP.NET Core 3.0中使用Serilog撰写了一篇详尽的博客文章。这是一篇非常详细(至少我认为是这样)的文章,我强烈建议您阅读。您可以在他的文章中找到我在本系列文章中谈论的大部分内容,所以请检察!

原生请求日志

在本节中,首先让我们创建一个标准的ASP.NET Core 3.0的Razor pages应用,固然你也可以直接使用dotnet new webapp命令来进行创建。这将创建一个标准Program.cs,如下所示:

public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }); }

还有一个Startup.cs,用于配置中间件管道,Configure如下所示:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Error"); // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); } app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapRazorPages(); }); }

如果您运行该应用措施并导航至主页,则默认情况下,您会在控制台中看到每个请求城市孕育产生许多的日志。以下日志是针对对主页的单个请求生成的(从此我还没有包孕对CSS和JS文件的其他请求)(这是是开发环境请求呈现的日志):

info: Microsoft.AspNetCore.Hosting.Diagnostics[1] Request starting HTTP/2 GET https://localhost:5001/ info: Microsoft.AspNetCore.Routing.EndpointMiddleware[0] Executing endpoint '/Index' info: Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker[3] Route matched with {page = "/Index"}. Executing page /Index info: Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker[101] Executing handler method SerilogRequestLogging.Pages.IndexModel.OnGet - ModelState is Valid info: Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker[102] Executed handler method OnGet, returned result . info: Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker[103] Executing an implicit handler method - ModelState is Valid info: Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker[104] Executed an implicit handler method, returned result Microsoft.AspNetCore.Mvc.RazorPages.PageResult. info: Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker[4] Executed page /Index in 221.07510000000002ms info: Microsoft.AspNetCore.Routing.EndpointMiddleware[1] Executed endpoint '/Index' info: Microsoft.AspNetCore.Hosting.Diagnostics[2] Request finished in 430.9383ms 200 text/html; charset=utf-8

单个请求就是10条日志。此刻,很清楚,它正在Development环境中运行,该环境默认情况下将Microsoft名称空间中的所有信息记录在“Information”或更高的级别。如果我们切换到Production环境,则默认模板会将Microsoft定名空间的日志过滤到“Warning” 。此刻导航到默认主页会生成以下日志(这里注意,如果你此刻使用ASP.NET Core3.1貌似Microsoft定名空间默认日志级别已经改为Warning):

是的,根柢没有日志!上一次运行中生成的所有日志都位于Microsoft定名空间中,并且属于“Information”级别,因此将它们全部过滤失。就小我私家而言,我感受这有点麻烦。如果出产版本仅仅只是想记录一部分内容,而其他相关联的内容则不进行记录,这将会更有用的。

一种可能的解决方案是自界说应用于每个定名空间的过滤器。例如,您可以将Microsoft.AspNetCore.Mvc.RazorPages定名空间限制为“Warning”级别,而将更通用的Microsoft定名空间保存为“Information”级别。此刻,您将获得精简后的日志集:

info: Microsoft.AspNetCore.Hosting.Diagnostics[1] Request starting HTTP/2 GET https://localhost:5001/ info: Microsoft.AspNetCore.Routing.EndpointMiddleware[0] Executing endpoint '/Index' info: Microsoft.AspNetCore.Routing.EndpointMiddleware[1] Executed endpoint '/Index' info: Microsoft.AspNetCore.Hosting.Diagnostics[2] Request finished in 184.788ms 200 text/html; charset=utf-8

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/31312.html