Table of Contents

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
object
CorrelationId
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

value Guid

The Guid value to assign the role of CorrelationId.

CorrelationId(string)

Initializes a new instance of the CorrelationId class.

public CorrelationId(string value)

Parameters

value string

The string value to assign the role of CorrelationId.

Exceptions

ArgumentNullException

value cannot be null.

ArgumentException

value cannot be empty or consist only of white-space characters.

ArgumentOutOfRangeException

value contained one or more white-space characters -or- value was less than 32 characters -or- value was greater than 128 characters -or- value consist 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

value Guid

The 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

value string

The value to convert.

Returns

CorrelationId

A CorrelationId that is equivalent to value.

See Also