Skip to content

Commit d475b0c

Browse files
HardBlock729claude
andcommitted
feat(logging): switch to JSON console output for structured CloudWatch logs
AddG3Logging now uses AddJsonConsole (IncludeScopes=true, compact) so every log line is a JSON object. CloudWatch Logs Insights can filter/correlate by RequestId, TenantId, and other scope properties. Rate-limited provider preserved by wrapping the ConsoleLoggerProvider after clearing providers. CI workflow updated to trigger on both main and master branches. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent f22c164 commit d475b0c

3 files changed

Lines changed: 20 additions & 11 deletions

File tree

.github/workflows/ci.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ name: CI
33
on:
44
workflow_dispatch:
55
push:
6-
branches: ["main"]
6+
branches: ["main", "master"]
77
pull_request:
8-
branches: ["main"]
8+
branches: ["main", "master"]
99

1010
env:
1111
DOTNET_SKIP_FIRST_TIME_EXPERIENCE: true
@@ -50,7 +50,7 @@ jobs:
5050
nuget:
5151
needs: test
5252
runs-on: ubuntu-latest
53-
if: github.ref == 'refs/heads/main' || github.event_name == 'workflow_dispatch'
53+
if: github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master' || github.event_name == 'workflow_dispatch'
5454
steps:
5555
- uses: actions/setup-dotnet@v5
5656
with:

G3.AspNetCore.Core/Extensions/LoggingExtensions.cs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Microsoft.Extensions.Hosting;
55
using Microsoft.Extensions.Logging;
66
using Microsoft.Extensions.Logging.Console;
7+
using System.Text.Json;
78

89
namespace G3.AspNetCore.Core.Extensions;
910

@@ -13,24 +14,32 @@ namespace G3.AspNetCore.Core.Extensions;
1314
public static class LoggingExtensions
1415
{
1516
/// <summary>
16-
/// Configures logging with a rate-limited console provider and suppresses high-volume
17-
/// framework log categories. Minimizes log volume in production to reduce costs on
18-
/// paid logging services (e.g. CloudWatch).
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.
1920
/// </summary>
2021
public static WebApplicationBuilder AddG3Logging(this WebApplicationBuilder builder)
2122
{
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).
25+
builder.Logging.AddJsonConsole(options =>
26+
{
27+
options.IncludeScopes = true;
28+
options.JsonWriterOptions = new JsonWriterOptions { Indented = false };
29+
});
30+
31+
// Remove the provider AddJsonConsole registered; keep the formatter + options.
2232
builder.Logging.ClearProviders();
2333

34+
// Re-add as a rate-limited wrapper around ConsoleLoggerProvider.
2435
builder.Logging.Services.AddSingleton<ILoggerProvider>(sp =>
2536
{
2637
var consoleProvider = new ConsoleLoggerProvider(
27-
sp.GetRequiredService<Microsoft.Extensions.Options.IOptionsMonitor<ConsoleLoggerOptions>>());
38+
sp.GetRequiredService<Microsoft.Extensions.Options.IOptionsMonitor<ConsoleLoggerOptions>>(),
39+
sp.GetServices<ConsoleFormatter>());
2840
return new RateLimitedLoggerProvider(consoleProvider);
2941
});
3042

31-
builder.Services.Configure<SimpleConsoleFormatterOptions>(options =>
32-
options.IncludeScopes = true);
33-
3443
builder.Logging.SetMinimumLevel(
3544
builder.Environment.IsProduction()
3645
? LogLevel.Warning

G3.AspNetCore.Core/G3.AspNetCore.Core.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<GenerateDocumentationFile>True</GenerateDocumentationFile>
77
<NoWarn>$(NoWarn);1591</NoWarn>
88
<PackageId>G3Software.Net.AspNetCore.Core</PackageId>
9-
<Version>1.0.2</Version>
9+
<Version>1.1.0</Version>
1010
<Authors>G3Software</Authors>
1111
<Company>G3Software</Company>
1212
<Title>Core ASP.NET Core helpers</Title>

0 commit comments

Comments
 (0)