Files

51 lines
1.5 KiB
C#
Executable File

using Opsoft.NetCore.O3App.Domain;
using Opsoft.NetCore.O3App.Domain.Models;
namespace Opsoft.NetCore.O3App.Infrastructure
{
/// <inheritdoc />
public class TraceContextFactory : ITraceContextFactory
{
private readonly ITraceContextAccessor traceContextAccessor;
/// <summary>
/// Initializes a new instance of the <see cref="T:TraceId.TraceContextFactory" /> class.
/// </summary>
public TraceContextFactory()
: this(null)
{ }
/// <summary>
/// Initializes a new instance of the <see cref="TraceContextFactory"/> class.
/// </summary>
/// <param name="traceContextAccessor">The <see cref="ITraceContextAccessor"/> through which the <see cref="TraceContext"/> will be set.</param>
public TraceContextFactory(ITraceContextAccessor traceContextAccessor)
{
this.traceContextAccessor = traceContextAccessor;
}
/// <inheritdoc />
public TraceContext Create(string traceId, string header)
{
var traceContext = new TraceContext(traceId, header);
if (traceContextAccessor != null)
{
traceContextAccessor.TraceContext = traceContext;
}
return traceContext;
}
/// <inheritdoc />
public void Dispose()
{
if (traceContextAccessor != null)
{
traceContextAccessor.TraceContext = null;
}
}
}
}