74 lines
2.3 KiB
C#
Executable File
74 lines
2.3 KiB
C#
Executable File
using Microsoft.AspNetCore.Builder;
|
|
using Opsoft.NetCore.O3App.Domain;
|
|
using Opsoft.NetCore.O3App.Shared;
|
|
using System;
|
|
|
|
namespace Opsoft.NetCore.O3App.Infrastructure
|
|
{
|
|
/// <summary>
|
|
/// Extension methods for the TraceIdMiddleware.
|
|
/// </summary>
|
|
public static class TraceIdExtensions
|
|
{
|
|
/// <summary>
|
|
/// Enables Trace IDs for the request.
|
|
/// </summary>
|
|
/// <param name="app"></param>
|
|
/// <returns></returns>
|
|
public static IApplicationBuilder UseTraceId(this IApplicationBuilder app)
|
|
{
|
|
if (app == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(app));
|
|
}
|
|
|
|
return app.UseTraceId(new TraceIdOptions());
|
|
}
|
|
|
|
/// <summary>
|
|
/// Enables Trace IDs for the request.
|
|
/// </summary>
|
|
/// <param name="app"></param>
|
|
/// <param name="header">The header field name to use for the Trace ID.</param>
|
|
/// <returns></returns>
|
|
public static IApplicationBuilder UseTraceId(this IApplicationBuilder app, string header)
|
|
{
|
|
if (app == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(app));
|
|
}
|
|
|
|
return app.UseTraceId(new TraceIdOptions
|
|
{
|
|
Header = header
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// Enables Trace IDs for the request.
|
|
/// </summary>
|
|
/// <param name="app"></param>
|
|
/// <param name="options"></param>
|
|
/// <returns></returns>
|
|
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<TraceIdMiddleware>(Microsoft.Extensions.Options.Options.Create(options));
|
|
}
|
|
}
|
|
}
|