Skip to content

Instantly share code, notes, and snippets.

@ankitvijay
Last active November 22, 2020 20:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ankitvijay/172b4bf30d67f38f368b68ee11bdf7b9 to your computer and use it in GitHub Desktop.
Save ankitvijay/172b4bf30d67f38f368b68ee11bdf7b9 to your computer and use it in GitHub Desktop.
CorrelationId Context
public class CorrelationIdContext
{
private static readonly AsyncLocal<string> _correlationId = new AsyncLocal<string>();
public static void SetCorrelationId(string correlationId)
{
if (string.IsNullOrWhiteSpace(correlationId)
{
throw new ArgumentException("Correlation Id cannot be null or empty", nameof(correlationId));
}
if (!string.IsNullOrWhiteSpace(_correlationId.Value))
{
throw new InvalidOperationException("Correlation Id is already set for the context");
}
_correlationId.Value = correlationId;
}
public static string GetCorrelationId()
{
return _correlationId.Value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment