Skip to content

Commit 73ddf6e

Browse files
authored
Logging Source Generator - Adds support to @ signed prefixed parameters (#64663)
* Adds support to `@` signed prefixed parameters Fixes #60968 * Move repetitive logic to a new property * Remove NeedsAtSign
1 parent c765925 commit 73ddf6e

3 files changed

Lines changed: 27 additions & 5 deletions

File tree

src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/LoggerMessageGenerator.Emitter.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ private void GenFieldAssignments(LoggerMethod lm, string nestedIndentation)
206206
{
207207
foreach (LoggerParameter p in lm.TemplateParameters)
208208
{
209-
_builder.AppendLine($" {nestedIndentation}this._{p.Name} = {p.Name};");
209+
_builder.AppendLine($" {nestedIndentation}this._{p.Name} = {p.CodeName};");
210210
}
211211
}
212212

@@ -265,7 +265,7 @@ private void GenCallbackArguments(LoggerMethod lm)
265265
{
266266
foreach (LoggerParameter p in lm.TemplateParameters)
267267
{
268-
_builder.Append($"{p.Name}, ");
268+
_builder.Append($"{p.CodeName}, ");
269269
}
270270
}
271271

@@ -323,7 +323,7 @@ private void GenParameters(LoggerMethod lm)
323323
{
324324
_builder.Append($"{p.Qualifier} ");
325325
}
326-
_builder.Append($"{p.Type} {p.Name}");
326+
_builder.Append($"{p.Type} {p.CodeName}");
327327
}
328328
}
329329

@@ -341,7 +341,7 @@ private void GenArguments(LoggerMethod lm)
341341
_builder.Append(", ");
342342
}
343343

344-
_builder.Append($"{p.Type} {p.Name}");
344+
_builder.Append($"{p.Type} {p.CodeName}");
345345
}
346346
}
347347

@@ -357,7 +357,7 @@ private void GenHolder(LoggerMethod lm)
357357
_builder.Append(", ");
358358
}
359359

360-
_builder.Append(p.Name);
360+
_builder.Append(p.CodeName);
361361
}
362362

363363
_builder.Append(')');

src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/LoggerMessageGenerator.Parser.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,15 @@ public IReadOnlyList<LoggerClass> GetLogClasses(IEnumerable<ClassDeclarationSynt
306306
foreach (IParameterSymbol paramSymbol in methodSymbol.Parameters)
307307
{
308308
string paramName = paramSymbol.Name;
309+
bool needsAtSign = false;
310+
if (paramSymbol.DeclaringSyntaxReferences.Length > 0)
311+
{
312+
ParameterSyntax paramSyntax = paramSymbol.DeclaringSyntaxReferences[0].GetSyntax(_cancellationToken) as ParameterSyntax;
313+
if (paramSyntax != null && !string.IsNullOrEmpty(paramSyntax.Identifier.Text))
314+
{
315+
needsAtSign = paramSyntax.Identifier.Text[0] == '@';
316+
}
317+
}
309318
if (string.IsNullOrWhiteSpace(paramName))
310319
{
311320
// semantic problem, just bail quietly
@@ -339,6 +348,7 @@ public IReadOnlyList<LoggerClass> GetLogClasses(IEnumerable<ClassDeclarationSynt
339348
Name = paramName,
340349
Type = typeName,
341350
Qualifier = qualifier,
351+
CodeName = needsAtSign ? "@" + paramName : paramName,
342352
IsLogger = !foundLogger && IsBaseOrIdentity(paramTypeSymbol!, loggerSymbol),
343353
IsException = !foundException && IsBaseOrIdentity(paramTypeSymbol!, exceptionSymbol),
344354
IsLogLevel = !foundLogLevel && IsBaseOrIdentity(paramTypeSymbol!, logLevelSymbol),
@@ -739,6 +749,7 @@ internal class LoggerParameter
739749
{
740750
public string Name = string.Empty;
741751
public string Type = string.Empty;
752+
public string CodeName = string.Empty;
742753
public string? Qualifier;
743754
public bool IsLogger;
744755
public bool IsException;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace Microsoft.Extensions.Logging.Generators.Tests.TestClasses
5+
{
6+
internal static partial class AtSymbolTestExtensions
7+
{
8+
[LoggerMessage(EventId = 0, Level = LogLevel.Information, Message = "M0 {event}")]
9+
internal static partial void M0(ILogger logger, string @event);
10+
}
11+
}

0 commit comments

Comments
 (0)