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

推荐订阅源

OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 聂微东
博客园 - 叶小钗
爱范儿
爱范儿
罗磊的独立博客
Hugging Face - Blog
Hugging Face - Blog
阮一峰的网络日志
阮一峰的网络日志
S
SegmentFault 最新的问题
Apple Machine Learning Research
Apple Machine Learning Research
美团技术团队
T
Tailwind CSS Blog
博客园 - 司徒正美
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
宝玉的分享
宝玉的分享
量子位
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
The Cloudflare Blog
人人都是产品经理
人人都是产品经理
小众软件
小众软件
博客园 - 【当耐特】
博客园 - 三生石上(FineUI控件)
V
Visual Studio Blog
雷峰网
雷峰网
酷 壳 – CoolShell
酷 壳 – CoolShell

博客园 - 我的用户名

解决方案:包 Avalonia.Android 11.x.x 与 net8.0-android21.0 (.NETCoreApp,Version=v8.0) 不兼容。 包 Avalonia.Android11.x.x 支持: net8.0-android34.0 (.NETCoreApp,Version=v8.0) avalonia android TextBox多行模式下回车会关闭输入法问题 avalonia ComboBox 绑定后无法选中项的问题 Winform 使用label画分割线 VS-PostBuildEvent常用示例 bat脚本,请求管理员权限(转载) 在 .NET 中使用依赖注入(转载) 在 ASP.NET Core 中使用多个环境(转载) .NET Core 和 ASP.NET Core 中的日志记录(转载) RSA 从私钥中获取公钥 C# 简繁互转 JT808-2019协议,协议消息ID .Net开发中不太常用的DLL及用法 分享一个dotnet自动发布Docker的脚本 VS常用设置 NuGet修改默认包保存的位置 winform下UserControl未标记为可序列化问题 AgileConfig服务端搭建 IE浏览器,各版本的请求头信息
CorrelationId 文档
我的用户名 · 2022-02-10 · via 博客园 - 我的用户名

Provides basic middleware for syncing a correlation ID across ASP.NET Core APIs.

Installation

First install the package via NuGet: Install-Package CorrelationId

In order to use the 3.0.0 version of this library, the IServiceCollection extension method will need to be called in ConfigureServices. To use the common defaults call AddDefaultCorrelationId.

public void ConfigureServices(IServiceCollection services)
{
   ...
   services.AddDefaultCorrelationId();
   ...
}

The middleware then needs to be added in the Configure method, prior to registering any other middleware that you want to have access to the correlation ID. Usually, it can be one of the first items registered.

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
   app.UseCorrelationId();

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

   app.UseRouting();

   ...
}

Any requests to your application, passing a CorrelationID header, will then use that correlation ID for the remainder of the request. By default, CorrelationId looks for a header named "X-Correlation-ID". This is configurable (see below).

To access the CorrelationId from your application code (for custom logging) you can use the newly added CorrelationContext which carries the CorrelationId for the current request. To gain access to the CorrelationContext from your classes you can add a constructor dependency on ICorrelationContextAccessor which will be injected by the DI framework.

You can then use the ICorrelationContextAccessor to access the CorrelationContext and its current ID.

public class TransientClass
{
   private readonly ICorrelationContextAccessor _correlationContext;

   public TransientClass(ICorrelationContextAccessor correlationContext)
   {
	  _correlationContext = correlationContext;
   }
   
    ...
}

Passing on the correlation ID

When your code calls another service you will need to get the correlation ID from the CorrelationContext.CorrelationId property and pass it as a header in your HTTP call. As long as both services are configured with the same header name and both have the Correlation ID middleware enabled, you will see all logging using a matching value between services.

When using the IHttpClientFactory feature of ASP.NET Core, you may configure automatic correlation ID propagation by registering the correlation ID forwarding handler.

services.AddHttpClient("MyClient")
                .AddCorrelationIdForwarding();

Custom Correlation ID Provider

A major new feature in this release is the concept of an ICorrelationIdProvider. This interface defines an abstraction for generating correlation IDs. The library includes two provider implementations which include the previous behaviour.

  • The GuidCorrelationIdProvider, when registered will generate new GUID-based correlations IDs.
  • The TraceIdCorrelationIdProvider will generate the correlation ID, setting it to the same value as the TraceIdentifier string on the HttpContext.

Only one provider may be registered. Registering multiple providers will cause an InvalidOperationException to be thrown.

To use the WithTraceIdentifierProvider you can call the TraceIdCorrelationIdProvider extension on the ICorrelationIdBuilder within ConfigureServices.

To register a custom provider, which must implement the ICorrelationIdProvider interface, use the WithCustomProvider<T> extension method.

Configuration

If you want to customise the options for the CorrelationId; then you may register the service, passing in an action to operate on the CorrelationIdOptions.

  1. RequestHeader - The name of the header from which the Correlation ID is read. The default value is X-Correlation-ID.
  2. ResponseHeader - The name of the header to which the Correlation ID is written on the response. The default value is to use the same value as the RequestHeader.
  3. IncludeInResponse - Controls whether the correlation ID is returned in the response headers. The default value is true.
  4. EnforceHeader - Enforce the inclusion of the correlation ID request header. When true and a correlation ID header is not included, the request will fail with a 400 Bad Request response.
  5. AddToLoggingScope - Add the correlation ID value to the logger scope for all requests. When true the value of the correlation ID will be added to the logger scope payload.
  6. LoggingScopeKey - The name for the key used when adding the correlation ID to the logger scope. The default value is "CorrelationId".
  7. IncludeInResponse - Controls whether the correlation ID is returned in the response headers. The default value is true.
  8. UpdateTraceIdentifier - Controls whether the ASP.NET Core TraceIdentifier will be set to match the CorrelationId. The default value is false.
  9. CorrelationIdGenerator - A Func<string> that returns the correlation ID in cases where no correlation ID is retrieved from the request header. It can be used to customise the correlation ID generation. When set, this function will be used instead of the registered ICorrelationIdProvider.

Example of registration with options:

services.AddDefaultCorrelationId(options =>
{ 
    options.CorrelationIdGenerator = () => "Foo";
    options.AddToLoggingScope = true;
    options.EnforceHeader = true;
    options.IgnoreRequestHeader = false;
    options.IncludeInResponse = true;
    options.RequestHeader = "My-Custom-Correlation-Id";
    options.ResponseHeader = "X-Correlation-Id";
    options.UpdateTraceIdentifier = false;
});

See the Release Notes for details of all breaking changes since 2.1.x.

其他文档:

https://www.cnblogs.com/night-w/p/14144112.html