Skip to content
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

[Analyzer] Flagging issues in logger message templates w/ incomplete braces pairs #101698

Open
Kritner opened this issue Apr 29, 2024 · 4 comments
Labels
api-ready-for-review API is ready for review, it is NOT ready for implementation area-Extensions-Logging code-analyzer Marks an issue that suggests a Roslyn analyzer
Milestone

Comments

@Kritner
Copy link

Kritner commented Apr 29, 2024

Posting this issue to this repo as per the suggestion of @tarekgh

dotnet/roslyn-analyzers#7285
dotnet/roslyn-analyzers#7286

tldr: Invalid braces in a message template aren't caught by CA2017, and when encountered lead to runtime exceptions.

The PR and issues (linked and relevant snippets below) go about introducing a new analyzer CA2023 because the changes introduced in some ways change the existing "meaning" of CA2017. Additionally this seems like it should probably be a compiler error rather than warning since otherwise a runtime exception occurs - though tbf i don't recall if having too few or too many message template params lead to the same thing or not

Suggested category: Reliability (and related to) https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca2017
Suggested severity: warning (https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/configuration-options#severity-level)


Issue:

malformed message template strings for at a minimum logged messages should be throwing compiler errors IMO, rather than the current runtime errors seen with .net8.

Repro:
https://github.com/Kritner/MessageTemplateNet8

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);
builder.Services.AddLogging(loggingBuilder => loggingBuilder.AddConsole());

var host = builder.Build();

var logger = host.Services.GetRequiredService<ILogger<Program>>();

logger.LogInformation("Hello world");

var i = 5;
logger.LogInformation("My value {i}}", i);

image


I feel like this needs to be a compiler error, lest you run into the same run time errors I've encountered.

It seems this scenario is already covered with https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca2017 @Kritner could you enable that analyzer in your repo (it is enabled and warns by default, but the rule might disabled for your project) and check the diagnostics?

Yeah so it's weird... we're not NoWarning against this particular "CA2017", but we don't get the string template being flagged as a CA2017... I can easily make the CA2017 appear (and get a compiler error yay) if I change...

logger.LogInformation("My value {i}}", i);

to

logger.LogInformation("My value {i}", i, i+1);

image


More relevant comments:
dotnet/roslyn-analyzers#7285 (comment)
dotnet/roslyn-analyzers#7285 (comment)
dotnet/roslyn-analyzers#7286 (comment)

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Apr 29, 2024
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-extensions-logging
See info in area-owners.md if you want to be subscribed.

@tarekgh tarekgh added this to the Future milestone Apr 29, 2024
@tarekgh tarekgh added the code-analyzer Marks an issue that suggests a Roslyn analyzer label Apr 29, 2024
@dotnet-policy-service dotnet-policy-service bot removed the untriaged New issue has not been triaged by the area owner label Apr 29, 2024
@tarekgh
Copy link
Member

tarekgh commented Apr 29, 2024

@Kritner

Suggested severity: error (https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/configuration-options#severity-level)

Thinking loud, would it be better to have this as a warning instead of error? I recall some external logger allow mismatches.

@tarekgh tarekgh added the api-ready-for-review API is ready for review, it is NOT ready for implementation label Apr 29, 2024
@Kritner
Copy link
Author

Kritner commented Apr 29, 2024

Thinking loud, would it be better to have this as a warning instead of error? I recall some external logger allow mismatches.

I’m sure that’s fine - i don’t know offhand what constitutes one over the other, just seemed like an error since failure to correct leads to runtime exceptions

but i guess that can be up to the consuming library, the built in net logging however does experience the exception

@tarekgh
Copy link
Member

tarekgh commented Apr 29, 2024

I changed it to warning and we can discuss that in the review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-ready-for-review API is ready for review, it is NOT ready for implementation area-Extensions-Logging code-analyzer Marks an issue that suggests a Roslyn analyzer
Projects
None yet
Development

No branches or pull requests

2 participants