-
Notifications
You must be signed in to change notification settings - Fork 0
/
LoggingFormatBuilder.cs
114 lines (105 loc) · 4.19 KB
/
LoggingFormatBuilder.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
namespace Microsoft.Extensions.Logging.Formatting;
/// <summary>
/// A component to configure logging providers using formatted log entries.
/// </summary>
/// <typeparam name="TFormat">The formatted entry type.</typeparam>
public class LoggingFormatBuilder<TFormat> : ILoggingBuilder
{
internal LoggingFormatBuilder(IServiceCollection services)
{
this.Services = services;
}
/// <inheritdoc/>
public IServiceCollection Services { get; }
/// <summary>
/// Configures formatting using the log category, level, and event ID.
/// </summary>
/// <param name="action">A delegate to format the entry.</param>
/// <returns>The same builder, for chaining.</returns>
public LoggingFormatBuilder<TFormat> OnEntry(Action<TFormat, string, LogLevel, EventId> action)
{
this.AddFormatter(new LogFieldFormatter<TFormat>(action));
return this;
}
/// <summary>
/// Configures formatting using the log message.
/// </summary>
/// <param name="action">A delegate to format the entry.</param>
/// <returns>The same builder, for chaining.</returns>
public LoggingFormatBuilder<TFormat> OnMessage(Action<TFormat, string> action)
{
this.AddFormatter(new LogMessageFormatter<TFormat>(action));
return this;
}
/// <summary>
/// Configures formatting using a logged exception.
/// </summary>
/// <param name="action">A delegate to format the entry.</param>
/// <param name="recursive">Whether to format inner exceptions.</param>
/// <returns>The same builder, for chaining.</returns>
public LoggingFormatBuilder<TFormat> OnException(Action<TFormat, Exception> action, bool recursive = false)
{
this.AddFormatter(new LogExceptionFormatter<TFormat>(action, recursive));
return this;
}
/// <summary>
/// Configures formatting using a specific logged exception.
/// </summary>
/// <typeparam name="T">The logged exception type.</typeparam>
/// <param name="action">A delegate to format the entry.</param>
/// <param name="recursive">Whether to format inner exceptions.</param>
/// <returns>The same builder, for chaining.</returns>
public LoggingFormatBuilder<TFormat> OnException<T>(Action<TFormat, T> action, bool recursive = false)
where T : Exception
{
return this.OnException(
(entry, exception) =>
{
if (exception is T match)
{
action(entry, match);
}
},
recursive);
}
/// <summary>
/// Configures formatting using a named log property (e.g. message template argument).
/// </summary>
/// <param name="action">A delegate to format the entry.</param>
/// <param name="category">
/// A pattern to filter properties by category. Supports prefix and wildcard ('*') matches.
/// </param>
/// <returns>The same builder, for chaining.</returns>
public LoggingFormatBuilder<TFormat> OnProperty(
Action<TFormat, KeyValuePair<string, object?>> action,
string category = "*")
{
this.AddFormatter(new LogPropertyFormatter<TFormat>(action, category));
return this;
}
/// <summary>
/// Configures formatting using a specific named log property (e.g. message template argument).
/// </summary>
/// <typeparam name="TCategory">
/// The type corresponding to the log category of the property (e.g. <see cref="ILogger{TCategoryName}"/>).
/// </typeparam>
/// <param name="name">The log property name</param>
/// <param name="action">A delegate to format the entry.</param>
/// <returns>The same builder, for chaining.</returns>
public LoggingFormatBuilder<TFormat> OnProperty<TCategory>(string name, Action<TFormat, object?> action)
{
return this.OnProperty(
(entry, kvp) =>
{
if (kvp.Key == name)
{
action(entry, kvp.Value);
}
},
LogCategory.ForType<TCategory>());
}
void AddFormatter(ILogFormatter<TFormat> formatter)
{
this.Services.Configure<LogFormatOptions<TFormat>>(x => x.Formatters.Add(formatter));
}
}