stevejgordon / CorrelationId

An ASP.NET Core middleware component which synchronises a correlation ID for cross API request logging.
MIT License
558 stars 106 forks source link

Usage in Azure Functions #98

Open alastairtree opened 3 years ago

alastairtree commented 3 years ago

Hi,

Thanks for this library, it is great.

I had a use case for it in Azure Functions where there is no middleware pipeline to hook into, so I used the library and added a small class that emulates the key bits of the middleware from this project but allowing it to be called as a wrapper function on a single HTTP Request in isolation.

You can see the code in this gist and you use it like this:

public MyFunc(HttpCorrelation http)
{
      this.http = http;
}

[FunctionName(nameof("MyFunc"))]
public async Task<IActionResult> Put([HttpTrigger(AuthorizationLevel.Function, "put", Route = "endpoint")]
            HttpRequest req)
{
    return await http.ExecuteFunction(req, async () =>
    {
          // Do stuff ...
         return new AcceptedResult();
    });
}

Just posting it here in case it is of use to anyone or you wanted to adopt a version of it.