Code Monkey home page Code Monkey logo

correlationid's Introduction

Correlation ID

Correlations IDs are used in distributed applications to trace requests across multiple services. This library and package provides a lightweight correlation ID approach. When enabled, request headers are checked for a correlation ID from the consumer. If found, this correlation ID is attached to the Correlation Context which can be used to access the current correlation ID where it is required for logging etc.

Optionally, this correlation ID can be attached to downstream HTTP calls made via a HttpClient instance created by the IHttpClientFactory.

NOTE: While I plan to add a few more features to this library, I believe it is close to feature complete for the scenario it was designed for. I recommend looking at built-in tracing for .NET apps for more complete and automated application tracing.

Release Notes

Change history and release notes.

Supported Runtimes

  • .NET Standard 2.0+
Package NuGet Stable NuGet Pre-release Downloads Travis CI Azure Pipelines
CorrelationId NuGet NuGet Nuget Build Status Build Status

Installation

You should install CorrelationId from NuGet:

Install-Package CorrelationId

This command from Package Manager Console will download and install CorrelationId and all required dependencies.

All stable and some pre-release packages are available on NuGet.

Quick Start

Register with DI

Inside ConfigureServices add the required correlation ID services, with common defaults.

services.AddDefaultCorrelationId

This registers a correlation ID provider which generates new IDs based on a random GUID.

Add the middleware

Register the middleware into the pipeline. This should occur before any downstream middleware which requires the correlation ID. Normally this will be registered very early in the middleware pipeline.

app.UseCorrelationId();

Where you need to access the correlation ID, you may request the ICorrelationContextAccessor from DI.

public class TransientClass
{
   private readonly ICorrelationContextAccessor _correlationContext;

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

   ...
}

See the sample app for example usage.

Full documentation can be found in the wiki.

Known Issue with ASP.NET Core 2.2.0

It appears that a regression in the code for ASP.NET Core 2.2.0 means that setting the TraceIdentifier on the context via middleware results in the context becoming null when accessed further down in the pipeline. A fix is was released in 2.2.2.

A workaround at this time is to disable the behaviour of updating the TraceIdentifier using the options when adding the middleware.

Support

If this library has helped you, feel free to buy me a coffee or see the "Sponsor" link at the top of the GitHub page.

correlationid's People

Contributors

stevejgordon avatar engenb avatar bossini-mailup avatar juchom avatar nrkirby avatar tdabasinskas avatar azure-pipelines[bot] avatar dependabot[bot] avatar maldworth avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.