ozakboy.NLOG 2.1.0

dotnet add package ozakboy.NLOG --version 2.1.0                
NuGet\Install-Package ozakboy.NLOG -Version 2.1.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="ozakboy.NLOG" Version="2.1.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ozakboy.NLOG --version 2.1.0                
#r "nuget: ozakboy.NLOG, 2.1.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.
// Install ozakboy.NLOG as a Cake Addin
#addin nuget:?package=ozakboy.NLOG&version=2.1.0

// Install ozakboy.NLOG as a Cake Tool
#tool nuget:?package=ozakboy.NLOG&version=2.1.0                

Ozakboy.NLOG

nuget github

English | 繁體中文

A lightweight and high-performance logging tool that provides asynchronous writing, intelligent file management, and rich configuration options. A local logging solution designed specifically for .NET applications.

Supported Frameworks

  • .NET Framework 4.6.2
  • .NET 6.0
  • .NET 7.0
  • .NET 8.0
  • .NET Standard 2.0/2.1

Key Features

Core Features

  • πŸ“ Automatic log file and directory structure creation
  • πŸ”„ Support for asynchronous log writing to enhance application performance
  • ⚑ Smart batch processing and queue management
  • πŸ” Detailed exception information logging and serialization
  • πŸ“Š Multi-level logging support
  • πŸ›‘οΈ Thread-safe design

Advanced Features

  • βš™οΈ Flexible configuration system
  • πŸ“‚ Custom log directory structure
  • πŸ”„ Automatic file splitting and management
  • ⏰ Configurable log retention period
  • πŸ’Ύ Smart file size management
  • 🎯 Support for custom log types
  • πŸ–₯️ Optional console output

Installation

Via NuGet Package Manager:

Install-Package Ozakboy.NLOG

Or using .NET CLI:

dotnet add package Ozakboy.NLOG

Quick Start

Basic Configuration

LOG.Configure(options => {
    options.KeepDays = -7;                    // Keep logs for the last 7 days
    options.SetFileSizeInMB(50);              // Set single file size limit to 50MB
    options.EnableAsyncLogging = true;         // Enable asynchronous writing
    options.EnableConsoleOutput = true;        // Enable console output
    
    // Configure async options
    options.ConfigureAsync(async => {
        async.MaxBatchSize = 100;              // Process up to 100 logs per batch
        async.MaxQueueSize = 10000;            // Maximum queue capacity
        async.FlushIntervalMs = 1000;          // Write once per second
    });
});

Basic Usage

// Log different levels
LOG.Trace_Log("Detailed trace information");
LOG.Debug_Log("Debug information");
LOG.Info_Log("General information");
LOG.Warn_Log("Warning message");
LOG.Error_Log("Error information");
LOG.Fatal_Log("Fatal error");

// Log with parameters
LOG.Info_Log("User {0} performed {1} operation", new string[] { "admin", "login" });

// Log objects
var data = new { Id = 1, Name = "Test" };
LOG.Info_Log("Data record", data);

// Log exceptions
try {
    // Code
} catch (Exception ex) {
    LOG.Error_Log(ex);
}

// Custom log type
LOG.CustomName_Log("API", "External service call");

Log File Management

Default Directory Structure

Application Root/
└── logs/                          # Default root directory (modifiable via LogPath)
    └── yyyyMMdd/                  # Date directory
        └── LogFiles/              # Default log file directory (modifiable via TypeDirectories.DirectoryPath)
            └── [LogType]_Log.txt  # Log files

Custom Directory Structure

You can configure independent directories for different log levels:

LOG.Configure(options => {
    // Modify root directory
    options.LogPath = "CustomLogs";  // Default is "logs"
    
    // Configure independent directories for different levels
    options.TypeDirectories.DirectoryPath = "AllLogs";     // Default directory for unspecified levels
    options.TypeDirectories.ErrorPath = "ErrorLogs";       // Directory for error logs
    options.TypeDirectories.InfoPath = "InfoLogs";         // Directory for info logs
    options.TypeDirectories.WarnPath = "WarningLogs";      // Directory for warning logs
    options.TypeDirectories.DebugPath = "DebugLogs";       // Directory for debug logs
    options.TypeDirectories.TracePath = "TraceLogs";       // Directory for trace logs
    options.TypeDirectories.FatalPath = "FatalLogs";       // Directory for fatal logs
    options.TypeDirectories.CustomPath = "CustomLogs";     // Directory for custom type logs
});

Example directory structure after configuration:

Application Root/
└── CustomLogs/                    # Custom root directory
    └── yyyyMMdd/                  # Date directory
        β”œβ”€β”€ ErrorLogs/             # Error logs directory
        β”‚   └── Error_Log.txt
        β”œβ”€β”€ InfoLogs/              # Info logs directory
        β”‚   └── Info_Log.txt
        β”œβ”€β”€ WarningLogs/           # Warning logs directory
        β”‚   └── Warn_Log.txt
        └── AllLogs/               # Default directory (for unspecified log types)
            └── [LogType]_Log.txt

File Naming Rules

  • Basic format: [LogType]_Log.txt
  • Split files: [LogType]_part[N]_Log.txt
  • Custom logs: [CustomName]_Log.txt

File Size Management

LOG.Configure(options => {
    // Set single file size limit (in MB)
    options.SetFileSizeInMB(50);  // Automatically split when file reaches 50MB
});

When a file exceeds the size limit, new split files are automatically created:

  • First split file: [LogType]_part1_Log.txt
  • Second split file: [LogType]_part2_Log.txt
  • And so on...

Example Use Cases

  1. Unified log management:
LOG.Configure(options => {
    options.LogPath = "logs";
    options.TypeDirectories.DirectoryPath = "LogFiles";
});
  1. Separate error log storage:
LOG.Configure(options => {
    options.LogPath = "logs";
    options.TypeDirectories.ErrorPath = "CriticalErrors";
    options.TypeDirectories.FatalPath = "CriticalErrors";
});
  1. Fully separated logging system:
LOG.Configure(options => {
    options.LogPath = "SystemLogs";
    options.TypeDirectories.ErrorPath = "Errors";
    options.TypeDirectories.InfoPath = "Information";
    options.TypeDirectories.WarnPath = "Warnings";
    options.TypeDirectories.DebugPath = "Debugging";
    options.TypeDirectories.TracePath = "Traces";
    options.TypeDirectories.FatalPath = "Critical";
    options.TypeDirectories.CustomPath = "Custom";
});

Automatic Cleanup Mechanism

// Set log retention period
LOG.Configure(options => {
    options.KeepDays = -30; // Keep logs for the last 30 days
});

Exception Handling Features

Detailed Exception Logging

try {
    // Your code
} catch (Exception ex) {
    // Log complete exception information, including:
    // - Exception type and message
    // - Stack trace
    // - Inner exceptions
    // - Additional properties
    LOG.Error_Log(ex);
}

Custom Exception Information

try {
    // Your code
} catch (Exception ex) {
    // Add custom message
    LOG.Error_Log("Data processing failed", ex);
    
    // Also log related data
    var contextData = new { UserId = "123", Operation = "DataProcess" };
    LOG.Error_Log("Operation context", contextData);
}

Exception Serialization

try {
    // Your code
} catch (Exception ex) {
    // Exception will be automatically serialized to structured JSON format
    LOG.Error_Log(ex);
    
    // Or serialize with other information
    var errorContext = new {
        Exception = ex,
        TimeStamp = DateTime.Now,
        Environment = "Production"
    };
    LOG.Error_Log(errorContext);
}

Immediate Write Mode

Synchronous Immediate Write

// Use immediateFlush parameter to force immediate writing
LOG.Error_Log("Important error", new string[] { "error_details" }, true, true);

// For custom logs
LOG.CustomName_Log("Critical", "System anomaly", new string[] { "error_code" }, true, true);

Asynchronous Immediate Write Configuration

LOG.Configure(options => {
    options.EnableAsyncLogging = true;
    options.ConfigureAsync(async => {
        async.FlushIntervalMs = 100;     // Reduce write interval
        async.MaxBatchSize = 1;          // Set minimum batch size
        async.MaxQueueSize = 1000;       // Set appropriate queue size
    });
});

// Error and Fatal level logs automatically trigger immediate writing
LOG.Error_Log("Severe error");
LOG.Fatal_Log("System crash");

Conditional Immediate Write

// Decide whether to write immediately based on conditions
void LogMessage(string message, bool isCritical) {
    if (isCritical) {
        LOG.Error_Log(message, new string[] { }, true, true);  // Immediate write
    } else {
        LOG.Info_Log(message);  // Normal write
    }
}

Performance Optimization

  • Asynchronous writing to avoid I/O blocking
  • Smart batch processing to reduce disk operations
  • Optimized serialization mechanism
  • Thread-safe queue management
  • Automatic file management to avoid oversized files

Best Practices

  1. Choose between synchronous or asynchronous mode based on application needs
  2. Configure appropriate batch size and write intervals
  3. Adjust file size limits based on log volume
  4. Set reasonable log retention periods
  5. Use custom types for log classification
  6. Record necessary exception information at critical points

Troubleshooting

Common issue handling:

  1. File Access Permission Issues

    • Ensure application has write permissions
    • Check folder access permission settings
  2. Performance Issues

    • Adjust async configuration parameters
    • Check log file size settings
    • Optimize write frequency
  3. File Management

    • Regularly check log cleanup status
    • Monitor disk space usage

License

MIT License

Support & Reporting

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  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 is compatible.  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 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. 
.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 is compatible. 
.NET Framework net461 was computed.  net462 is compatible.  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
2.1.0 28 11/24/2024
2.0.0 79 11/12/2024
1.2.1 76 11/8/2024
1.1.6 198 6/30/2023
1.1.5 164 6/30/2023
1.1.4 157 5/15/2023
1.1.3 171 5/2/2023
1.1.2 221 4/2/2023
1.1.1 401 10/28/2022
1.0.1 377 10/28/2022
1.0.0 419 3/27/2022

Version 2.1.0 (2024):
     - Added support for .NET 8.0
     - Introduced async logging with configurable batch processing
     - Enhanced file management with automatic log rotation
     - Improved thread safety and performance
     - Added customizable directory structure for different log levels
     - Implemented intelligent file size management
     - Enhanced exception handling and serialization
     - Added support for custom log types
     - Improved configuration system with more options
     - Added console output support
     - Better handling of file paths across operating systems