First commit
This commit is contained in:
142
Logger/README.md
Normal file
142
Logger/README.md
Normal file
@@ -0,0 +1,142 @@
|
||||
# Custom Logger Project
|
||||
|
||||
A flexible .NET logging library that supports both console and file logging with configurable options.
|
||||
|
||||
## Features
|
||||
|
||||
- **Multiple Output Targets**: Log to console and file simultaneously
|
||||
- **Configurable Log Levels**: Trace, Debug, Info, Warning, Error, Fatal
|
||||
- **File Rotation**: Automatically rotates log files based on size limits
|
||||
- **Colored Console Output**: Different colors for each log level
|
||||
- **Thread-Safe**: Safe for multi-threaded applications
|
||||
- **Flexible Configuration**: Customize file names, directories, and size limits
|
||||
|
||||
## Log Levels
|
||||
|
||||
| Level | Value | Description |
|
||||
|-------|-------|-------------|
|
||||
| Trace | 0 | Very detailed diagnostic information |
|
||||
| Debug | 1 | Detailed information for debugging |
|
||||
| Info | 2 | Informational messages |
|
||||
| Warning | 3 | Warning messages for potentially harmful situations |
|
||||
| Error | 4 | Error messages for error events |
|
||||
| Fatal | 5 | Fatal error messages |
|
||||
|
||||
## Usage
|
||||
|
||||
### Basic Setup in Program.cs
|
||||
|
||||
```csharp
|
||||
using Logger;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Configure file logger options
|
||||
var fileLoggerOptions = new FileLoggerOptions
|
||||
{
|
||||
FileName = "app.log", // Name of the log file
|
||||
LogDirectory = "Logs", // Directory to store logs
|
||||
MaxFileSizeBytes = 10 * 1024 * 1024, // 10 MB
|
||||
MaxBackupFiles = 5, // Keep 5 backup files
|
||||
MinimumLevel = LogLevel.Info // Only log Info and above to file
|
||||
};
|
||||
|
||||
// Create and register the composite logger (console + file)
|
||||
builder.Services.AddSingleton<Logger.ILogger>(
|
||||
Logger.LoggerFactory.CreateCompositeLogger(Logger.LogLevel.Debug, fileLoggerOptions));
|
||||
|
||||
var app = builder.Build();
|
||||
```
|
||||
|
||||
### Using the Logger in Controllers
|
||||
|
||||
```csharp
|
||||
using Logger;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
public class AuthController : ControllerBase
|
||||
{
|
||||
private readonly Logger.ILogger _logger;
|
||||
|
||||
public AuthController(Logger.ILogger logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
[HttpPost("login")]
|
||||
public IActionResult Login([FromBody] LoginRequest request)
|
||||
{
|
||||
_logger.Info($"Login attempt for user: {request.Email}");
|
||||
|
||||
try
|
||||
{
|
||||
// authentication logic
|
||||
_logger.Info($"User {request.Email} logged in successfully");
|
||||
return Ok(new { token = "..." });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Error($"Login failed for user {request.Email}: {ex.Message}");
|
||||
return Unauthorized();
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Using Different Logger Types
|
||||
|
||||
```csharp
|
||||
// Console only
|
||||
var consoleLogger = Logger.LoggerFactory.CreateConsoleLogger(Logger.LogLevel.Debug);
|
||||
|
||||
// File only
|
||||
var fileLogger = Logger.LoggerFactory.CreateFileLogger(fileLoggerOptions);
|
||||
|
||||
// Console and file (composite)
|
||||
var compositeLogger = Logger.LoggerFactory.CreateCompositeLogger(
|
||||
Logger.LogLevel.Debug,
|
||||
fileLoggerOptions
|
||||
);
|
||||
```
|
||||
|
||||
## Configuration Options
|
||||
|
||||
### FileLoggerOptions
|
||||
|
||||
| Property | Type | Default | Description |
|
||||
|----------|------|---------|-------------|
|
||||
| FileName | string | "app.log" | Name of the log file |
|
||||
| LogDirectory | string | "Logs" | Directory where logs are stored |
|
||||
| MaxFileSizeBytes | long | 10485760 (10 MB) | Maximum size before rotation |
|
||||
| MaxBackupFiles | int | 5 | Number of backup files to keep |
|
||||
| MinimumLevel | LogLevel | Info | Minimum level to log to file |
|
||||
|
||||
## Log Output Format
|
||||
|
||||
Logs are formatted with timestamps and log levels:
|
||||
|
||||
```
|
||||
[2024-01-15 14:30:45.123] [Info] User authentication successful
|
||||
[2024-01-15 14:30:46.456] [Error] Database connection timeout
|
||||
[2024-01-15 14:30:47.789] [Warning] High memory usage detected
|
||||
```
|
||||
|
||||
## File Rotation
|
||||
|
||||
When a log file reaches the `MaxFileSizeBytes` limit:
|
||||
|
||||
1. The current log file is renamed with a timestamp: `app_2024-01-15_14-30-45.log`
|
||||
2. A new `app.log` file is created
|
||||
3. Old backup files exceeding `MaxBackupFiles` limit are automatically deleted
|
||||
|
||||
## Thread Safety
|
||||
|
||||
The FileLogger uses a lock mechanism to ensure thread-safe file writing. Multiple threads can safely call logging methods simultaneously.
|
||||
|
||||
## Error Handling
|
||||
|
||||
If any logging operation fails:
|
||||
- Console errors are written to `Console.Error`
|
||||
- Errors in one logger don't affect others (in composite logger)
|
||||
- The application continues running normally
|
||||
Reference in New Issue
Block a user