K.Bag.Log4Net.Extension
0.0.1
See the version list below for details.
dotnet add package K.Bag.Log4Net.Extension --version 0.0.1
NuGet\Install-Package K.Bag.Log4Net.Extension -Version 0.0.1
<PackageReference Include="K.Bag.Log4Net.Extension" Version="0.0.1" />
<PackageVersion Include="K.Bag.Log4Net.Extension" Version="0.0.1" />
<PackageReference Include="K.Bag.Log4Net.Extension" />
paket add K.Bag.Log4Net.Extension --version 0.0.1
#r "nuget: K.Bag.Log4Net.Extension, 0.0.1"
#:package K.Bag.Log4Net.Extension@0.0.1
#addin nuget:?package=K.Bag.Log4Net.Extension&version=0.0.1
#tool nuget:?package=K.Bag.Log4Net.Extension&version=0.0.1
K.Bag.Log4Net.Extension
A powerful and flexible logging extension library built on top of log4net, providing enhanced logging capabilities with customizable formatters, writers, and configurations.
📦 Installation
Install via NuGet Package Manager:
<PackageReference Include="K.Bag.Log4Net.Extension" Version="1.0.0" />
Or via Package Manager Console:
Install-Package K.Bag.Log4Net.Extension
Or via .NET CLI:
dotnet add package K.Bag.Log4Net.Extension
💡 Basic Usage
1. Quick Setup
The easiest way to get started with logging:
using K.Bag.Log4Net.Extension;
using K.Bag.Log4Net.Extension.Configuration;
// Configure the logger once in your application startup
var config = new KlypzLoggerConfiguration();
config.SetFileFormatter(LoggerFileFormatterDefault.CsvPattern);
config.SetConsoleWriter(LoggerWriterConsoleDefault.Modern);
// Set up file logging (optional - omit the path to log only to console)
LoggerManager.ConfigureDefault(config, @"C:\logs\application.log");
// Use the logger in your classes
IKlypzCustomLogger logger = LoggerManager.GetLoggerDefault(typeof(MyClass));
logger.AddInformation("Application started successfully");
2. Advanced Configuration
Configure different logging levels and options:
var config = new KlypzLoggerConfiguration()
{
DebugEnable = LevelTypeEnable.OnlyConsole, // Debug only shown in console, not file
InfoEnable = LevelTypeEnable.Both, // Info shown in both console and file
WarnEnable = LevelTypeEnable.Both,
ErrorEnable = LevelTypeEnable.Both,
FatalEnable = LevelTypeEnable.Both,
IgnoresException = false, // Log exception details
MaximumFileSize = "10MB", // Rotate log files at 10MB
MaxSizeRollBackups = 5 // Keep up to 5 rotated log files
};
config.SetFileFormatter(LoggerFileFormatterDefault.TabPattern);
config.SetConsoleWriter(LoggerWriterConsoleDefault.ModernClean);
LoggerManager.ConfigureDefault(config, @"C:\logs\application.log");
3. Different Console Writers
Choose from three built-in console writers:
// Old school style (similar to Situator)
config.SetConsoleWriter(LoggerWriterConsoleDefault.OldSchool);
// Modern formatted output
config.SetConsoleWriter(LoggerWriterConsoleDefault.Modern);
// Clean modern output
config.SetConsoleWriter(LoggerWriterConsoleDefault.ModernClean);
4. Different File Formatters
Choose from three built-in file formatters:
// CSV-style formatting
config.SetFileFormatter(LoggerFileFormatterDefault.CsvPattern);
// Tab-separated formatting
config.SetFileFormatter(LoggerFileFormatterDefault.TabPattern);
// Traditional .log formatting
config.SetFileFormatter(LoggerFileFormatterDefault.OldSchoolPattern);
5. Using the Logger
Add different types of log messages:
IKlypzCustomLogger logger = LoggerManager.GetLoggerDefault(typeof(MyClass));
// Different log levels
logger.AddDebug("Debugging information");
logger.AddInformation("General information");
logger.AddWarning("Warning message");
logger.AddError("Error occurred");
logger.AddFatal("Fatal error");
// With exception details
try
{
// Something that throws an exception
}
catch(Exception ex)
{
logger.AddError("An error occurred processing data", ex);
logger.AddWarning("Recovering from error", ex);
}
6. Named Loggers
Create multiple loggers with different names for different purposes:
IKlypzCustomLogger dataAccessLogger = LoggerManager.GetLoggerDefault("Data Access");
IKlypzCustomLogger businessLogicLogger = LoggerManager.GetLoggerDefault("Business Logic");
dataAccessLogger.AddDebug("Executing query took 250ms");
businessLogicLogger.AddInformation("User authentication successful");
7. Custom File Formatting
Define custom columns for your log files:
var config = new KlypzLoggerConfiguration();
// Define custom column layout
var columns = new[]
{
ColumnLog.DateTime,
ColumnLog.Level,
ColumnLog.Name,
ColumnLog.Message,
ColumnLog.ExceptionMessage
};
config.SetFileFormatter(LoggerFileFormatterDefault.CsvPattern);
config.SetColumnsFormatter(columns);
LoggerManager.ConfigureDefault(config, @"C:\logs\application.log");
⚙️ Configuration Options
| Option | Type | Default | Description |
|---|---|---|---|
| DebugEnable | LevelTypeEnable | Both | Control where DEBUG level messages appear |
| InfoEnable | LevelTypeEnable | Both | Control where INFO level messages appear |
| WarnEnable | LevelTypeEnable | Both | Control where WARN level messages appear |
| ErrorEnable | LevelTypeEnable | Both | Control where ERROR level messages appear |
| FatalEnable | LevelTypeEnable | Both | Control where FATAL level messages appear |
| IgnoresException | bool | false | Whether to include exception details |
| MaximumFileSize | string | 1MB | Size when to rotate log files |
| MaxSizeRollBackups | int | 5 | Number of rotated files to keep |
Available LevelTypeEnable values:
OnlyFile- Messages appear only in fileOnlyConsole- Messages appear only in consoleBoth- Messages appear in both file and console
🔧 Supported Column Types
When configuring file formatters, you can use these column types:
ColumnLog.Name- Class or grouping nameColumnLog.FullName- Full class name (namespace + class)ColumnLog.DateTime- Timestamp of log entryColumnLog.Level- Log level (DEBUG, INFO, WARN, ERROR, FATAL)ColumnLog.Message- Log messageColumnLog.FullMessage- Full message including line breaksColumnLog.ExceptionMessage- Exception message if providedColumnLog.ExceptionStackTrace- Exception stack trace if providedColumnLog.Thread- Thread ID where log occurred
🛠️ Logger Methods
| Method | Description |
|---|---|
AddDebug(string message) |
Log a debug message |
AddDebug(string message, Exception e) |
Log a debug message with exception details |
AddInformation(string message) |
Log an info message |
AddWarning(string message) |
Log a warning message |
AddWarning(string message, Exception e) |
Log a warning message with exception details |
AddError(string message) |
Log an error message |
AddError(string message, Exception e) |
Log an error message with exception details |
AddFatal(string message) |
Log a fatal message |
AddFatal(string message, Exception e) |
Log a fatal message with exception details |
✅ Best Practices
- Initialize Once: Configure the logger manager only once during application startup
- Use Named Loggers: For different components of your application
- Keep Debug Off in Production: Set
DebugEnable = LevelTypeEnable.OnlyConsoleor disable for production - Manage File Sizes: Use appropriate
MaximumFileSizeandMaxSizeRollBackupssettings - Include Exceptions: Always include exception details when logging errors
📋 Dependencies
This package depends on:
- log4net
📄 License
See the LICENSE file for licensing information.
🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
| 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 | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 is compatible. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | 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.1
- log4net (>= 2.0.15)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.8)
- Microsoft.Extensions.Http (>= 9.0.8)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.