CompileTimeLogger 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package CompileTimeLogger --version 1.0.0
                    
NuGet\Install-Package CompileTimeLogger -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CompileTimeLogger" Version="1.0.0">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CompileTimeLogger" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="CompileTimeLogger">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add CompileTimeLogger --version 1.0.0
                    
#r "nuget: CompileTimeLogger, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package CompileTimeLogger@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=CompileTimeLogger&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=CompileTimeLogger&version=1.0.0
                    
Install as a Cake Tool

CompileTimeLogger

A Roslyn analyzer that detects ILogger.Log* method calls and provides code fixes to convert them to compile-time generated logging using the [LoggerMessage] attribute for better performance.

Build and Publish NuGet

Why Use Compile-Time Logging?

The [LoggerMessage] attribute (introduced in .NET 6) enables source generation for high-performance logging:

  • Zero allocations - No boxing of value types
  • No parsing at runtime - Message templates are parsed at compile time
  • Strongly typed - Compile-time validation of log message parameters
  • Better performance - Up to 6x faster than traditional logging extensions

Installation

dotnet add package CompileTimeLogger

Or via Package Manager:

Install-Package CompileTimeLogger

Usage

The analyzer detects calls like:

_logger.LogInformation("User {UserId} logged in", userId);

And offers two code fixes:

Fix 1: Convert to Instance Method

// Before
_logger.LogInformation("User {UserId} logged in", userId);

// After
LogUserLoggedIn(userId);

[LoggerMessage(Level = LogLevel.Information, Message = "User {UserId} logged in")]
private partial void LogUserLoggedIn(string userId);

Fix 2: Convert to Private Log Class

// Before
_logger.LogInformation("User {UserId} logged in", userId);

// After
Log.UserLoggedIn(_logger, userId);

private static partial class Log
{
    [LoggerMessage(Level = LogLevel.Information, Message = "User {UserId} logged in")]
    public static partial void UserLoggedIn(ILogger logger, string userId);
}

Supported Log Methods

  • LogTrace
  • LogDebug
  • LogInformation
  • LogWarning
  • LogError
  • LogCritical

Features

  • Works with both ILogger and ILogger<T>
  • Handles exceptions in log calls (e.g., _logger.LogError(ex, "message"))
  • Automatically extracts message template placeholders
  • Generates properly named methods from message templates
  • Makes containing class partial automatically

Requirements

  • .NET 6.0 or later (for [LoggerMessage] attribute support)
  • C# 9.0 or later

Diagnostic ID

ID Description
CTL001 ILogger.Log* call can be converted to compile-time generated logging

License

MIT License - see LICENSE.txt

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.6 101 2/17/2026
1.0.5 91 2/3/2026
1.0.4 105 1/30/2026
1.0.0 104 1/27/2026

Initial release with support for all log levels and two code fix options.