How one can add person context to request traces in ASP.NET Core

Learn extra at:

The answer to this drawback is including metadata comparable to person Id or person title to counterpoint your traces. To realize this, we’ll implement a middleware element in our challenge. 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 person context

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


public sealed class MyUserContextMiddleware
{
    public async Job 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 some other delegate that accepts an HttpContext and returns a Job as proven within the code snippet given under.


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

You may as well write the above piece of code as proven under.

Turn leads into sales with free email marketing tools (en)

Leave a reply

Please enter your comment!
Please enter your name here