Oakrey.Applications.CustomLogging
7.0.0
dotnet add package Oakrey.Applications.CustomLogging --version 7.0.0
NuGet\Install-Package Oakrey.Applications.CustomLogging -Version 7.0.0
<PackageReference Include="Oakrey.Applications.CustomLogging" Version="7.0.0" />
<PackageVersion Include="Oakrey.Applications.CustomLogging" Version="7.0.0" />
<PackageReference Include="Oakrey.Applications.CustomLogging" />
paket add Oakrey.Applications.CustomLogging --version 7.0.0
#r "nuget: Oakrey.Applications.CustomLogging, 7.0.0"
#:package Oakrey.Applications.CustomLogging@7.0.0
#addin nuget:?package=Oakrey.Applications.CustomLogging&version=7.0.0
#tool nuget:?package=Oakrey.Applications.CustomLogging&version=7.0.0
Oakrey.Applications.CustomLogging
A flexible and extensible .NET logging library. It decouples the logging API from concrete logging destinations, supports broadcasting to multiple receivers simultaneously, and provides a ready-to-use WPF ViewModel for in-memory log binding.
Main features
- Multiple logging engines — console (color-coded), debug output, UTF-8 file, and in-memory
- Broadcasting —
CustomLoggerBroadcastfans out every log call to any number ofICustomLogConsumerreceivers - Structured log entries —
LogMessagerecord carries level, message, caller source, indent depth, and timestamp - Log lifecycle — explicit
StartLog/StopLog/ClearLogcontrol on every consumer - Indentation —
IncreaseIndent/DecreaseIndentfor structured hierarchical output - Contextual logging — attach an
INamecontext item to all active consumers - WPF ViewModel —
CustomLoggerViewModelcaptures the current synchronization context so background log calls are safely marshalled for XAML binding - File path customization —
ICustomLoggerFilePathProviderabstracts file naming;DocumentsCustomFilePathProviderwrites date-stamped.txtfiles toMy Documents\CustomLogs
Architecture
classDiagram
class ICustomLogger {
+LogInfo()
+LogWarning()
+LogError()
+StartLog()
+StopLog()
+ClearLog()
+IncreaseIndent()
+DecreaseIndent()
+SetCurrentContextItem()
}
class ICustomLogConsumer {
+Log(LogMessage)
+StartLog()
+StopLog()
+ClearLog()
+SetCurrentContextItem()
}
class CustomLoggerBroadcast {
-IEnumerable~ICustomLogConsumer~ receivers
}
ICustomLogger <|.. CustomLoggerBroadcast
CustomLoggerBroadcast --> ICustomLogConsumer : fans out to
ICustomLogConsumer <|.. ConsoleCustomLogger
ICustomLogConsumer <|.. FileCustomLogger
ICustomLogConsumer <|.. DebugCustomLogger
ICustomLogConsumer <|.. CustomLoggerViewModel
The key design split:
ICustomLogger— the interface used by application code to emit log entriesICustomLogConsumer— the interface implemented by each logging engineCustomLoggerBroadcast— bridges the two, routing everyICustomLoggercall to all registered consumers
Requirements
- .NET 10 or higher
- Windows (due to
net10.0-windowstarget; WPF types inCustomLoggerViewModel)
Installation
NuGet Package Manager
Search for Oakrey.Applications.CustomLogging in Tools > NuGet Package Manager > Manage NuGet Packages for Solution.
.NET CLI
dotnet add package Oakrey.Applications.CustomLogging
Package Manager Console
Install-Package Oakrey.Applications.CustomLogging
Example usage
Basic broadcast setup
// Compose consumers
CustomLoggerViewModel viewModel = new();
FileCustomLogger fileLogger = new(new DocumentsCustomFilePathProvider());
ConsoleCustomLogger consoleLogger = new();
// Create broadcast
ICustomLogger logger = new CustomLoggerBroadcast([viewModel, fileLogger, consoleLogger]);
// Use
logger.StartLog("MySession");
logger.LogInfo("Application started");
logger.IncreaseIndent();
logger.LogWarning("Configuration value missing, using default");
logger.DecreaseIndent();
logger.LogError("Unhandled exception occurred");
logger.StopLog();
WPF binding
Bind the CustomLoggerViewModel directly in XAML:
<ListView ItemsSource="{Binding LogMessages}">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Message}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Custom file path provider
Implement ICustomLoggerFilePathProvider to control where and how log files are named:
public class AppFilePathProvider : ICustomLoggerFilePathProvider
{
public DirectoryInfo GetDirectoryPath() => new(@"C:\Logs\MyApp");
public FileInfo GetFileInfo() => new($@"C:\Logs\MyApp\log_{DateTime.Now:yyyyMMdd}.txt");
}
Custom consumer
Implement ICustomLogConsumer to add any new logging destination:
public class DatabaseLogConsumer : ICustomLogConsumer
{
public void Log(LogMessage message) { /* insert to DB */ }
public void StartLog(string name) { }
public void StopLog() { }
public void ClearLog() { }
public void SetCurrentContextItem(IName item) { }
}
Configuration
| Type | Purpose |
|---|---|
DocumentsCustomFilePathProvider |
Default file path provider. Writes date-stamped .txt files to %USERPROFILE%\Documents\CustomLogs. |
ICustomLoggerFilePathProvider |
Implement to override file location and naming strategy. |
Development notes
FileCustomLoggerisIDisposable. Dispose it (or callStopLog) to flush and close the underlying stream.StartLogtruncates an existing destination andClearLogclears the active file.CustomLoggerBroadcasttracks a single shared indent level and propagates it to every consumer viaLogMessage.IndentLevel.[CallerMemberName]is used onsourceparameters so the calling method name is captured automatically without explicit argument passing.- Dependencies:
Oakrey.Collections2.1.0 (forIName),Oakrey.Files4.0.0 (for file naming), andOakrey.Log.Abstractions3.0.0 (for operational diagnostics). - Operational diagnostics use placeholder-based formatting and do not repeat application log message contents.
Project information
- Author: Oakrey
- License: MIT
- Repository: ApplicationServices
- NuGet: Oakrey.Applications.CustomLogging
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 | net10.0-windows7.0 is compatible. |
-
net10.0-windows7.0
- Oakrey.Collections (>= 2.1.0)
- Oakrey.Files (>= 4.0.0)
- Oakrey.Log.Abstractions (>= 3.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.