Repository navigation
Closed
Labels
api-approvedAPI was approved in API review, it can be implementedarea-Extensions-Loggingcode-analyzerMarks an issue that suggests a Roslyn analyzerenhancementProduct code improvement that does NOT require public API changes/additions
Milestone
Description
Scope for analyzer
With these set of analyzers the the developers would be able to use the logging APIs at runtime in a more guided way.
For example, this is related to LoggerExtensions, APIs such as BeginScope, LogTrace, LogDebug, etc. Also APIs such as LoggerMessage Define and DefineScope.
- There is no fixer for any of these diagnostics.
List of proposed diagnostics
| Diagnostic | Severity | Category | Purpose/Message |
|---|---|---|---|
| DIAG_1 | Hidden | Naming | Use PascalCase for log message tokens |
| DIAG_2 | Hidden | Performance | For improved performance, use pre-compiled log messages instead |
| DIAG_3 | Information | Usage | Numerics should not be used in logging format string |
| DIAG_4 | Information | Usage | Logging format string should not be dynamically generated |
| DIAG_5 | Warning | Reliability | Logging format string parameter count mismatch |
Usage samples:
DIAG_1: Use PascalCase for log message tokens
logger.BeginScope("{camelCaseVsPascalCase}", "1");
// Resolution:
// logger.BeginScope("{CamelCaseVsPascalCase}", "1");DIAG_2: For improved performance, use pre-compiled log messages instead
logger.LogTrace("This is a test {Message}", "Foo");
// Resolution:
// Action<ILogger, string, Exception?> precompiledLogMessage = LoggerMessage.Define<string>(LogLevel.Trace, 42, "This is a test {Message}" ); // where 42 is its event id
// precompiledLogMessage(logger, "Foo", null);DIAG_3: Numerics should not be used in logging format string
logger.LogTrace("{0}", 1); // Produces both `DIAG_3` and `DIAG_2`
// Resolution for `DIAG_3`
// logger.LogTrace("{One}", 1);DIAG_4: Logging format string should not be dynamically generated
This would be forLogTraceand similar APIs.
logger.LogTrace("message with {argument1}" + 2);
logger.LogTrace("{string.Empty}");
// Resolution: make sure last argument is not dynamically generatedDIAG_5: Logging format string parameter count mismatch
LoggerMessage.Define<int>(LogLevel.Information, 42, "{One} {Two} {Three}");
// Resolution:
// either: LoggerMessage.Define<int>(LogLevel.Information, 42, "{One}");
// or: LoggerMessage.Define<int, int, int>(LogLevel.Information, 42, "{One} {Two} {Three}");- Why warning for
DIAG_5? when this code snippet runs we get this exception thrown at runtime:
System.ArgumentException: 'The format string '{One} {Two} {Three}' does not have the expected number of named parameters. Expected 1 parameter(s) but found 3 parameter(s).'
Link to code
The latest PR is available at dotnet/roslyn-analyzers#5244
The code was originally prototyped as preview in https://github.com/dotnet/extensions/tree/master/src/Logging/Logging.Analyzers/src but has not yet been shipped analyzers.
Original Description
Consider adding analyzers for DefineMessage
Reference analyzers from our projects
Decide how are they going to be shipped
/cc @glennc
Reacted by Andrew Scott, Christopher-Marcel Esser, cocowalla, James Chaldecott, Kalle Olavi Niemitalo and Stuart Lang
Metadata
Metadata
Assignees
Labels
api-approvedAPI was approved in API review, it can be implementedarea-Extensions-Loggingcode-analyzerMarks an issue that suggests a Roslyn analyzerenhancementProduct code improvement that does NOT require public API changes/additions
Activity
paulomorgado commentedon Oct 10, 2017
Where are these analyzers published?
pakrym commentedon Oct 10, 2017
Right now they are published to https://dotnet.myget.org/F/aspnetcore-ci-dev/api/v3/index.json feed. https://dotnet.myget.org/feed/aspnetcore-ci-dev/package/nuget/Microsoft.Extensions.Logging.Analyzers
paulomorgado commentedon Oct 10, 2017
Will they work on VS2015?
paulomorgado commentedon Apr 2, 2018
@pakrym, still no plans to release this?
pakrym commentedon Apr 2, 2018
No, not in 2.1
paulomorgado commentedon Apr 2, 2018
😢
Eilon commentedon Apr 2, 2018
@pakrym - are we shipping them in "experimental" form so that people can easily try them out? I thought I heard someone say that the other day.
pakrym commentedon Apr 2, 2018
Yes, we would continue to ship them in the same form they are now, but wouldn't do the same thing we did with MVC and EF analyzers (reference from packages themselves)
Genbox commentedon Aug 8, 2018
I think these analyzers should be a little more extensive than they are now. It would also be great if they supported structured logging templates. The structured message analyzer rule can be 'none' severity by default and only enabled if users want them.
penenkel commentedon Dec 17, 2018
any updates on this?
JanEggers commentedon Feb 3, 2020
@pakrym any updates on this one? can you please point me to the current nuget feed used for nightly builds? I didnt find any info on this in the readme and the link above does not work (vs shows no packages) and the link to the package asks for credentials.
pakrym commentedon Feb 3, 2020
@anurse might have more information on this.
analogrelay commentedon Feb 3, 2020
I believe the packages should be on the
dotnet-corenightly feed:https://dotnetfeed.blob.core.windows.net/dotnet-core/index.jsonI don't have specific updates on the progress here, but it will be considered as part of 5.0 planning.
analogrelay commentedon Apr 24, 2020
Could this tie in to the analyzer effort you're working on @bartonjs @jeffhandley ? These are analyzers currently produced in extensions (though the logging stuff is now owned by the runtime) that we don't currently ship (they're marked as non-shipping packages) but would be useful for anyone using Microsoft.Extensions.Logging.
39 remaining items
bartonjs commentedon Jul 27, 2021
Video
API Review notes
logger.LogTrace("{0}", value)=>logger.LogTrace("{Value}", value)davidfowl commentedon Jul 27, 2021
Are we doing a fixer from
LoggerMessage.Defineto the new source generator approach? That would be great 😄maryamariyan commentedon Jul 27, 2021
we're adding the analyzer as an analyzer in the SDK so my assumption is that the source generator wont be available in all cases that this analyzer suit is available for, and the fixer wouldn't be able to use the source gen. But would be good to use the source generator for adding the fixer.
cc: @ericstj
maryamariyan commentedon Jul 27, 2021
We'll add the fixers in the 7.0 timeframe.
poke commentedon Jul 27, 2021
Does that mean that the analyzers are on track for 6.0? That's great news! We've been hit by the issue from diagnostic 5 a few times in production so this will help a lot. Thanks for following up with this @maryamariyan!
maryamariyan commentedon Jul 28, 2021
Yes :) Here's a link to the merged PR dotnet/roslyn-analyzers#5244
maryamariyan commentedon Jul 28, 2021
Closing this issue as fixed and opened dotnet/roslyn-analyzers#7241 to track adding the fixers.
Kesmy commentedon Jul 29, 2021
Are these analyzers available prior to .NET 6? If not, we're probably not going to be able to use them for another decade.
ericstj commentedon Jul 29, 2021
They have been added to the SDK so you can use them when targeting any framework and running on the latest SDK. The analyzers are also available out-of-band in https://www.nuget.org/packages/Microsoft.CodeAnalysis.NetAnalyzers. You can even try out a nightly build of those today: https://dev.azure.com/dnceng/public/_packaging?_a=package&feed=dotnet6&package=Microsoft.CodeAnalysis.NetAnalyzers&protocolType=NuGet&version=6.0.0-preview7.21378.5&view=overview
Please let us know if you face any problems by filing new issues here.