AttributeLogger 1.0.0
dotnet add package AttributeLogger --version 1.0.0
NuGet\Install-Package AttributeLogger -Version 1.0.0
<PackageReference Include="AttributeLogger" Version="1.0.0" />
<PackageVersion Include="AttributeLogger" Version="1.0.0" />
<PackageReference Include="AttributeLogger" />
paket add AttributeLogger --version 1.0.0
#r "nuget: AttributeLogger, 1.0.0"
#:package AttributeLogger@1.0.0
#addin nuget:?package=AttributeLogger&version=1.0.0
#tool nuget:?package=AttributeLogger&version=1.0.0
AttributeLogger
This library provides a simple Aspect-Oriented Programming(AOP) approach to logging of C# classes and methods, using AspectCore.DynamicProxy to intercept method calls and integrates with Microsoft.Extensions.Logging. It automatically logs nested exceptions and propagates them by default. The standard logging info is formatted as follow
InvocationGuid: [f5c7e3c2-0d4f-45e8-9a2d-123456789abc] MyApp.Services.OrderService.CreateOrder Start
InvocationGuid: [f5c7e3c2-0d4f-45e8-9a2d-123456789abc] MyApp.Services.OrderService.CreateOrder Param firstParam: ""
InvocationGuid: [f5c7e3c2-0d4f-45e8-9a2d-123456789abc] MyApp.Services.OrderService.CreateOrder End
Main Purpose
The main purpose is to:
- Automatically log method start, end, and failures.
- Allow flexible logging levels (Trace, Debug, Information, Warning, Error, Critical).
- Enable or disable logging at the method level.
- Optionally log method parameters.
- Correlate logs across method calls using a correlation GUID.
Attributes
LogAttribute
This is the beating heart of this library. It's responsable of the whole logging system and specifies the logging level.
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)] public class LogAttribute : AbstractInterceptorAttribute { public LogLevel LogLevel { get; set; }
public LogAttribute(LogLevel logLevel)
{
LogLevel = logLevel;
}
}
DisableLogAttribute
this method avoids unwanted methods logging, do not need further parameters.
[AttributeUsage(AttributeTargets.Method, Inherited = false)] public class DisableLogAttribute : Attribute { // This attribute is used to disable logging for specific methods }
LogParametersAttribute
This method enables method parameters input logging, to a specific level. Also allows to handle exception propagation
[AttributeUsage(AttributeTargets.Method, Inherited = false)] public class LogParametersAttribute : Attribute { public LogLevel LogLevel { get; set; } public bool RaiseExceptions { get; set; } public LogParametersAttribute() { } public LogParametersAttribute(LogLevel logLevel = LogLevel.Information, bool raiseExceptions = false) { LogLevel = logLevel; RaiseExceptions = raiseExceptions; } }
Usage
- Decorate Classes
[Log(LogLevel.Information)] public class MyServiceService { public void CreateOrder(string customerId) { // Logs automatically } }
this will create the following logging
InvocationGuid: [f5c7e3c2-0d4f-45e8-9a2d-123456789abc] MyApp.Services.MyServiceService.CreateOrder Start InvocationGuid: [f5c7e3c2-0d4f-45e8-9a2d-123456789abc] MyApp.Services.MyServiceService.CreateOrder End
- Decorate Methods (Optional)
[Log(LogLevel.Information)] public class MyServiceService { public void CreateOrder(string customerId) { MyCommand.CreateOrder(customerId); }
public void CreateOrder(string OrderId)
{
// Logs automatically
}
}
[Log(LogLevel.Information)] public class MyCommand(){
[DisableLog]
public CreateOrder(string customerId){
WriteToDatabase(customerId);
}
}
Register AspectCore
In your Program.cs or Startup.cs:
services.AddDynamicProxy(); // Enables AspectCore interception
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net6.0 is compatible. 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. |
-
net6.0
- AspectCore.Core (>= 2.4.0)
- Microsoft.Extensions.Logging.Abstractions (>= 7.0.0)
- Newtonsoft.Json (>= 13.0.3)
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 |
|---|