[英]AsyncLocal Serilog LogContext in Blazor Server Middleware
我創建了一個自定義 Middliware 以將有關 HttpContext 的信息推送到我的 Serilog LogContext 中:
if (context == null) throw new ArgumentNullException(nameof(context));
string profile = context.Request.Host.Host;
string ipAddress = context.Connection.RemoteIpAddress?.ToString();
string userAgent = context.Request.Headers["User-Agent"].ToString();
using (LogContext.PushProperty("Profile", profile))
using (LogContext.PushProperty("IpAddress", ipAddress))
using (LogContext.PushProperty("UserAgent", userAgent))
{
await _next(context);
}
我想了解我的 LogContext 屬性在各種查詢(Hhtp 請求)中的行為。
從微軟文檔:
AsyncLocal 表示給定異步控制流(例如異步方法)本地的環境數據。
Blazor 服務器中間件中的異步控制流程是什么?
我的異步控制流是什么
AsyncLocal<T>
值在整個await _next(context)
中持續存在; 即,直到_next
完成。 在所有現代中間件系統中,這是繼當前中間件之后的所有中間件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.