Skip to content

Commit

Permalink
Logging Source Generator - Adds support to @ signed prefixed parame…
Browse files Browse the repository at this point in the history
…ters (dotnet#64663)

* Adds support to `@` signed prefixed parameters

Fixes dotnet#60968

* Move repetitive logic to a new property

* Remove NeedsAtSign
  • Loading branch information
maryamariyan committed Feb 3, 2022
1 parent c765925 commit 73ddf6e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,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 @@ -265,7 +265,7 @@ private void GenCallbackArguments(LoggerMethod lm)
{
foreach (LoggerParameter p in lm.TemplateParameters)
{
_builder.Append($"{p.Name}, ");
_builder.Append($"{p.CodeName}, ");
}
}

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

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

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

Expand All @@ -357,7 +357,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 @@ -306,6 +306,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 @@ -339,6 +348,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 @@ -739,6 +749,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);
}
}

0 comments on commit 73ddf6e

Please sign in to comment.