Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Logging Source Generator - Adds support to @ signed prefixed parame…
…ters (#64663)

* Adds support to `@` signed prefixed parameters

Fixes #60968

* Move repetitive logic to a new property

* Remove NeedsAtSign
  • Loading branch information
maryamariyan committed Feb 4, 2022
commit cb38fc7df45476f8f36ff8714e7b7cab3c066616
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ private void GenFieldAssignments(LoggerMethod lm, string nestedIndentation)
{
foreach (LoggerParameter p in lm.TemplateParameters)
{
_builder.AppendLine($" {nestedIndentation}this._{p.Name} = {p.Name};");
_builder.AppendLine($" {nestedIndentation}this._{p.Name} = {p.CodeName};");
}
}

Expand Down Expand Up @@ -255,7 +255,7 @@ private void GenCallbackArguments(LoggerMethod lm)
{
foreach (LoggerParameter p in lm.TemplateParameters)
{
_builder.Append($"{p.Name}, ");
_builder.Append($"{p.CodeName}, ");
}
}

Expand Down Expand Up @@ -313,7 +313,7 @@ private void GenParameters(LoggerMethod lm)
{
_builder.Append($"{p.Qualifier} ");
}
_builder.Append($"{p.Type} {p.Name}");
_builder.Append($"{p.Type} {p.CodeName}");
}
}

Expand All @@ -331,7 +331,7 @@ private void GenArguments(LoggerMethod lm)
_builder.Append(", ");
}

_builder.Append($"{p.Type} {p.Name}");
_builder.Append($"{p.Type} {p.CodeName}");
}
}

Expand All @@ -347,7 +347,7 @@ private void GenHolder(LoggerMethod lm)
_builder.Append(", ");
}

_builder.Append(p.Name);
_builder.Append(p.CodeName);
}

_builder.Append(')');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,15 @@ public IReadOnlyList<LoggerClass> GetLogClasses(IEnumerable<ClassDeclarationSynt
foreach (IParameterSymbol paramSymbol in methodSymbol.Parameters)
{
string paramName = paramSymbol.Name;
bool needsAtSign = false;
if (paramSymbol.DeclaringSyntaxReferences.Length > 0)
{
ParameterSyntax paramSyntax = paramSymbol.DeclaringSyntaxReferences[0].GetSyntax(_cancellationToken) as ParameterSyntax;
if (paramSyntax != null && !string.IsNullOrEmpty(paramSyntax.Identifier.Text))
{
needsAtSign = paramSyntax.Identifier.Text[0] == '@';
}
}
if (string.IsNullOrWhiteSpace(paramName))
{
// semantic problem, just bail quietly
Expand Down Expand Up @@ -341,6 +350,7 @@ public IReadOnlyList<LoggerClass> GetLogClasses(IEnumerable<ClassDeclarationSynt
Name = paramName,
Type = typeName,
Qualifier = qualifier,
CodeName = needsAtSign ? "@" + paramName : paramName,
IsLogger = !foundLogger && IsBaseOrIdentity(paramTypeSymbol!, loggerSymbol),
IsException = !foundException && IsBaseOrIdentity(paramTypeSymbol!, exceptionSymbol),
IsLogLevel = !foundLogLevel && IsBaseOrIdentity(paramTypeSymbol!, logLevelSymbol),
Expand Down Expand Up @@ -719,6 +729,7 @@ internal class LoggerParameter
{
public string Name = string.Empty;
public string Type = string.Empty;
public string CodeName = string.Empty;
public string? Qualifier;
public bool IsLogger;
public bool IsException;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.Extensions.Logging.Generators.Tests.TestClasses
{
internal static partial class AtSymbolTestExtensions
{
[LoggerMessage(EventId = 0, Level = LogLevel.Information, Message = "M0 {event}")]
internal static partial void M0(ILogger logger, string @event);
}
}