|
17 | 17 | namespace Microsoft.Extensions.Logging;
|
18 | 18 |
|
19 | 19 | /// <summary>
|
20 |
| -/// Lets you register log buffers in a dependency injection container. |
| 20 | +/// Lets you register HTTP request log buffering in a dependency injection container. |
21 | 21 | /// </summary>
|
22 | 22 | [Experimental(diagnosticId: DiagnosticIds.Experiments.Telemetry, UrlFormat = DiagnosticIds.UrlFormat)]
|
23 |
| -public static class HttpRequestBufferLoggerBuilderExtensions |
| 23 | +public static class HttpRequestBufferingLoggingBuilderExtensions |
24 | 24 | {
|
25 | 25 | /// <summary>
|
26 |
| - /// Adds HTTP request-aware buffer to the logging infrastructure. Matched logs will be buffered in |
27 |
| - /// a buffer specific to each HTTP request and can optionally be flushed and emitted during the request lifetime./>. |
| 26 | + /// Adds HTTP request log buffering to the logging infrastructure. |
28 | 27 | /// </summary>
|
29 | 28 | /// <param name="builder">The <see cref="ILoggingBuilder" />.</param>
|
30 | 29 | /// <param name="configuration">The <see cref="IConfiguration" /> to add.</param>
|
31 | 30 | /// <returns>The value of <paramref name="builder"/>.</returns>
|
32 | 31 | /// <exception cref="ArgumentNullException"><paramref name="builder"/> is <see langword="null"/>.</exception>
|
| 32 | + /// <remarks> |
| 33 | + /// Matched logs will be buffered in a buffer specific to each HTTP request and can optionally be flushed and emitted during the request lifetime. |
| 34 | + /// </remarks> |
33 | 35 | public static ILoggingBuilder AddHttpRequestBuffering(this ILoggingBuilder builder, IConfiguration configuration)
|
34 | 36 | {
|
35 | 37 | _ = Throw.IfNull(builder);
|
36 | 38 | _ = Throw.IfNull(configuration);
|
37 | 39 |
|
| 40 | + _ = builder.Services.AddSingleton<IConfigureOptions<HttpRequestLogBufferingOptions>>(new HttpRequestLogBufferingConfigureOptions(configuration)); |
| 41 | + |
38 | 42 | return builder
|
39 |
| - .AddHttpRequestBufferConfiguration(configuration) |
40 | 43 | .AddHttpRequestBufferManager()
|
41 |
| - .AddGlobalBuffer(configuration); |
| 44 | + .AddGlobalBuffering(configuration); |
42 | 45 | }
|
43 | 46 |
|
44 | 47 | /// <summary>
|
45 |
| - /// Adds HTTP request-aware buffering to the logging infrastructure. Matched logs will be buffered in |
46 |
| - /// a buffer specific to each HTTP request and can optionally be flushed and emitted during the request lifetime./>. |
| 48 | + /// Adds HTTP request log buffering to the logging infrastructure. |
47 | 49 | /// </summary>
|
48 | 50 | /// <param name="builder">The <see cref="ILoggingBuilder" />.</param>
|
49 |
| - /// <param name="level">The log level (and below) to apply the buffer to.</param> |
50 |
| - /// <param name="configure">The buffer configuration options.</param> |
| 51 | + /// <param name="configure">The buffer configuration delegate.</param> |
51 | 52 | /// <returns>The value of <paramref name="builder"/>.</returns>
|
52 | 53 | /// <exception cref="ArgumentNullException"><paramref name="builder"/> is <see langword="null"/>.</exception>
|
53 |
| - public static ILoggingBuilder AddHttpRequestBuffering(this ILoggingBuilder builder, LogLevel? level = null, Action<HttpRequestBufferOptions>? configure = null) |
| 54 | + /// <remarks> |
| 55 | + /// Matched logs will be buffered in a buffer specific to each HTTP request and can optionally be flushed and emitted during the request lifetime. |
| 56 | + /// </remarks> |
| 57 | + public static ILoggingBuilder AddHttpRequestBuffering(this ILoggingBuilder builder, Action<HttpRequestLogBufferingOptions> configure) |
54 | 58 | {
|
55 | 59 | _ = Throw.IfNull(builder);
|
| 60 | + _ = Throw.IfNull(configure); |
| 61 | + |
| 62 | + _ = builder.Services.Configure(configure); |
56 | 63 |
|
57 |
| - _ = builder.Services |
58 |
| - .Configure<HttpRequestBufferOptions>(options => options.Rules.Add(new BufferFilterRule(null, level, null, null))) |
59 |
| - .Configure(configure ?? new Action<HttpRequestBufferOptions>(_ => { })); |
| 64 | + HttpRequestLogBufferingOptions options = new HttpRequestLogBufferingOptions(); |
| 65 | + configure(options); |
60 | 66 |
|
61 | 67 | return builder
|
62 | 68 | .AddHttpRequestBufferManager()
|
63 |
| - .AddGlobalBuffer(level); |
| 69 | + .AddGlobalBuffering(opts => opts.Rules = options.Rules); |
64 | 70 | }
|
65 | 71 |
|
66 | 72 | /// <summary>
|
67 |
| - /// Adds HTTP request buffer provider to the logging infrastructure. |
| 73 | + /// Adds HTTP request log buffering to the logging infrastructure. |
68 | 74 | /// </summary>
|
69 | 75 | /// <param name="builder">The <see cref="ILoggingBuilder" />.</param>
|
70 |
| - /// <returns>The <see cref="ILoggingBuilder"/> so that additional calls can be chained.</returns> |
| 76 | + /// <param name="level">The log level (and below) to apply the buffer to.</param> |
| 77 | + /// <returns>The value of <paramref name="builder"/>.</returns> |
71 | 78 | /// <exception cref="ArgumentNullException"><paramref name="builder"/> is <see langword="null"/>.</exception>
|
72 |
| - internal static ILoggingBuilder AddHttpRequestBufferManager(this ILoggingBuilder builder) |
| 79 | + /// <remarks> |
| 80 | + /// Matched logs will be buffered in a buffer specific to each HTTP request and can optionally be flushed and emitted during the request lifetime. |
| 81 | + /// </remarks> |
| 82 | + public static ILoggingBuilder AddHttpRequestBuffering(this ILoggingBuilder builder, LogLevel? level = null) |
73 | 83 | {
|
74 | 84 | _ = Throw.IfNull(builder);
|
75 | 85 |
|
76 |
| - builder.Services.TryAddScoped<HttpRequestBufferHolder>(); |
77 |
| - builder.Services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>(); |
78 |
| - builder.Services.TryAddSingleton<HttpRequestBufferManager>(); |
79 |
| - builder.Services.TryAddSingleton<IBufferManager>(static sp => sp.GetRequiredService<HttpRequestBufferManager>()); |
80 |
| - builder.Services.TryAddSingleton<IHttpRequestBufferManager>(static sp => sp.GetRequiredService<HttpRequestBufferManager>()); |
| 86 | + _ = builder.Services.Configure<HttpRequestLogBufferingOptions>(options => options.Rules.Add(new LogBufferingFilterRule { LogLevel = level })); |
81 | 87 |
|
82 |
| - return builder; |
| 88 | + return builder |
| 89 | + .AddHttpRequestBufferManager() |
| 90 | + .AddGlobalBuffering(level); |
83 | 91 | }
|
84 | 92 |
|
85 |
| - /// <summary> |
86 |
| - /// Configures <see cref="HttpRequestBufferOptions" /> from an instance of <see cref="IConfiguration" />. |
87 |
| - /// </summary> |
88 |
| - /// <param name="builder">The <see cref="ILoggingBuilder" />.</param> |
89 |
| - /// <param name="configuration">The <see cref="IConfiguration" /> to add.</param> |
90 |
| - /// <returns>The value of <paramref name="builder"/>.</returns> |
91 |
| - /// <exception cref="ArgumentNullException"><paramref name="builder"/> is <see langword="null"/>.</exception> |
92 |
| - internal static ILoggingBuilder AddHttpRequestBufferConfiguration(this ILoggingBuilder builder, IConfiguration configuration) |
| 93 | + internal static ILoggingBuilder AddHttpRequestBufferManager(this ILoggingBuilder builder) |
93 | 94 | {
|
94 |
| - _ = Throw.IfNull(builder); |
95 |
| - |
96 |
| - _ = builder.Services.AddSingleton<IConfigureOptions<HttpRequestBufferOptions>>(new HttpRequestBufferConfigureOptions(configuration)); |
| 95 | + builder.Services.TryAddScoped<HttpRequestBufferHolder>(); |
| 96 | + builder.Services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>(); |
| 97 | + builder.Services.TryAddSingleton(sp => |
| 98 | + { |
| 99 | + var globalBufferManager = sp.GetRequiredService<GlobalBufferManager>(); |
| 100 | + return ActivatorUtilities.CreateInstance<HttpRequestBufferManager>(sp, globalBufferManager); |
| 101 | + }); |
| 102 | + builder.Services.TryAddSingleton<LogBuffer>(sp => sp.GetRequiredService<HttpRequestBufferManager>()); |
| 103 | + builder.Services.TryAddSingleton<HttpRequestLogBuffer>(sp => sp.GetRequiredService<HttpRequestBufferManager>()); |
97 | 104 |
|
98 | 105 | return builder;
|
99 | 106 | }
|
|
0 commit comments