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
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Ashutosh.Common.Logger" Version="4.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Ashutosh.Common.Logger" Version="4.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Ashutosh.Common.Logger" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Ashutosh.Common.Logger --version 4.0.0
                    
#r "nuget: Ashutosh.Common.Logger, 4.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Ashutosh.Common.Logger@4.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Ashutosh.Common.Logger&version=4.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Ashutosh.Common.Logger&version=4.0.0
                    
Install as a Cake Tool

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 discover ILogger implementations at runtime via ApplicationCatalog
  • Automatic Fallback: Falls back to FallbackLogger (which writes to System.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, and Fatal
  • Exception Handling: Built-in support for logging exceptions including AggregateException expansion
  • Thread-Safe: Console logging is synchronized for thread safety

How It Works

Logger Discovery

  1. The LoggerContainer class uses MEF to scan all assemblies in the application for types that export ILogger
  2. If a valid logger is discovered, it is added to the list of active loggers
  3. If no logger is found and no additional loggers are configured, the FallbackLogger is used
  4. The FallbackLogger writes formatted log messages to System.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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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