/
ProcessEnricherServiceCollectionExtensions.cs
80 lines (69 loc) · 3.4 KB
/
ProcessEnricherServiceCollectionExtensions.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
79
80
// 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 Process enrichers in an <see cref="IServiceCollection" />.
/// </summary>
public static class ProcessEnricherServiceCollectionExtensions
{
/// <summary>
/// Adds an instance of the process enricher to the <see cref="IServiceCollection"/>.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> to add the process 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 AddProcessLogEnricher(this IServiceCollection services)
{
_ = Throw.IfNull(services);
return services
.AddProcessLogEnricher(_ => { });
}
/// <summary>
/// Adds an instance of the process enricher to the <see cref="IServiceCollection"/>.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> to add the process enricher to.</param>
/// <param name="configure">The <see cref="ProcessLogEnricherOptions"/> 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 AddProcessLogEnricher(this IServiceCollection services, Action<ProcessLogEnricherOptions> configure)
{
_ = Throw.IfNull(services);
_ = Throw.IfNull(configure);
return services
.AddLogEnricher<ProcessLogEnricher>()
.AddStaticLogEnricher<StaticProcessLogEnricher>()
.AddLogEnricherOptions(configure);
}
/// <summary>
/// Adds an instance of the host enricher to the <see cref="IServiceCollection"/>.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> to add the process enricher to.</param>
/// <param name="section">The <see cref="IConfigurationSection"/> to use for configuring <see cref="ProcessLogEnricherOptions"/> in the process 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 AddProcessLogEnricher(this IServiceCollection services, IConfigurationSection section)
{
_ = Throw.IfNull(services);
_ = Throw.IfNull(section);
return services
.AddLogEnricher<ProcessLogEnricher>()
.AddStaticLogEnricher<StaticProcessLogEnricher>()
.AddLogEnricherOptions(_ => { }, section);
}
private static IServiceCollection AddLogEnricherOptions(
this IServiceCollection services,
Action<ProcessLogEnricherOptions> configure,
IConfigurationSection? section = null)
{
_ = services.Configure(configure);
if (section is not null)
{
_ = services.Configure<ProcessLogEnricherOptions>(section);
}
return services;
}
}