4

我想了解登录asp.net cortex 2.1的问题。我的目标是记录所有可能导致红隼服务器中断的严重错误。正如我从文档 Microsoft.Extensions.Logging 和 Serilog 中了解到的,您需要在每个控制器中创建一个记录器,并设置记录条件(例如 try/catch)。有没有一种更方便的方法可以从一个地方记录项目中的任何严重错误?如有任何帮助,我将不胜感激。

4

2 回答 2

3

您可以创建一个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作为你的第一个中间件,这样它也可以从其他中间件中捕获异常。

于 2020-02-25T06:53:40.013 回答
1

优秀指南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>();
    }
}
于 2020-02-25T06:55:45.767 回答