HomeCloud ComputingLearn how to add consumer context to request traces in ASP.NET Core

Learn how to add consumer context to request traces in ASP.NET Core



The answer to this drawback is including metadata reminiscent of consumer Id or consumer identify to counterpoint your traces. To realize this, we’ll implement a middleware element in our undertaking. Recall that, in ASP.NET Core, a middleware is a element that’s able to dealing with Http requests and responses by attaching itself to the request processing pipeline.

Create a middleware to seize consumer context

A middleware element in ASP.NET Core is represented by a category that appears like every other C# class and comprises the InvokeAsync technique. To implement the middleware for this instance, create a brand new class named MyUserContextMiddleware into your undertaking. Initially, this class ought to appear to be this:


public sealed class MyUserContextMiddleware
{
    public async Activity InvokeAsync(HttpContext context)
    {
        //Not but applied
    }
}

In ASP.NET Core, a middleware ought to have a constructor that accepts a reference to an occasion of a RequestDelegate sort as a parameter. To be extra exact, a RequestDelegate is much like every other delegate that accepts an HttpContext and returns a Activity as proven within the code snippet given under.


public class MyUserContextMiddleware
{
    personal readonly RequestDelegate _next;
    public MyUserContextMiddleware(RequestDelegate subsequent)
    {
        _next = subsequent;
    }
    public async Activity InvokeAsync(HttpContext context)
    {
        //Not but applied
    }
}

It’s also possible to write the above piece of code as proven under.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments