-
Notifications
You must be signed in to change notification settings - Fork 729
/
ApplicationEnricherServiceCollectionExtensions.cs
78 lines (67 loc) · 3.33 KB
/
ApplicationEnricherServiceCollectionExtensions.cs
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Diagnostics.Enrichment;
using Microsoft.Shared.Diagnostics;
namespace Microsoft.Extensions.DependencyInjection;
/// <summary>
/// Provides extension methods for setting up the service enrichers in an <see cref="IServiceCollection" />.
/// </summary>
public static class ApplicationEnricherServiceCollectionExtensions
{
/// <summary>
/// Adds an instance of the service enricher to the <see cref="IServiceCollection"/>.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> to add the service enricher to.</param>
/// <returns>The value of <paramref name="services"/>.</returns>
/// <exception cref="ArgumentNullException"><paramref name="services"/> is <see langword="null"/>.</exception>
public static IServiceCollection AddServiceLogEnricher(this IServiceCollection services)
{
_ = Throw.IfNull(services);
return services
.AddServiceLogEnricher(_ => { });
}
/// <summary>
/// Adds an instance of the service enricher to the <see cref="IServiceCollection"/>.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> to add the service enricher to.</param>
/// <param name="configure">The <see cref="ApplicationLogEnricherOptions"/> configuration delegate.</param>
/// <returns>The value of <paramref name="services"/>.</returns>
/// <exception cref="ArgumentNullException">Any of the arguments is <see langword="null"/>.</exception>
public static IServiceCollection AddServiceLogEnricher(this IServiceCollection services, Action<ApplicationLogEnricherOptions> configure)
{
_ = Throw.IfNull(services);
_ = Throw.IfNull(configure);
return services
.AddStaticLogEnricher<ApplicationLogEnricher>()
.AddLogEnricherOptions(configure);
}
/// <summary>
/// Adds an instance of the service enricher to the <see cref="IServiceCollection"/>.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> to add the service enricher to.</param>
/// <param name="section">The <see cref="IConfigurationSection"/> to use for configuring <see cref="ApplicationLogEnricherOptions"/> in the service enricher.</param>
/// <returns>The value of <paramref name="services"/>.</returns>
/// <exception cref="ArgumentNullException">Any of the arguments is <see langword="null"/>.</exception>
public static IServiceCollection AddServiceLogEnricher(this IServiceCollection services, IConfigurationSection section)
{
_ = Throw.IfNull(services);
_ = Throw.IfNull(section);
return services
.AddStaticLogEnricher<ApplicationLogEnricher>()
.AddLogEnricherOptions(_ => { }, section);
}
private static IServiceCollection AddLogEnricherOptions(
this IServiceCollection services,
Action<ApplicationLogEnricherOptions> configure,
IConfigurationSection? section = null)
{
_ = services.Configure(configure);
if (section is not null)
{
_ = services.Configure<ApplicationLogEnricherOptions>(section);
}
return services;
}
}