CorrelationId Context
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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