CorrelationId in .Net Core and Java


1. In Java
https://github.com/rabbitmq/rabbitmq-tutorials/blob/master/java
segmentfault.com/a/1190000012690912

2. .Net Core
1. set up reference in *.csproj file
  <ItemGroup>
  <PackageReference Include="CorrelationId" Version="2.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.App" />
  </ItemGroup>
   <!--PackageReference Include="CorrelationId" Version="1.0.1" /-->  

2. Add Extension methods:AddCorrelationId and UseCorrelationId
using CorrelationId;
in class Startup
 public void ConfigureServices(IServiceCollection services)
        {...
            services.AddCorrelationId();
        }
        public void Configure(IApplicationBuilder app)
        {...
            app.UseCorrelationId("testRelationID");
         ...
        }

3. insert a ICorrelationContextAccessor  parameter,  into constructor of controller
public TestController
(TestContext context,
ICorrelationContextAccessor correlationContext){...}

4. For Request
private const string CorrelationIdHeaderName = "X-Correlation-Id";
var correlation = _correlationContext.CorrelationContext.CorrelationId;
    var s = Request;
    var sRequest = this.HttpContext.Request;
    var rH = sRequest.Headers;
    rH.Add(CorrelationIdHeaderName,correlation);
    Console.WriteLine();
    Console.WriteLine($"Method=={sRequest.Method} ");
    Console.WriteLine($"Protocol=={sRequest.Protocol} ");
    foreach (var sHead in rH)
            {
                Console.WriteLine($"{sHead.Key}=={sHead.Value}");
    }
//X-Correlation-Id==0HLEU0E6GSAJ8:00000001

Reference:
https://github.com/stevejgordon/CorrelationId/blob/dev/samples/MvcCorrelationIdSample/Controllers/ValuesController.cs
https://github.com/jin-xinchen/visualStudioCode/blob/master/TodoApi/Controllers/TodoController.cs
https://github.com/jin-xinchen/visualStudioCode/blob/master/TodoApi/TodoApi.csproj
https://github.com/dotnet/standard/tree/81a810a27baf0716b4d518f3adb9a0d68f1e02aa/docs/versions
https://www.stevejgordon.co.uk/asp-net-core-correlation-ids
https://github.com/stevejgordon/CorrelationId

评论

此博客中的热门博文

XML, XSL, HTML

Input in element.eleme.io

Data URI是由RFC 2397 ACE