.NET 6.0 中的全局错误处理

示例源码:https://download.csdn.net/download/hefeng_aspnet/89998881 

这是一篇关于如何在 .NET 6.0 中实现全局异常处理程序的快速文章。

全局错误处理程序用于捕获所有异常,并且无需在每个 .NET 应用程序中创建多个处理代码。

在此示例中,我们将使用中间件 和Serilog来集中日志收集的捕获和目标配置。

先决条件

Visual Studio 2022 (.NET 6.0)
Nuget包Serilog.AspNetCoreSerilog.Enrichers.EnvironmentSerilog.Exceptions
中间件

        中间件基本上是 定义 HTTP 调用执行管道的组件,即请求在应用程序内执行的所有步骤,从请求到响应。它们是链接在一起的,因此一个中间件可以通过传递相同请求上下文的委托来执行另一个中间件。

        需要创建的第一个中间件是ErrorHandlingMiddleware,负责捕获应用程序中的所有(未处理的)错误:

ErrorHandlingMiddleware.cs :

public class ErrorHandlingMiddleware
{
    private readonly RequestDelegate next;

    public ErrorHandlingMiddleware(RequestDelegate next)
    {
        this.next = next;
    }

    public async Task Invoke(HttpContext context)
    {
        try
        {
            await next(context);
        }
        catch (Exception ex)
        {
            await HandleExceptionAsync(context, ex);
        }
    }

    private static Task HandleExceptionAsync(HttpContext context, Exception exception)
    {
        Log.Error(exception, "Erro na aplicação");

        var code = HttpStatusCode.InternalServerError;

        if (exception is Exception) code = HttpStatusCode.BadRequest;
        // else if (exception is MyUnauthorizedException) code = HttpStatusCode.Unauthorized;
        // else if (exception is MyException)             code = HttpStatusCode.BadRequest;

        var result = JsonConvert.SerializeObject(new { error = exception.Message });

        context.Response.ContentType = "application/json";
        context.Response.StatusCode = (int)code;
        return context.Response.WriteAsync(result);
    }
}

第二个中间件RequestSerilLogMiddleware)将用于捕获附加信息,例如:认证用户的电子邮件、用户IP和标头信息。 

RequestSerilLogMiddleware.cs:

public class RequestSerilLogMiddleware
{
    private readonly RequestDelegate _next;

    public RequestSerilLogMiddleware(RequestDelegate next)
    {
        _next = next;
    }

    public Task Invoke(HttpContext context)
    {
        using (LogContext.PushProperty("UserName", context?.User?.Identity?.Name ?? "anônimo"))
        using (LogContext.PushProperty("CorrelationId", GetCorrelationId(context)))
        {
            return _next.Invoke(context);
        }
    }
}

接下来需要配置Serilog,通知主要丰富日志记录添加更多信息: 

SerilogExtensions.cs:

public static class SerilogExtensions
{
    public static void AddSerilogApi(IConfiguration configuration)
    {
        Log.Logger = new LoggerConfiguration()
            .ReadFrom.Configuration(configuration)
            .Enrich.WithProperty("ApplicationName", $"API Exceptions - {Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT")}")
            .Enrich.FromLogContext()
            .Enrich.WithMachineName()
            .Enrich.WithEnvironmentUserName()
            .Enrich.WithExceptionDetails()
            .WriteTo.Console()
            .WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} {Properties:j}{NewLine}{Exception}")
            .CreateLogger();
    }
}

最后,在program.cs类中,需要注册创建的两个中间件的使用:

program.cs:

var builder = WebApplication.CreateBuilder(args);
SerilogExtensions.AddSerilogApi(builder.Configuration);
builder.Host.UseSerilog(Log.Logger);

builder.Services.AddRouting(options => options.LowercaseUrls = true);
builder.Services.AddSwaggerApi(builder.Configuration);
builder.Services.AddControllers();

var app = builder.Build();

if (app.Environment.IsDevelopment())
{
    app.UseDeveloperExceptionPage();
}

app.UseMiddleware<RequestSerilLogMiddleware>();
app.UseMiddleware<ErrorHandlingMiddleware>();

app.UseSwaggerDocApi();

app.UseHttpsRedirection();
app.UseRouting();
app.MapControllers();

app.Run();

测试

        运行 API 时,可以通过调用/api/log/exception端点来测试错误收集,这会在应用程序控制器中引发错误并被ErrorHandlingMiddleware 中间件捕获:

        捕获异常是所有应用程序的关键部分,使用中间件除了作为收集日志、自定义返回消息和获取用户的集中位置之外,也是让您的应用程序不因缺乏错误处理而“解除武装”的最安全方法凭证信息。

如果您喜欢此文章,请收藏、点赞、评论,谢谢,祝您快乐每一天。 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

csdn_aspnet

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值