Skip to content

Commit c834422

Browse files
author
Sviatoslav Bychkov
committed
Fixed providing message value with escape sequences to the attribute parameter
1 parent 321ed29 commit c834422

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Microsoft.Extensions.Logging.AutoLoggerMessage
2+
{
3+
static partial class AutoLoggerMessage
4+
{
5+
// <LogCallMappingLocation>: Guid_1
6+
[Microsoft.Extensions.Logging.LoggerMessageAttribute(Level = Microsoft.Extensions.Logging.LogLevel.Trace, Message = "All characters should be passed as a string literal expression: \n\r\t", SkipEnabledCheck = false)]
7+
internal static partial void Log_SomeNamespaceSomeClass_3_33(Microsoft.Extensions.Logging.ILogger Logger, int @intParam, string @stringParam, bool @boolParam, SomeClass @classParam, SomeStruct @structParam);
8+
}
9+
}

src/AutoLoggerMessageGenerator.UnitTests/VirtualLoggerMessage/VirtualLoggerMessageClassBuilderTests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,26 @@ await Verify(syntaxTree)
5858
.ScrubInlineGuids();
5959
}
6060

61+
[Fact]
62+
public async Task Build_WithEscapeSequences_ShouldBuildAsItIs()
63+
{
64+
var sut = new VirtualLoggerMessageClassBuilder(default);
65+
66+
var result = sut.Build([
67+
_logCall with
68+
{
69+
Id = Guid.NewGuid(),
70+
Location = new LogCallLocation("path/to/another/file.cs", 3, 33, Location.None),
71+
Message = "All characters should be passed as a string literal expression: \n\r\t",
72+
LogLevel = "Trace"
73+
}
74+
]);
75+
76+
var syntaxTree = (await result.SyntaxTree.GetRootAsync()).NormalizeWhitespace().ToFullString();
77+
78+
await Verify(syntaxTree).ScrubInlineGuids();
79+
}
80+
6181
public static TheoryData<object, bool> TestConfigurations =
6282
new()
6383
{

src/AutoLoggerMessageGenerator/VirtualLoggerMessage/VirtualLoggerMessageClassBuilder.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ private AttributeListSyntax GenerateLoggerMessageAttribute(LogCall logCall)
8181
ParseExpression(
8282
$"Level = {Constants.DefaultLoggingNamespace}.LogLevel.{logCall.LogLevel}")),
8383
AttributeArgument(
84-
ParseExpression($"Message = \"{logCall.Message}\"")),
84+
AssignmentExpression(SyntaxKind.SimpleAssignmentExpression,
85+
IdentifierName("Message"),
86+
LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(logCall.Message)))),
8587
AttributeArgument(
8688
ParseExpression(
8789
$"SkipEnabledCheck = {configuration.GenerateSkipEnabledCheck.ToLowerBooleanString()}"))

0 commit comments

Comments
 (0)