Skip to content

Commit 32bb34c

Browse files
HardBlock729claude
andcommitted
refactor(logging): simplify AddG3Logging to direct AddJsonConsole call
Remove the add-then-clear hack. RateLimitedLoggerProvider stays as an opt-in class but is no longer wired in by default. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent d475b0c commit 32bb34c

1 file changed

Lines changed: 5 additions & 20 deletions

File tree

G3.AspNetCore.Core/Extensions/LoggingExtensions.cs

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
using G3.AspNetCore.Core.Logging;
21
using Microsoft.AspNetCore.Builder;
3-
using Microsoft.Extensions.DependencyInjection;
42
using Microsoft.Extensions.Hosting;
53
using Microsoft.Extensions.Logging;
6-
using Microsoft.Extensions.Logging.Console;
74
using System.Text.Json;
85

96
namespace G3.AspNetCore.Core.Extensions;
@@ -14,32 +11,20 @@ namespace G3.AspNetCore.Core.Extensions;
1411
public static class LoggingExtensions
1512
{
1613
/// <summary>
17-
/// Configures logging with a rate-limited JSON console provider and suppresses high-volume
18-
/// framework log categories. Outputs structured JSON per log line so CloudWatch Logs Insights
19-
/// can correlate requests by RequestId, TenantId, and other scope properties.
14+
/// Configures structured JSON console logging and suppresses high-volume framework log
15+
/// categories. Outputs one JSON object per log line so CloudWatch Logs Insights can
16+
/// correlate requests by RequestId, TenantId, and other scope properties.
2017
/// </summary>
2118
public static WebApplicationBuilder AddG3Logging(this WebApplicationBuilder builder)
2219
{
23-
// Register JSON formatter and configure options (AddJsonConsole also adds a provider,
24-
// which we remove below so we can wrap it in the rate limiter).
20+
builder.Logging.ClearProviders();
21+
2522
builder.Logging.AddJsonConsole(options =>
2623
{
2724
options.IncludeScopes = true;
2825
options.JsonWriterOptions = new JsonWriterOptions { Indented = false };
2926
});
3027

31-
// Remove the provider AddJsonConsole registered; keep the formatter + options.
32-
builder.Logging.ClearProviders();
33-
34-
// Re-add as a rate-limited wrapper around ConsoleLoggerProvider.
35-
builder.Logging.Services.AddSingleton<ILoggerProvider>(sp =>
36-
{
37-
var consoleProvider = new ConsoleLoggerProvider(
38-
sp.GetRequiredService<Microsoft.Extensions.Options.IOptionsMonitor<ConsoleLoggerOptions>>(),
39-
sp.GetServices<ConsoleFormatter>());
40-
return new RateLimitedLoggerProvider(consoleProvider);
41-
});
42-
4328
builder.Logging.SetMinimumLevel(
4429
builder.Environment.IsProduction()
4530
? LogLevel.Warning

0 commit comments

Comments
 (0)