Skip to content

Kralizek/LoggingExtensions

Repository files navigation

Buy me a pizza!

Build status NuGet version

Kralizek.Extensions.Logging

This package contains a set of extension methods designed to improve logging using ILogger<T> in .NET projects.

The extension methods use generics to avoid boxing of the logging values and check the logging level before unboxing them.

Further more, the extension methods are placed in the same namespace of ILogger<T>, once the package is installed, all your methods will automatically be using them.

How to use it

You can install the package using the .NET SDK CLI

$ dotnet add package Kralizek.Extensions.Logging

Now you keep logging as usual.

public class MyService(ILogger<MyService> logger) : IService
{
    public void DoSomething(int value)
    {
        logger.LogInformation("I'm doing something: {Value}", value);
    }
}

How does it work?

The library relies on the C# compiler overload resolution algorithm to make sure that calls like the one above are routed to the extension methods instead of the default one whose signature is LogInformation(this ILogger logger, string? message, params object?[] args).

Gotchas

The library improves old-style logging. Across several releases, Microsoft has added more ways to generate efficient logging statements like using LoggerMessage. Consider reading this article by Microsoft regarding high-performance logging in .NET.

The library only handles typical green-path scenarios. Currently there are no extension methods accepting either instances of EventId or Exception. If this is important, feel free to create a feature request.

Credits

This library implements the suggestions by Nick Chapsas in his video You are doing .NET logging wrong. Let's fix it.

Versioning

This library follows Semantic Versioning 2.0.0 for the public releases (published to the nuget.org).

How to build

This project uses Cake as a build engine.

If you would like to build this project locally, just execute the build.cake script.

You can do it by using the .NET tool created by CAKE authors and use it to execute the build script.

dotnet tool install -g Cake.Tool
dotnet cake

About

Provides some utility extension methods to aspnet/Logging ILogger

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Languages