Ashutosh.Common.Logger
4.0.0
dotnet add package Ashutosh.Common.Logger --version 4.0.0
NuGet\Install-Package Ashutosh.Common.Logger -Version 4.0.0
<PackageReference Include="Ashutosh.Common.Logger" Version="4.0.0" />
<PackageVersion Include="Ashutosh.Common.Logger" Version="4.0.0" />
<PackageReference Include="Ashutosh.Common.Logger" />
paket add Ashutosh.Common.Logger --version 4.0.0
#r "nuget: Ashutosh.Common.Logger, 4.0.0"
#:package Ashutosh.Common.Logger@4.0.0
#addin nuget:?package=Ashutosh.Common.Logger&version=4.0.0
#tool nuget:?package=Ashutosh.Common.Logger&version=4.0.0
Ashutosh.Common.Logger
A flexible .NET logging library with runtime logger discovery using MEF (Managed Extensibility Framework).
Overview
This library provides a unified logging interface that discovers and loads logger implementations at runtime using MEF. If no custom logger is found, it automatically falls back to Console and Trace output.
Features
- Runtime Logger Discovery: Uses MEF (
System.ComponentModel.Composition) to discoverILoggerimplementations at runtime viaApplicationCatalog - Automatic Fallback: Falls back to
FallbackLogger(which writes toSystem.Diagnostics.Trace) when no MEF-exported logger is found - Console Logging: Optional console output with color-coded severity levels
- Multiple Logger Support: Can route logs to multiple logger implementations simultaneously
- Severity Levels: Supports
None,Verbose,Info,Warning,Error, andFatal - Exception Handling: Built-in support for logging exceptions including
AggregateExceptionexpansion - Thread-Safe: Console logging is synchronized for thread safety
How It Works
Logger Discovery
- The
LoggerContainerclass uses MEF to scan all assemblies in the application for types that exportILogger - If a valid logger is discovered, it is added to the list of active loggers
- If no logger is found and no additional loggers are configured, the
FallbackLoggeris used - The
FallbackLoggerwrites formatted log messages toSystem.Diagnostics.Trace
Fallback Behavior
When no MEF-exported logger is discovered:
- Log output is directed to
System.Diagnostics.Trace.WriteLine() - This ensures logs are never silently lost
- Trace output can be captured by attaching trace listeners
Usage
Basic Logging
using Ashutosh.Common.Logger;
// Create a logger instance
var logger = new Logger(typeof(MyClass), "MyClass");
// Log messages at different severity levels
logger.Log("Information message");
logger.LogVerbose("Verbose details");
logger.LogWarning("Warning message");
logger.LogError("Error occurred");
// Log with exceptions
try
{
// some code
}
catch (Exception ex)
{
logger.LogError(ex, "Operation failed: {0}", operationName);
}
Static Logging
Logger.Log("ModuleName", "Message", Severity.Info);
Logger.Log("ModuleName", "Error message", Severity.Error, exception);
Automatic Method Tracing
using (var log = new Log(typeof(MyClass), "MyMethod", arg1, arg2))
{
// Method body - entry and exit are automatically logged
log.WriteLine("Custom trace message");
}
Implementing a Custom Logger
To provide your own logger implementation that will be discovered at runtime:
using System.ComponentModel.Composition;
using Ashutosh.Common.Logger;
[Export(typeof(ILogger))]
public class MyCustomLogger : ILogger
{
public void Log(LogData logData)
{
// Your custom logging implementation
// logData contains: ModuleName, NameSpace, Message, Severity, Exception, ProcessId, ThreadId
}
}
The [Export(typeof(ILogger))] attribute marks the class for MEF discovery. When your assembly is loaded, MEF will automatically find and use your logger.
Configuration
Force Console Output
LoggerInitializer.ForceLogToConsole = true;
Add Additional Loggers
LoggerInitializer.AdditionalLoggers.Add(new MyCustomLogger());
Log Data Structure
The LogData class contains:
- ModuleName: Name of the logging module/class
- NameSpace: Namespace of the logging source
- Message: The log message
- Severity: Log level (None, Verbose, Info, Warning, Error, Fatal)
- Exception: Formatted exception details
- ProcessId: Current process ID
- ThreadId: Current thread ID
License
All rights reserved. Reproduction or transmission in whole or in part, in any form or by any means, electronic, mechanical or otherwise, is prohibited without prior written permission.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. 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. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- System.ComponentModel.Composition (>= 7.0.0)
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 |
|---|---|---|
| 4.0.0 | 234 | 12/25/2025 |