Skip to content

Commit 536f8b4

Browse files
committed
fix: prepend category as plain text, not a template placeholder
Copilot's review on this PR caught a real issue: prepending a {Category} placeholder to honor IncludeCategory in JSON mode makes the formatter's positional-argument detection treat the whole template as named, which can shift how a caller's own {0}/{1} placeholders line up with their values. Prepend the category as literal text instead, same as the text format already does. Updated the two category tests and added one covering a positional template to confirm it stays untouched. All 17 tests pass locally on net8.0 and net10.0.
1 parent 53fb69d commit 536f8b4

2 files changed

Lines changed: 53 additions & 9 deletions

File tree

‎Libraries/src/Amazon.Lambda.Logging.AspNetCore/LambdaILogger.cs‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,12 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
7878
if (_options.IncludeCategory)
7979
{
8080
// Unlike the text format, the JSON format otherwise drops the
81-
// category entirely, so IncludeCategory has no effect. Prepend a
82-
// "{Category}" placeholder (which the JSON formatter turns into a
83-
// queryable property) and supply the category value.
84-
messageTemplate = "[{Category}] " + messageTemplate;
85-
parameters.Insert(0, _categoryName);
81+
// category entirely, so IncludeCategory has no effect. Prepend the
82+
// category as literal text rather than a "{Category}" placeholder:
83+
// a placeholder would be picked up by the formatter's positional
84+
// argument detection and shift how the template's own {0}/{name}
85+
// placeholders get matched to the caller's arguments.
86+
messageTemplate = $"[{_categoryName}] " + messageTemplate;
8687
}
8788

8889
Amazon.Lambda.Core.LambdaLogger.Log(lambdaLogLevel, exception, messageTemplate, parameters.ToArray());

‎Libraries/test/Amazon.Lambda.Logging.AspNetCore.Tests/LoggingTests.cs‎

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -598,10 +598,11 @@ public void TestJSONParameterLoggingIncludesCategoryWhenEnabled()
598598
logger.LogError(new Exception("Too Cheap"), "User {name} fail to by {product} for {price}", "Gilmour", "Guitar", 55.55);
599599

600600
var text = writer.ToString();
601-
// Category is prepended as a placeholder + argument, so it becomes
602-
// a queryable JSON property instead of being dropped.
603-
Assert.Contains("{Category}", text);
604-
Assert.Contains("parameter count: 4", text);
601+
// Category is prepended as literal text, not a template placeholder,
602+
// so the caller's own {name}/{product}/{price} arguments are untouched.
603+
Assert.Contains("[JSONLogging]", text);
604+
Assert.DoesNotContain("{Category}", text);
605+
Assert.Contains("parameter count: 3", text);
605606
}
606607
}
607608
finally
@@ -634,6 +635,7 @@ public void TestJSONParameterLoggingOmitsCategoryWhenDisabled()
634635
logger.LogError(new Exception("Too Cheap"), "User {name} fail to by {product} for {price}", "Gilmour", "Guitar", 55.55);
635636

636637
var text = writer.ToString();
638+
Assert.DoesNotContain("[JSONLogging]", text);
637639
Assert.DoesNotContain("{Category}", text);
638640
Assert.Contains("parameter count: 3", text);
639641
}
@@ -644,6 +646,47 @@ public void TestJSONParameterLoggingOmitsCategoryWhenDisabled()
644646
}
645647
}
646648

649+
[Fact]
650+
public void TestJSONParameterLoggingWithCategoryLeavesPositionalTemplateIntact()
651+
{
652+
// Prepending category as a "{Category}" template placeholder (the earlier approach)
653+
// would mix a named property into an otherwise all-numeric template, which the JSON
654+
// formatter's positional-argument detection relies on being all-numeric. Prepending
655+
// it as literal text instead means the caller's own "{0}"/"{1}" placeholders, and the
656+
// argument array they line up with, are passed through completely untouched.
657+
Environment.SetEnvironmentVariable("AWS_LAMBDA_LOG_FORMAT", "JSON");
658+
try
659+
{
660+
using (var writer = new StringWriter())
661+
{
662+
ConnectLoggingActionToLogger(message => writer.Write(message));
663+
664+
var configuration = new ConfigurationBuilder()
665+
.AddJsonFile(GetAppSettingsPath("appsettings.json"))
666+
.Build();
667+
668+
var loggerOptions = new LambdaLoggerOptions(configuration);
669+
loggerOptions.IncludeCategory = true;
670+
var loggerFactory = new TestLoggerFactory()
671+
.AddLambdaLogger(loggerOptions);
672+
673+
var logger = loggerFactory.CreateLogger("JSONLogging");
674+
675+
logger.LogInformation("{0} scored {1}", "Alice", 42);
676+
677+
var text = writer.ToString();
678+
Assert.Contains("[JSONLogging]", text);
679+
Assert.Contains("{0} scored {1}", text);
680+
Assert.DoesNotContain("{Category}", text);
681+
Assert.Contains("parameter count: 2", text);
682+
}
683+
}
684+
finally
685+
{
686+
Environment.SetEnvironmentVariable("AWS_LAMBDA_LOG_FORMAT", null);
687+
}
688+
}
689+
647690
[Fact]
648691
public void JsonLoggingWithNoOriginalFormat()
649692
{

0 commit comments

Comments
 (0)