stbychkov.AutoLoggerMessage 1.0.11

Prefix Reserved
dotnet add package stbychkov.AutoLoggerMessage --version 1.0.11
                    
NuGet\Install-Package stbychkov.AutoLoggerMessage -Version 1.0.11
                    
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="stbychkov.AutoLoggerMessage" Version="1.0.11" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="stbychkov.AutoLoggerMessage" Version="1.0.11" />
                    
Directory.Packages.props
<PackageReference Include="stbychkov.AutoLoggerMessage" />
                    
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 stbychkov.AutoLoggerMessage --version 1.0.11
                    
#r "nuget: stbychkov.AutoLoggerMessage, 1.0.11"
                    
#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 stbychkov.AutoLoggerMessage@1.0.11
                    
#: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=stbychkov.AutoLoggerMessage&version=1.0.11
                    
Install as a Cake Addin
#tool nuget:?package=stbychkov.AutoLoggerMessage&version=1.0.11
                    
Install as a Cake Tool

AutoLoggerMessage

NuGet Build License NuGet Downloads PRs Welcome Ask DeepWiki


Welcome to AutoLoggerMessage, a source generator that automatically creates LoggerMessage methods, enabling high-performance logging.

Demo

Click the image below to watch the demo video:

Demo

Getting started

Install the Package from NuGet
dotnet add package stbychkov.AutoLoggerMessage

Check this page for configuration options that can tweak the source generation process.

Benchmarks

You can achieve performance boosts of up to 90% just by including this source generator in your project.

For ILogger.Log* methods:

Configuration Mean Ratio Allocated
Default implementation 41.419 ns 1.00 216 B
Default + LoggerMessage 4.004 ns 0.10 -
AutoLoggerMessage 4.577 ns 0.11 -

And for ILogger.DefineScope:

Configuration Mean Ratio Allocated
BeginScope 39.566 ns 1.00 216 B
DefineScope 5.197 ns 0.13 -
AutoLoggerMessage 5.296 ns 0.13 -

Take a look at benchmark page for more details.

Known Limitations

  • It supports only static EventId parameter. If you pass the explicit EventId parameter, which basically no one does as far as I can tell, it generates a new EventId and the existing one will be passed to the formatter state, but it won't be logged. This limitation comes from the original LoggerMessage generator as they don't support the explicit parameter ~~ yet~~.
  • Log.Define supports only 6 message parameters (src) so if you pass more than that, the default Logger.Log(*, params object[] args) will be executed.
  • As this solution is based on interceptors, only .NET 8+ is supported
  • Generic arguments are not supported. If you pass a generic argument to the log function, the default Logger.Log(*, params object[] args) will be executed.

Is something wrong?

If something is not working as expected, you can fall back on the default implementation of ILogger extensions. To do this, call the extensions directly, for example:

logger.LogInformation("Some message"); // instead of this
LoggerExtensions.LogInformation(logger, "Some message"); // use this

In such case, the source generator will bypass the log call, ensuring you get the expected behavior.

If you require functionality from the LoggerMessage source generator that is not supported by this library, you can manually create your own source-generated version. Simply define your own partial class and partial method, annotated with the [LoggerMessage] attribute.

logger.LogInformation("Some message"); // instead of this

[LoggerMessage(LogLevel = LogLevel.Information, Message = "Some message")]
public partial void LogSomeMessage(); // use this

But for both scenarios, it’s recommended to report the issue, as it shouldn't happen under normal circumstances. Your feedback can help improve the library and address potential shortcomings. Thank you!

Motivation

Source-generated logging is increasingly recognized as a modern and efficient approach. Check this page to see why.

But let’s be real, when you’ve got a mid-sized project, migrating to the new logging approach is not exactly a simple task. And even when starting a new project, marking every class as partial or depending on an external partial class for logging messages can feel disconnected from the code where they are actually used.

This library handles most of the heavy lifting for you, so you can spend your time doing more important things!

But I hope this is a temporary solution — maybe one day, something similar will be added to the core library. Check this discussion for updates.

How It Works

This source generator searches for all logger.Log* methods in your code and, based on their parameters, automatically creates partial methods for LoggerMessage. It also generates a set of interceptors to forward the logging calls to the newly generated LoggerMessage methods.

For more details, see the How It Works documentation

Questions?

I bet you have some questions about why things are set up the way they are. Refer to the ADR files for detailed explanations of the design decisions behind this package.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages (61)

Showing the top 5 NuGet packages that depend on stbychkov.AutoLoggerMessage:

Package Downloads
Reo.Core.Validation

Package Description

Reo.Core.DataModels

Package Description

Reo.Core.Queue.Abstractions

Package Description

Reo.Core.Hosting

Package Description

Reo.Core.HealthCheck

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.11 861 6/16/2025
1.0.10 3,809 4/20/2025
1.0.9 495 1/29/2025
1.0.9-beta1 95 1/29/2025
1.0.8 132 1/21/2025
1.0.7 124 1/21/2025
1.0.7-beta.1 61 1/21/2025
1.0.6 173 12/19/2024
1.0.5 122 12/3/2024