繁体   English   中英

Asp.net Core 2.1 日志记录

[英]Asp.net Core 2.1 Logging

我想了解登录asp.net cortex 2.1的问题。 我的目标是注册所有可能导致 kestrel 服务器中断的严重错误。 正如我从文档 Microsoft.Extensions.Logging 和 Serilog 中了解到的,您需要在每个控制器中创建一个记录器,并设置记录条件(例如 try/catch)。 有没有更方便的方法来从一个地方记录项目中的任何关键错误? 我会很乐意提供任何帮助。

您可以创建一个Exception Middleware 请参阅有关中间件的文档

using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using System;
using System.Net;
using System.Threading.Tasks;

namespace API.Middlewares
{
    /// <summary>
    /// Custom Exception middleware to catch unhandled exception during runtime
    /// </summary>
    public class ExceptionMiddleware
    {
        private readonly RequestDelegate _next;
        private readonly ILogger<ExceptionMiddleware> _logger;

        public ExceptionMiddleware(RequestDelegate next, ILogger<ExceptionMiddleware> logger)
        {
            _logger = logger;
            _next = next;
        }

        public async Task InvokeAsync(HttpContext httpContext)
        {
            try
            {
                // next request in pipeline
                await _next(httpContext);
            }
            catch (Exception ex)
            {
                // Logs your each error
                _logger.LogError($"Something went wrong: {ex}");
                await HandleExceptionAsync(httpContext, ex);
            }
        }

        /// <summary>
        /// Handle runtime error
        /// </summary>
        /// <param name="context"></param>
        /// <param name="exception"></param>
        /// <returns></returns>
        private Task HandleExceptionAsync(HttpContext context, Exception exception)
        {
            // Customise it to handle more complex errors
            context.Response.ContentType = "application/json";
            context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;

            return context.Response.WriteAsync($"Something went wrong: {exception.Message}");
        }
    }
}

然后将其添加到您的Startup.cs

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            // Custom exception middleware
            app.UseMiddleware<ExceptionMiddleware>();;

            app.UseMvc();
        }

根据DOCS

在 Startup.Configure 方法中添加中间件组件的顺序定义了对请求调用中间件组件的顺序以及响应的相反顺序。

因此,始终将Exception Middleware添加为您的第一个Middleware ,以便它也可以捕获来自其他Middlewares的异常。

优秀指南Net Core 错误处理

异常处理程序 lambda

自定义异常处理程序页面的替代方法是向 UseExceptionHandler 提供 lambda。 使用 lambda 允许在返回响应之前访问错误。

if (env.IsDevelopment())
{
    app.UseDeveloperExceptionPage();
}
else
{
   app.UseExceptionHandler(errorApp =>
   {
        errorApp.Run(async context =>
        {
            context.Response.StatusCode = 500;
            context.Response.ContentType = "text/html";

            await context.Response.WriteAsync("<html lang=\"en\"><body>\r\n");
            await context.Response.WriteAsync("ERROR!<br><br>\r\n");

            var exceptionHandlerPathFeature = 
                context.Features.Get<IExceptionHandlerPathFeature>();

            // Use exceptionHandlerPathFeature to process the exception (for example, 
            // logging), but do NOT expose sensitive error information directly to 
            // the client.

            if (exceptionHandlerPathFeature?.Error is FileNotFoundException)
            {
                await context.Response.WriteAsync("File error thrown!<br><br>\r\n");
            }

            await context.Response.WriteAsync("<a href=\"/\">Home</a><br>\r\n");
            await context.Response.WriteAsync("</body></html>\r\n");
            await context.Response.WriteAsync(new string(' ', 512)); // IE padding
        });
    });
    app.UseHsts();
}

因此,您可以在 lamda 中进行日志记录并捕获从 Web api 抛出的所有异常。

异常过滤器

异常过滤器可用于捕获发生在 MVC 操作中的异常,但它们不如异常处理中间件灵活。 我们推荐使用中间件。 仅在需要根据选择的 MVC 操作以不同方式执行错误处理的地方使用过滤器。

自定义异常中间件

已经在其他答案中提到了。 这里非常整洁的指南。 我只是添加了一种巧妙的方法来添加扩展方法作为开箱即用的中间件。

// Extension method used to add the middleware to the HTTP request pipeline.
public static class HttpStatusCodeExceptionMiddlewareExtensions
{
    public static IApplicationBuilder UseHttpStatusCodeExceptionMiddleware(this IApplicationBuilder builder)
    {
        return builder.UseMiddleware<HttpStatusCodeExceptionMiddleware>();
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM