Skip to content

Finalize logging analyzers #36064

Closed
Closed

Description

@pakrym

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 for LogTrace and similar APIs.
logger.LogTrace("message with {argument1}" + 2);
logger.LogTrace("{string.Empty}");
// Resolution: make sure last argument is not dynamically generated

  • DIAG_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

Activity

  1. paulomorgado commented on Oct 10, 2017

    @paulomorgado
    Contributor

    Where are these analyzers published?

  2. paulomorgado commented on Oct 10, 2017

    @paulomorgado
    Contributor

    Will they work on VS2015?

  3. paulomorgado commented on Apr 2, 2018

    @paulomorgado
    Contributor

    @pakrym, still no plans to release this?

  4. pakrym commented on Apr 2, 2018

    @pakrym
    ContributorAuthor

    No, not in 2.1

  5. paulomorgado commented on Apr 2, 2018

    @paulomorgado
    Contributor

    😢

  6. Eilon commented on Apr 2, 2018

    @Eilon
    Contributor

    @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.

  7. pakrym commented on Apr 2, 2018

    @pakrym
    ContributorAuthor

    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)

  8. Genbox commented on Aug 8, 2018

    @Genbox
    Contributor

    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.

  9. penenkel commented on Dec 17, 2018

    @penenkel

    any updates on this?

  10. JanEggers commented on Feb 3, 2020

    @JanEggers

    @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.

  11. pakrym commented on Feb 3, 2020

    @pakrym
    ContributorAuthor

    @anurse might have more information on this.

  12. analogrelay commented on Feb 3, 2020

    @analogrelay
    Contributor

    I believe the packages should be on the dotnet-core nightly feed: https://dotnetfeed.blob.core.windows.net/dotnet-core/index.json

    I don't have specific updates on the progress here, but it will be considered as part of 5.0 planning.

  13. analogrelay commented on Apr 24, 2020

    @analogrelay
    Contributor

    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.

  14. 39 remaining items

  15. added
    blocking
    on Jul 23, 2021
  16. bartonjs commented on Jul 27, 2021

    @bartonjs
    Member

    Video

    API Review notes

    • Diagnostic 1
      • A fixer seems doable here.
      • A level higher than Disabled seems warranted. (Hidden?)
      • Category: Naming
    • Diagnostic 2
      • A fixer seems possible here, but the resolution should create a static field for the defined message, not a local variable.
      • Should be Hidden, not Disabled.
      • Category: Performance
    • Diagnostic 3
      • The example should be updated. logger.LogTrace("{0}", value) => logger.LogTrace("{Value}", value)
      • Information / Usage
    • Diagnostic 4
      • This should not apply to LoggerMessage.Define, just LogTrace (and friends).
      • Information / Usage
    • Diagnostic 5
      • Warning does seems appropriate when/since you can prove it's a guaranteed exception.
      • Since it's about an exception, let's use Reliability instead of Usage.
      • Warning / Reliability
  17. added
    api-approved
    and removed
    api-ready-for-review
    blocking
    on Jul 27, 2021
  18. davidfowl commented on Jul 27, 2021

    @davidfowl
    Member

    Are we doing a fixer from LoggerMessage.Define to the new source generator approach? That would be great 😄

  19. maryamariyan commented on Jul 27, 2021

    @maryamariyan
    Contributor

    Are we doing a fixer from LoggerMessage.Define to the new source generator approach? That would be great 😄

    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

  20. maryamariyan commented on Jul 27, 2021

    @maryamariyan
    Contributor

    We'll add the fixers in the 7.0 timeframe.

  21. poke commented on Jul 27, 2021

    @poke
    Contributor

    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!

  22. maryamariyan commented on Jul 28, 2021

    @maryamariyan
    Contributor

    Does that mean that the analyzers are on track for 6.0?

    Yes :) Here's a link to the merged PR dotnet/roslyn-analyzers#5244

  23. maryamariyan commented on Jul 28, 2021

    @maryamariyan
    Contributor

    Closing this issue as fixed and opened dotnet/roslyn-analyzers#7241 to track adding the fixers.

  24. Kesmy commented on Jul 29, 2021

    @Kesmy

    Are these analyzers available prior to .NET 6? If not, we're probably not going to be able to use them for another decade.

  25. ericstj commented on Jul 29, 2021

    @ericstj
    Member

    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.

  26. ghost locked as resolved and limited conversation to collaborators on Aug 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api-approvedarea-Extensions-Loggingcode-analyzerenhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions