Fineboym.Logging.Generator
1.9.0
See the version list below for details.
dotnet add package Fineboym.Logging.Generator --version 1.9.0
NuGet\Install-Package Fineboym.Logging.Generator -Version 1.9.0
<PackageReference Include="Fineboym.Logging.Generator" Version="1.9.0" />
paket add Fineboym.Logging.Generator --version 1.9.0
#r "nuget: Fineboym.Logging.Generator, 1.9.0"
// Install Fineboym.Logging.Generator as a Cake Addin #addin nuget:?package=Fineboym.Logging.Generator&version=1.9.0 // Install Fineboym.Logging.Generator as a Cake Tool #tool nuget:?package=Fineboym.Logging.Generator&version=1.9.0
Logging Decorator Source Generator
Generates logger decorator class for an interface at compile time(no runtime reflection). Uses Microsoft.Extensions.Logging.ILogger
to log and requires it in decorator class constructor.
- Logs method parameters and return value(can omit secrets from log using
[NotLoggedAttribute]
) - Supports async methods
- Supports log level, event id, and event name override through attribute
- Can catch and log specific exceptions
- Can measure method duration for performance reporting
- Follows High-performance logging in .NET guidance
Getting started
Use [DecorateWithLogger]
attribute in Fineboym.Logging.Attributes
namespace on an interface. In Visual Studio you can see the generated code in Solution Explorer if you expand Dependencies->Analyzers->Fineboym.Logging.Generator.
Prerequisites
Latest version of Visual Studio 2022.
Usage
using Fineboym.Logging.Attributes;
using Microsoft.Extensions.Logging;
namespace SomeFolder.SomeSubFolder;
// Default log level is Debug, applied to all methods. Can be changed through attribute's constructor.
[DecorateWithLogger]
public interface ISomeService
{
int SomeMethod(DateTime someDateTime);
// Override log level and event id. EventName is also supported.
[LogMethod(Level = LogLevel.Information, EventId = 100, MeasureDuration = true)]
Task<double?> SomeAsyncMethod(string? s);
// By default, exceptions are not logged and there is no try-catch block around the method call.
// If you want to log exceptions, use ExceptionToLog property.
// Default log level for exceptions is Error and it can be changed through ExceptionLogLevel property.
[LogMethod(ExceptionToLog = typeof(InvalidOperationException))]
Task<string> AnotherAsyncMethod(int x);
// You can omit secrets or PII from logs using [NotLogged] attribute.
[return: NotLogged]
string GetMySecretString(string username, [NotLogged] string password);
}
This will create a generated class named SomeServiceLoggingDecorator
in the same namespace as the interface.
You can see the generated code example for above interface on GitHub README.
Additional documentation
If you use .NET dependency injection, then you can decorate your service interface. You can do it yourself or use Scrutor. Here is an explanation Adding decorated classes to the ASP.NET Core DI container using Scrutor. If you're not familiar with Source Generators, read Source Generators.
Limitations
Currently it supports non-generic interfaces, only with methods as its members and up to 6 parameters in a method which is what
LoggerMessage.Define Method
supports. To work around 6 parameters limitation, you can encapsulate some
parameters in a class or a struct or omit them from logging using [NotLogged]
attribute.
Feedback
Please go to GitHub repository for feedback. Feel free to open issues for questions, bugs, and improvements and I'll try to address them as soon as I can. Thank you.
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- 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.10.0 | 663 | 12/22/2023 | |
1.9.0 | 1,897 | 9/23/2023 | |
1.8.0 | 934 | 7/14/2023 | |
1.7.0 | 942 | 6/16/2023 | |
1.6.0 | 923 | 5/27/2023 | |
1.5.0 | 1,002 | 5/20/2023 | |
1.4.0 | 907 | 4/29/2023 | |
1.3.1 | 876 | 4/26/2023 | |
1.3.0 | 843 | 4/21/2023 | |
1.2.0 | 1,116 | 3/31/2023 | |
1.1.0 | 1,006 | 3/18/2023 | |
1.0.0 | 576 | 3/17/2023 | |
0.1.0-beta | 1,043 | 12/25/2022 |