This project is provided to the larger open-source community by Innovian.
This solution implements an aspect using Metalama targeting .NET 8 that provides logging capabilities to projects targeting Microsoft.Extensions.Logging using ILoggerFactory and/or ILogger. This project
includes a single aspect intended for direct use, InjectLoggerAttribute (intended to be applied to a class) and a fabric that automatically applies this attribute to every non-static class and non-static method across whatever project that
targets it.
The attribute performs the following:
- Automatically wraps eligible methods in try/catch blocks
- Injects an optional instance of
ILoggerFactoryinto all constructors of an eligible class - Injects and instantiates an
ILoggeras a private field on the class from theILoggerFactoryor from aNullLoggerFactoryinstance if the injectedILoggerFactoryis null - Creates a stopwatch in each method that times and records, via the
ILoggerfield, how long it takes a given method to execute
The team at Postsharp were kind enough to grant this project an open source license meaning that this namespace can be built with any number of aspects without a Metalama license. However, you will a Metalama license to use this in your own project. Their free license allows up to 3 aspects at no cost. I've purchased a commercial license for their software for use at my own company and if you similarly find that this aspects adds sufficient value to your own work, I encourage you to purchase licenses for your own team.
Note: The Innovian.Aspects.Logging project can be used with the free plan by decorating each class with [InjectLogger], but you'll need the cheapest paid plan to support the Fabric (automatic project-wide aspect decorating) used in Innovian.Aspects.Logging.Fabric as Fabrics are not supported in the free license.
The following instructions detail how to use the InjectLoggerAttribute directly in those project. It's intended to be applied as an attribute to any classes (not records) in your project and will automatically apply the other attributes
in the project. Direct usage of these other attributes isn't intended.
Using the .NET CLI tools:
dotnet add package Innovian.Aspects.LoggingUsing the Package Manager Console:
Install-Package Innovian.Aspects.LoggingFrom within Visual Studio:
- Open the Solution Explorer.
- Right-click on the project within your solution you wish to add the attribute to.
- Click on "Manage NuGet Packages...".
- Click on the "Browse" tab and search for "Innovian.Aspects.Logging".
- Click on the "Innovian.Aspects.Logging" package, select the appropriate version in the right-tab and click Install.
Simply apply the [InjectLogger] attribute to any non-static class you would like the logging capabilities added to. It's recommended that the class be marked with the partial keyword so that you're able to access the ILogger field on the
type named _logger, but this isn't required.
The following instructions detail how to use the Fabric-based approach to applying the InjectLoggerAttribute automatically throughout the project referencing this package. This fabric will automatically apply the aspect to
all non-static classes and non-static methods.
Using the .NET CLI tools:
dotnet add package Innovian.Aspects.Logging.FabricUsing the Package Manager Console:
Install-Package Innovian.Aspects.Logging.FabricFrom within Visual Studio:
- Open the Solution Explorer.
- Right-click on the project within your solution you wish to add the attribute to.
- Click on "Manage NuGet Packages...".
- Click on the "Browse" tab and search for "Innovian.Aspects.Logging.Fabric".
- Click on the "Innovian.Aspects.Logging.Fabric" package, select the appropriate version in the right-tab and click Install.
No additional effort is necessary beyond installation of the Innovian.Aspects.Logging.Fabric package on the project. It will automatically identify all non-static classes to apply the [InjectLogger] attribute to automatically. In turn, this
will perform the code generation functionality described above.
The fabric will only install to classes that have at least one method as there's little point to it being on the class otherwise.
Further, ecause we at Innovian also build functionality around Dapr Workflows and the Workflow base type requires parameterless constructors, the fabric will
also neglect to apply to any type that implements an abstract base type called "Workflow".