-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathHttpContextExtensions.cs
More file actions
27 lines (24 loc) · 1.13 KB
/
HttpContextExtensions.cs
File metadata and controls
27 lines (24 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Serilog.Preparers.CorrelationIds;
namespace Serilog.Enrichers;
/// <summary>
/// Extension methods for <see cref="HttpContext" /> to access enriched values.
/// </summary>
public static class HttpContextExtensions
{
/// <summary>
/// Retrieves the correlation ID value from the current HTTP context.
/// </summary>
/// <param name="httpContext">The HTTP context.</param>
/// <returns>The correlation ID as a string, or null if not available.</returns>
public static string GetCorrelationId(this HttpContext httpContext)
=> httpContext?.Items[Constants.CorrelationIdValueKey] as string;
/// <summary>
/// Retrieves the correlation ID preparer for processing the current HTTP context.
/// </summary>
/// <param name="httpContext">The HTTP context.</param>
/// <returns>Correlation ID preparer.</returns>
internal static ICorrelationIdPreparer GetCorrelationIdPreparer(this HttpContext httpContext)
=> httpContext.RequestServices.GetService<ICorrelationIdPreparer>() ?? new CorrelationIdPreparer();
}