G-Research / fsharp-analyzers

Analyzers for F#
https://g-research.github.io/fsharp-analyzers/
Apache License 2.0
14 stars 1 forks source link

Interpolated string in log functions #49

Open nojaf opened 6 months ago

nojaf commented 6 months ago

Related to https://g-research.github.io/fsharp-analyzers/analyzers/LoggingArgFuncNotFullyAppliedAnalyzer.html Would it be interesting to also have an analyzer that detects a log call with a single interpolated string? Instead of using the log message template.

let p = 42

// ❌
logger.Log(LogLevel.Information, $"foo %i{p}")

// ✅
logger.Log(LogLevel.Information, "foo {p}", p)

Would do you think @Smaug123?

Smaug123 commented 6 months ago

Interesting - probably yes, but one I would prefer to have would be "not all templated args have been given values".

nojaf commented 6 months ago

That would be something like warn for logger.Log(LogLevel.Information, "foo {p} {x}", p) because there is no value for x?

Smaug123 commented 6 months ago

That would be something like warn for logger.Log(LogLevel.Information, "foo {p} {x}", p) because there is no value for x?

Yes, exactly.

Numpsy commented 6 months ago

There's a whole family of these sort of things in NetAnalyers, e.g. https://learn.microsoft.com/en-gb/dotnet/fundamentals/code-analysis/quality-rules/ca2254 https://learn.microsoft.com/en-gb/dotnet/fundamentals/code-analysis/quality-rules/ca2017 https://learn.microsoft.com/en-gb/dotnet/fundamentals/code-analysis/quality-rules/ca2253

Also - some related things in this Serilog Analyzer - https://github.com/Suchiman/SerilogAnalyzer#serilog003-property-binding-verifier

(I don't know if there's any general view on porting Roslyn analyzers, or how such things should be grouped.)