using Microsoft.AspNetCore.Builder;
using Opsoft.NetCore.O3App.Domain;
using Opsoft.NetCore.O3App.Shared;
using System;
namespace Opsoft.NetCore.O3App.Infrastructure
{
///
/// Extension methods for the TraceIdMiddleware.
///
public static class TraceIdExtensions
{
///
/// Enables Trace IDs for the request.
///
///
///
public static IApplicationBuilder UseTraceId(this IApplicationBuilder app)
{
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
return app.UseTraceId(new TraceIdOptions());
}
///
/// Enables Trace IDs for the request.
///
///
/// The header field name to use for the Trace ID.
///
public static IApplicationBuilder UseTraceId(this IApplicationBuilder app, string header)
{
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
return app.UseTraceId(new TraceIdOptions
{
Header = header
});
}
///
/// Enables Trace IDs for the request.
///
///
///
///
public static IApplicationBuilder UseTraceId(this IApplicationBuilder app, TraceIdOptions options)
{
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
if (app.ApplicationServices.GetService(typeof(ITraceContextFactory)) == null)
{
throw new InvalidOperationException("Unable to find the required services. You must call the AddTraceId method in ConfigureServices in the application startup code.");
}
return app.UseMiddleware(Microsoft.Extensions.Options.Options.Create(options));
}
}
}