Class CorrelationId
- Namespace
- Codebelt.SharedKernel
- Assembly
- Codebelt.SharedKernel.dll
Represents a CorrelationId object that can be used as unique identifier that help you trace requests across multiple services in a distributed system.
public record CorrelationId : Token, IEquatable<ValueObject>, IEquatable<SingleValueObject<string>>, IEquatable<Token>, IEquatable<CorrelationId>
- Inheritance
-
objectCorrelationId
- Implements
- Inherited Members
-
SingleValueObject<string>.ToString()SingleValueObject<string>.GetHashCode()SingleValueObject<string>.Equals(object)SingleValueObject<string>.Equals(ValueObject)SingleValueObject<string>.Equals(SingleValueObject<string>)SingleValueObject<string>.<Clone>$()SingleValueObject<string>.EqualityContract
Examples
The following example creates a CorrelationId from a Guid, prints the resulting trace identifier so a developer can see it at runtime, and then returns it so a downstream service can pick it up from the result. Use the implicit Guid conversion when an upstream component already produced a request id, or the string constructor when one arrives as an HTTP header.
using System;
using Codebelt.SharedKernel;
namespace DistributedTracingSample;
public static class CorrelationProbe
{
public static string EmitTrace(Guid upstreamId)
{
CorrelationId correlation = upstreamId;
var traceId = correlation.Value;
// Print the correlation id so distributed traces and logs are visible during local debugging.
Console.WriteLine(traceId);
return traceId;
}
}
Remarks
Constructors
CorrelationId(Guid)
Initializes a new instance of the CorrelationId class.
public CorrelationId(Guid value)
Parameters
valueGuidThe Guid value to assign the role of CorrelationId.
CorrelationId(string)
Initializes a new instance of the CorrelationId class.
public CorrelationId(string value)
Parameters
valuestringThe string value to assign the role of CorrelationId.
Exceptions
- ArgumentNullException
valuecannot be null.- ArgumentException
valuecannot be empty or consist only of white-space characters.- ArgumentOutOfRangeException
valuecontained one or more white-space characters -or-valuewas less than 32 characters -or-valuewas greater than 128 characters -or-valueconsist only of same repeated character.
Operators
implicit operator CorrelationId(Guid)
Performs an implicit conversion from Guid to CorrelationId.
public static implicit operator CorrelationId(Guid value)
Parameters
valueGuidThe value to convert.
Returns
- CorrelationId
A CorrelationId that is equivalent to
value.
implicit operator CorrelationId(string)
Performs an implicit conversion from string to CorrelationId.
public static implicit operator CorrelationId(string value)
Parameters
valuestringThe value to convert.
Returns
- CorrelationId
A CorrelationId that is equivalent to
value.