Novelog is a simple and flexible logging library for .NET applications. It supports multiple log handlers, message enrichment, and formatting.
- Log to multiple handlers (e.g., console, file)
- Rolling file log handler with size-based rotation
- Custom log handlers
To install Novelog, add the following NuGet package to your project:
dotnet add package Novelog
To use the logger, you need to configure it first. You can configure the logger using Dependency Injection (DI) or manually. The shared logger instance will be set up automatically when using either method.
- Add the logger to your service collection
Program.cs
:
using Microsoft.Extensions.DependencyInjection;
using Novelog;
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddLogger(config =>
{
config.AttachConsole();
});
}
}
- Inject and use the logger in your classes:
using Novelog;
public class MyClass
{
private readonly ILogger _logger;
public MyClass(ILogger logger)
{
_logger = logger;
}
public void DoSomething()
{
_logger.LogInfo("This is an info message.");
}
}
- Configure the logger manually:
using Novelog;
using Novelog.Config;
var logger = new LoggerConfigBuilder()
.AttachConsole()
.Build();
- Use the shared logger:
using Novelog;
public class MyClass
{
public void DoSomething()
{
Logger.Shared.LogInfo("This is an info message.");
}
}
The rolling file log handler rotates log files based on size. Configure it using the RollingFileConfig
class:
using Novelog.Config;
var rollingFileConfig = new RollingFileConfig
{
FilePath = "logs/log.txt",
MaxFileSize = 10 * 1024 * 1024,
MaxFileCount = 5,
MinLogLevel = LogLevel.INFO
};
var logger = new LoggerConfigBuilder()
.AttachRollingFile(rollingFileConfig)
.Build();
This project is licensed under the MIT License. See the LICENSE
file for details.
Contributions are welcome! Please open an issue or submit a pull request.