AndrewK.Logger.ScopedLogging
1.0.1
dotnet add package AndrewK.Logger.ScopedLogging --version 1.0.1
NuGet\Install-Package AndrewK.Logger.ScopedLogging -Version 1.0.1
<PackageReference Include="AndrewK.Logger.ScopedLogging" Version="1.0.1" />
<PackageVersion Include="AndrewK.Logger.ScopedLogging" Version="1.0.1" />
<PackageReference Include="AndrewK.Logger.ScopedLogging" />
paket add AndrewK.Logger.ScopedLogging --version 1.0.1
#r "nuget: AndrewK.Logger.ScopedLogging, 1.0.1"
#:package AndrewK.Logger.ScopedLogging@1.0.1
#addin nuget:?package=AndrewK.Logger.ScopedLogging&version=1.0.1
#tool nuget:?package=AndrewK.Logger.ScopedLogging&version=1.0.1
ScopedLogging
ScopedLogging makes it possible to easily write scoped parameters to all log messages within a specific scope.
It provides extension methods for ILogger<T> to simplify adding contextual information to log entries.
Features
- Scoped logging: Attach contextual parameters to all log messages within a defined scope.
- Flexible initialization: Initialize scopes with zero, one, or multiple parameters.
- Automatic cleanup: Ensures proper disposal of logging scopes.
- Simple API: Easy-to-use extension methods for
ILogger<T>.
Installation
You can install the package via NuGet (replace with actual package name if published):
dotnet add package KudAndrii.Logger.ScopedLogging
Then, include the necessary namespace in your project:
using KudAndrii.Logger.ScopedLogging.Extensions;
Usage
Basic Usage
using (var scopesBag = logger.InitScopes())
{
scopesBag.AppendScope("UserId", 12345);
logger.LogInformation("User authenticated.");
// Logs will include UserId.
scopesBag.AppendScope("Age", 18);
logger.LogInformation("Process completed.");
// Logs will include UserId along with Age.
}
Initializing with Parameters
using (var scopesBag = logger.InitScopes("UserId", 12345))
{
logger.LogInformation("User logged in.");
// Logs will include UserId.
scopesBag.AppendScope("SessionId", "XYZ789");
logger.LogInformation("Session started.");
// Logs will include UserId and SessionId.
}
Initializing with Multiple Parameters
using (var scopesBag = logger.InitScopes(("UserId", 12345), ("Role", "Admin")))
{
logger.LogInformation("User dashboard accessed.");
// Logs will include UserId and Role.
}
API Reference
ScopesBag<TCategoryName> Class
Overview
A helper class for managing logging scopes, allowing the addition of contextual parameters to log messages.
Methods
AppendScope(string propertyName, object? propertyValue)
Adds a single scoped parameter to all subsequent log messages.
Example:scopesBag.AppendScope("UserId", 12345);AppendScope(params (string PropertyName, object? PropertyValue)[] args)
Adds multiple scoped parameters.
Example:scopesBag.AppendScope(("UserId", 12345), ("Role", "Admin"));
Extension Methods
The ScopedLogging library provides the following extension methods for ILogger<T>:
InitScopes<T>(this ILogger<T> logger)
Creates a new ScopesBag<T> instance to manage log scopes.
using (var scopesBag = logger.InitScopes())
{
logger.LogInformation("Log with default scope");
}
InitScopes<T>(this ILogger<T> logger, string propertyName, object? propertyValue)
Creates a new ScopesBag<T> and adds an initial scope parameter.
using (var scopesBag = logger.InitScopes("UserId", 12345))
{
logger.LogInformation("Log with UserId scope");
}
InitScopes<T>(this ILogger<T> logger, params (string PropertyName, object? PropertyValue)[] args)
Creates a new ScopesBag<T> and adds multiple initial scope parameters.
using (var scopesBag = logger.InitScopes(("UserId", 12345), ("Role", "Admin")))
{
logger.LogInformation("Log with UserId and Role scopes");
}
Best Practices
Use short-lived scopes:
Always wrap scopes inusingstatements to ensure proper disposal.Minimize scope size:
Avoid adding too many properties to prevent performance overhead.Consistent naming:
Use a consistent naming pattern for scoped properties to improve log readability.
Contributing
Contributions are welcome! Please follow these steps to contribute:
- Fork the repository.
- Create a new branch with your feature or bug fix.
- Submit a pull request.
License
This project is licensed under the MIT License. See the LICENSE file for details.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. 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. |
-
net8.0
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.2)
- 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.