Skip to content

fix CA2254: Template should be a static expression #1048

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 21 commits into
base: main
Choose a base branch
from

Conversation

SimonCropp
Copy link
Contributor

No description provided.

@SimonCropp SimonCropp requested a review from a team as a code owner May 13, 2025 04:30
@DaveTryon
Copy link
Contributor

I've read the page at https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca2254, which explains the rule and how to apply it. It fails, however, to explain the reasoning behind the rule or what benefit it provides. I could imagine a benefit if we were planning to localize, but since that's not on any roadmap, CA2254 seems pointless here.

My objections are to the changes are twofold:

  1. It requires more dev effort to mentally combine the formatting string and the parameters into what the output should look like. String interpolation makes this simple
  2. It introduces a potential for the formatting string and the parameters to get out of sync. String interpolation avoids this problem.

I'd love to hear any concrete benefits CA2254 would bring to this project, then we can make a decision based on the merit of those benefits when weighed with the points I listed above.

@KalleOlaviNiemitalo
Copy link

I thought CA2254 only fires when methods of Microsoft.Extensions.Logging.LoggerExtensions are called with non-constant message templates. This code seems to be calling methods of Serilog.ILogger instead. How can it even get any CA2254 warnings?

@SimonCropp
Copy link
Contributor Author

if you use interpolation, it means the logger cant tokenize the values.

so for example

logger.Information($"No namespace URI base provided, using unique generated default value {defaultNamespaceUriBase}");

the logger treats it as single string

but with

logger.Information("No namespace URI base provided, using unique generated default value {DefaultNamespaceUriBase}", defaultNamespaceUriBase);

the defaultNamespaceUriBase is extracted as a key value, so in the underlying logging infrastructure you can search and filers on the key defaultNamespaceUriBase or the value of that key

@SimonCropp
Copy link
Contributor Author

here is an example tokenized log message in seq https://datalust.co/seq

contextualLog

u can see that the Handler property is tokenized out. if that log message had instead used interpolation, you would not have Handler as an extracted property

@SimonCropp
Copy link
Contributor Author

should i resolve the conflicts?

@pragnya17
Copy link
Contributor

should i resolve the conflicts?

Yes, please resolve the conflicts when you can

SimonCropp and others added 18 commits June 26, 2025 23:00
Co-authored-by: Pragnya <59893188+pragnya17@users.noreply.github.com>
Co-authored-by: Pragnya <59893188+pragnya17@users.noreply.github.com>
…getUtils.cs

Co-authored-by: Pragnya <59893188+pragnya17@users.noreply.github.com>
…byGemsUtils.cs

Co-authored-by: Pragnya <59893188+pragnya17@users.noreply.github.com>
…onverter.cs

Co-authored-by: Pragnya <59893188+pragnya17@users.noreply.github.com>
…er.cs

Co-authored-by: Pragnya <59893188+pragnya17@users.noreply.github.com>
Co-authored-by: Pragnya <59893188+pragnya17@users.noreply.github.com>
Co-authored-by: Pragnya <59893188+pragnya17@users.noreply.github.com>
Co-authored-by: Pragnya <59893188+pragnya17@users.noreply.github.com>
Co-authored-by: Pragnya <59893188+pragnya17@users.noreply.github.com>
Co-authored-by: Pragnya <59893188+pragnya17@users.noreply.github.com>
…venUtils.cs

Co-authored-by: Pragnya <59893188+pragnya17@users.noreply.github.com>
Co-authored-by: Pragnya <59893188+pragnya17@users.noreply.github.com>
Co-authored-by: Pragnya <59893188+pragnya17@users.noreply.github.com>
Co-authored-by: Pragnya <59893188+pragnya17@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants