惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

月光博客
月光博客
Cyberwarzone
Cyberwarzone
L
LINUX DO - 最新话题
N
News and Events Feed by Topic
T
Troy Hunt's Blog
Help Net Security
Help Net Security
S
Security @ Cisco Blogs
Google DeepMind News
Google DeepMind News
Security Archives - TechRepublic
Security Archives - TechRepublic
M
MIT News - Artificial intelligence
G
Google Developers Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
V2EX - 技术
V2EX - 技术
Y
Y Combinator Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
大猫的无限游戏
大猫的无限游戏
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Microsoft Security Blog
Microsoft Security Blog
Cisco Talos Blog
Cisco Talos Blog
T
Threatpost
Recent Commits to openclaw:main
Recent Commits to openclaw:main
S
SegmentFault 最新的问题
I
InfoQ
H
Hacker News: Front Page
D
Docker
Scott Helme
Scott Helme
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Blog — PlanetScale
Blog — PlanetScale
人人都是产品经理
人人都是产品经理
博客园 - 叶小钗
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
N
Netflix TechBlog - Medium
AWS News Blog
AWS News Blog
Know Your Adversary
Know Your Adversary
博客园 - 【当耐特】
T
Tor Project blog
U
Unit 42
H
Heimdal Security Blog
Microsoft Azure Blog
Microsoft Azure Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
P
Privacy & Cybersecurity Law Blog
PCI Perspectives
PCI Perspectives
美团技术团队
O
OpenAI News
T
Tailwind CSS Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
B
Blog
GbyAI
GbyAI
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
MyScale Blog
MyScale Blog

博客园 - BloggerSb

Swagger 文档设置api版本 .Net Core Routing Demo .net Core读取配置比较 简化版DbExecutor,将DataTable映射到T属性(支持Dapper风格的匿名参数)。(编程题) 大文件单词统计 (编程题) ASP.NET Core CRUD API 创建 UserController,实现 Get, Post, Put, Delete 方法,使用 EF Core 访问数据库。 (编程题) 设计模式落地:Repository + UnitOfWork + CQRS 完整实现 (编程题) 给定百万级订单表,实现高效分页 + 动态条件查询 + 导出 Excel(避免内存爆炸) (编程题) 异步限流器实现(编程题) 编程题,记录所有接口的执行耗时 .net面试题目 (问答题) 面试高频简答题 Aspose最新Slides破解 HttpContext.User.Identity.IsAuthenticated 为false 关于Cannot resolve scoped service from root provider解决方案 MongoDB用户权限管理,设置密码并连接 mongodb连接字符串 mongodb 使用 MongoDB Compass 创建账号,角色 安装mongodb bootstrap popover 设置悬浮框宽度 div contenteditable="true" 添加placehoder效果 光标自动定位到起始位置contenteditable="true" ,v-html绑定内容,div可编辑时,光标移到最前面
实现一个带 CorrelationId、请求日志、异常统一处理的中间件链 (编程题)
BloggerSb · 2026-04-17 · via 博客园 - BloggerSb

ASP.NET Core 中间件 / Filter / Minimal API

   题:实现一个带 CorrelationId、请求日志、异常统一处理的中间件链。 

   要求:支持 Scoped 服务注入 + 性能无损。

先定义统一的异常响应模型、CorrelationId 存取器(支持 Scoped 注入)
// CorrelationId 存取器(Scoped 服务,每个请求独立实例)
public interface ICorrelationIdAccessor
{
    string GetCorrelationId();
    void SetCorrelationId(string correlationId);
}

public class CorrelationIdAccessor : ICorrelationIdAccessor
{
    private string _correlationId;
    public string GetCorrelationId() => _correlationId;
    public void SetCorrelationId(string correlationId) => _correlationId = correlationId;
}

// 统一异常响应模型
public class ApiErrorResponse
{
    public string TraceId { get; set; }
    public int StatusCode { get; set; }
    public string Message { get; set; }
    public DateTime Timestamp { get; set; } = DateTime.UtcNow;
}

从请求头读取 / 生成 CorrelationId,存入 Scoped 服务 + 响应头
using Microsoft.AspNetCore.Http;

// 纯静态类 + 委托实现,无实例化开销,性能最优
public static class CorrelationIdMiddleware
{
    // 约定请求头/响应头名称
    private const string CorrelationIdHeader = "X-Correlation-ID";

    public static RequestDelegate UseCorrelationId(this RequestDelegate next)
    {
        // 异步委托,非阻塞,无闭包捕获
        return async context =>
        {
            // 从 Scoped 服务获取存取器(官方推荐,支持作用域)
            var correlationIdAccessor = context.RequestServices.GetRequiredService<ICorrelationIdAccessor>();
            
            // 读取请求头的 CorrelationId,不存在则生成 GUID
            var correlationId = context.Request.Headers.TryGetValue(CorrelationIdHeader, out var id) 
                ? id.ToString() 
                : Guid.NewGuid().ToString("N");

            // 存入 Scoped 服务 + 响应头
            correlationIdAccessor.SetCorrelationId(correlationId);
            context.Response.Headers.TryAdd(CorrelationIdHeader, correlationId);

            // 传递到下一个中间件
            await next(context);
        };
    }
}

捕获所有未处理异常,返回标准化 JSON 响应,记录错误日志
using Microsoft.AspNetCore.Http;
using System.Net;
using System.Text.Json;

public static class ExceptionHandlingMiddleware
{
    // 序列化配置(静态只读,全局复用,性能极高)
    private static readonly JsonSerializerOptions _jsonOptions = new()
    {
        PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
        WriteIndented = false
    };

    public static RequestDelegate UseExceptionHandling(this RequestDelegate next)
    {
        return async context =>
        {
            try
            {
                await next(context);
            }
            catch (Exception ex)
            {
                // 获取 Scoped CorrelationId
                var correlationId = context.RequestServices
                    .GetRequiredService<ICorrelationIdAccessor>()
                    .GetCorrelationId();

                // 获取日志服务(支持 Scoped/Transient/Singleton)
                var logger = context.RequestServices
                    .GetRequiredService<ILogger<Program>>();

                // 记录错误日志(携带 CorrelationId)
                logger.LogError(ex, "请求发生异常 | TraceId: {TraceId}", correlationId);

                // 统一响应处理
                context.Response.ContentType = "application/json";
                context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;

                var errorResponse = new ApiErrorResponse
                {
                    TraceId = correlationId,
                    StatusCode = context.Response.StatusCode,
                    Message = ex.Message // 生产环境可替换为友好提示
                };

                // 序列化写入响应
                await JsonSerializer.SerializeAsync(context.Response.Body, errorResponse, _jsonOptions);
            }
        };
    }
}
记录请求开始 / 结束、耗时、路径、状态码、CorrelationId,零内存分配
using Microsoft.AspNetCore.Http;
using System.Diagnostics;

public static class RequestLoggingMiddleware
{
    public static RequestDelegate UseRequestLogging(this RequestDelegate next)
    {
        return async context =>
        {
            var logger = context.RequestServices.GetRequiredService<ILogger<Program>>();
            var correlationIdAccessor = context.RequestServices.GetRequiredService<ICorrelationIdAccessor>();
            
            var method = context.Request.Method;
            var path = context.Request.Path;
            var traceId = correlationIdAccessor.GetCorrelationId();
            
            // 高精度计时(性能远优于 DateTime)
            var stopwatch = Stopwatch.StartNew();

            try
            {
                logger.LogInformation(
                    "请求开始 | Method: {Method} | Path: {Path} | TraceId: {TraceId}",
                    method, path, traceId);

                await next(context);
            }
            finally
            {
                stopwatch.Stop();
                var statusCode = context.Response.StatusCode;
                var elapsedMs = stopwatch.ElapsedMilliseconds;

                logger.LogInformation(
                    "请求结束 | Method: {Method} | Path: {Path} | Status: {StatusCode} | 耗时: {ElapsedMs}ms | TraceId: {TraceId}",
                    method, path, statusCode, elapsedMs, traceId);
            }
        };
    }
}

Program.cs 注册 + 配置(Minimal API 完整版)
关键要点:
1.    注册顺序:异常处理 → CorrelationId → 请求日志 → 路由(必须严格按这个顺序)
2.    Scoped 服务注册:ICorrelationIdAccessor 作用域注入
3.    纯委托中间件,无反射、无实例化、无性能损耗

var builder = WebApplication.CreateBuilder(args);

// 1. 注册 Scoped 服务(核心:支持作用域注入,每个请求独立实例)
builder.Services.AddScoped<ICorrelationIdAccessor, CorrelationIdAccessor>();

// 2. 添加日志(框架默认集成,无需额外配置)
builder.Logging.AddConsole();

var app = builder.Build();

// 3. 注册高性能中间件链(顺序绝对不能错!)
app.Use(async (context, next) =>
{
    // 异常处理(最外层,捕获所有异常)
    await ExceptionHandlingMiddleware.UseExceptionHandling(next)(context);
});

app.Use(async (context, next) =>
{
    // CorrelationId(第二层,生成唯一标识)
    await CorrelationIdMiddleware.UseCorrelationId(next)(context);
});

app.Use(async (context, next) =>
{
    // 请求日志(第三层,记录完整请求生命周期)
    await RequestLoggingMiddleware.UseRequestLogging(next)(context);
});

// 4. Minimal API 路由测试
app.MapGet("/", () => "Hello World!");

// 测试异常接口
app.MapGet("/error", () =>
{
    throw new InvalidOperationException("测试业务异常");
});

// 测试在业务中获取 CorrelationId(验证 Scoped 服务生效)
app.MapGet("/trace", (ICorrelationIdAccessor correlationIdAccessor) =>
{
    return new
    {
        Message = "当前请求 TraceId",
        TraceId = correlationIdAccessor.GetCorrelationId()
    };
});

app.Run();