SerilogAnalyzer

repository·master·Indexed 43 days ago

https://github.com/suchiman/seriloganalyzer

A Roslyn-based static analysis tool for C# projects using the Serilog logging library. It identifies common pitfalls such as incorrect exception handling, malformed message templates, property binding issues, and performance problems related to non-constant templates. Includes specific analyzers Serilog001 through Serilog008 and provides CodeFixes for several rules.

Tokens
1.8K
Snippets
10
Records
14
Agent score
5%

What's inside SerilogAnalyzer

  1. Package the SerilogAnalyzer as a NuGet package or VSIX extension

    master

    Building the project produces an analyzer .dll. You can package this in two ways depending on your deployment needs:

    1. NuGet package (.nupkg): Adds the analyzer as a project-local analyzer that participates in builds. This is the standard way to distribute analyzers for use in CI/CD and local development.
    2. VSIX extension (.vsix): Applies the analyzer to all projects within the IDE. This is useful for IDE-only enforcement.
  2. Test a local NuGet package installation

    master

    To verify a .nupkg file locally, follow these steps:

    1. Create a local NuGet feed (refer to NuGet documentation).
    2. Copy the generated .nupkg file into your local feed folder.
    3. Open your target project in Visual Studio.
    4. Right-click the project in Solution Explorer and select Manage NuGet Packages.
    5. Select your local NuGet feed from the left-hand pane.
    6. Select the analyzer from the list and click Install.
  3. Install SerilogAnalyzer

    master

    SerilogAnalyzer is a Roslyn-based analyzer that checks for common mistakes and usage problems when using the Serilog logging library. You can install it using one of the following methods:

    • Visual Studio Extension: Download the VSIX from the releases list.
    • Visual Studio Gallery: Install directly from the Visual Studio Marketplace.
    • NuGet: Add the SerilogAnalyzer NuGet package to your project.
    dotnet add package SerilogAnalyzer
  4. Automate NuGet package deployment to a local feed

    master

    You can configure the project to automatically copy the .nupkg file to your local NuGet feed folder upon every build by modifying the project file:

    1. Right-click the project in Solution Explorer and select Unload Project.
    2. Right-click the project again and select Edit.
    3. Locate the <AfterBuild> target in the XML.
    4. Within the <Exec> task, update the Command attribute. Change the path following the -OutputDirectory flag to point to your local NuGet feed folder.
    <!-- Example of the target to modify in the .csproj file -->
    <Target Name="AfterBuild">
      <Exec Command="... -OutputDirectory C:\Your\Local\NuGet\Feed\Path" />
    </Target>
  5. Refactor Fluent LoggerConfiguration to AppSettings

    master
    SerilogAnalyzer can perform static analysis on fluent LoggerConfiguration calls to generate configuration suitable for use with serilog-settings-appsettings or serilog-settings-configuration (e.g., appSettings.json).
  6. Serilog003: Verify Property Binding

    master

    The Serilog003 analysis ensures coherence between message template tokens and the supplied arguments. Every property named in the message template must correspond to exactly one argument.

    Incorrect usage:

    Log.Information("Saving {File} to {Directory}", file);

    Correct usage:

    Log.Information("Saving {File} to {Directory}", file, directory);
    Log.Information("Saving {File} to {Directory}", file, directory);
  7. Serilog007: Destructure Anonymous Objects

    master

    The Serilog007 analysis checks that anonymous objects passed to the logger use the destructuring operator (@). Without this, the object is logged as a string rather than its properties.

    Incorrect usage:

    Log.Information("Saving {File} to {Directory}", new { Name = name, Size = size }, directory); 

    Correct usage:

    Log.Information("Saving {@File} to {Directory}", new { Name = name, Size = size }, directory); 

    A CodeFix is available to apply the destructuring hint (@).

    Log.Information("Saving {@File} to {Directory}", new { Name = name, Size = size }, directory);
  8. Serilog001: Ensure exceptions are passed as the first argument

    master

    The Serilog001 analysis checks that exceptions are passed to the exception argument rather than being treated as a normal property. Serilog provides special handling for exceptions when they are passed as the first argument of a log method call.

    Incorrect usage:

    catch (Exception ex)
    {
       Log.Error("Could not save {File}: {Error}", file, ex);
    }

    Correct usage:

    catch (Exception ex)
    {
       Log.Error(ex, "Could not save {File}", file);
    }

    A CodeFix is available to correct this usage.

    Log.Error(ex, "Could not save {File}", file);
  9. Serilog008: Use Correct Contextual Logger Type

    master

    The Serilog008 analysis ensures that contextual loggers are constructed using the correct type for the class they are in.

    Incorrect usage:

    class A
    {
        private static readonly ILogger Logger = Logger.ForContext<B>();
    }
    
    class B {}

    Correct usage:

    class A
    {
        private static readonly ILogger Logger = Logger.ForContext<A>();
    }
    
    class B {}

    A CodeFix is available to update the type in the ForContext<T> call.

    private static readonly ILogger Logger = Logger.ForContext<A>();
  10. Serilog005: Ensure Unique Property Names

    master

    The Serilog005 analysis checks that all property names within a single message template are unique. If names are duplicated, Serilog may only record one of them.

    Incorrect usage:

    Log.Information("Saving {Path} to {Path}", file, directory); 

    Correct usage:

    Log.Information("Saving {File} to {Directory}", file, directory); 
    Log.Information("Saving {File} to {Directory}", file, directory);
  11. Serilog006: Use PascalCased Property Names

    master

    The Serilog006 analysis checks that all property names in a message template use PascalCase.

    Incorrect usage:

    Log.Information("Saving {file} to {directory}", file, directory); 

    Correct usage:

    Log.Information("Saving {File} to {Directory}", file, directory); 

    A CodeFix is available to automatically apply Pascal casing to property names.

    Log.Information("Saving {File} to {Directory}", file, directory);