Plinth Common logging library for host projects to log to standard targets
$ dotnet add package Plinth.Logging.HostLogging utilities for hosted applications and services
Provides simple logging configuration for console applications, Windows services, and Azure Functions using the Plinth logging ecosystem.
:point_right: For sophisticated logging scenarios, use Plinth.Logging.NLog instead.
Configure logging to the console with sensible defaults:
using Plinth.Logging.Host;
class Program
{
static void Main(string[] args)
{
var log = StaticLogManagerSetup.ConfigureForConsoleLogging();
log.Info("Application started");
log.Debug("Debug information");
log.Warn("Warning message");
log.Error("Error occurred");
}
}
With custom formatting:
var log = StaticLogManagerSetup.ConfigureForConsoleLogging(
minLevel: LogLevel.Debug,
configAction: c =>
{
c.ColorBehavior = LoggerColorBehavior.Enabled;
c.SingleLine = true;
c.TimestampFormat = "yyyy-MM-dd HH:mm:ss ";
});
Configure logging to debug output (useful for unit tests and debugging):
var log = StaticLogManagerSetup.ConfigureForDebugLogging(LogLevel.Debug);
log.Debug("This will appear in debug output");
Simple file logging with optional rolling files:
var log = StaticLogManagerSetup.ConfigureForFileLogging(
minLevel: LogLevel.Information,
filePath: "app.log");
log.Info("This will be written to app.log");
With rolling file options:
var options = new FileLoggerOptions
{
Append = true,
FileSizeLimitBytes = 10 * 1024 * 1024, // 10 MB
MaxRollingFiles = 5 // Keep up to 5 log files
};
var log = StaticLogManagerSetup.ConfigureForFileLogging(
minLevel: LogLevel.Information,
filePath: "app.log",
options: options);
Load logging configuration from your appsettings.json file:
var log = StaticLogManagerSetup.FromAppSettings();
// Or specify custom paths
var log = StaticLogManagerSetup.FromAppSettings(
configRoot: "Logging",
appSettings: "appsettings.json");
Example appsettings.json:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"System": "Warning",
"Microsoft": "Warning"
},
"Console": {
"IncludeScopes": true
},
"Debug": { },
"File": {
"Path": "logs/app.log",
"Append": true,
"MinLevel": "Information",
"FileSizeLimitBytes": 10485760,
"MaxRollingFiles": 5
}
}
}
:point_right: Include "Console", "Debug", or "File" sections to enable those providers.
Use with a custom configuration source:
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{environment}.json", optional: true)
.Build();
var log = StaticLogManagerSetup.FromAppSettings(config, "Logging");
For Azure Functions where you're passed an ILogger:
public class MyFunction
{
[FunctionName("MyFunction")]
public async Task Run(
[TimerTrigger("0 */5 * * * *")] TimerInfo timer,
ILogger logger)
{
// Configure Plinth to use the Azure Functions logger
StaticLogManagerSetup.ConfigureForSingleThread(logger);
// Now all Plinth components will use the Azure Functions logger
var myService = new MyService();
await myService.DoWork();
}
}
public class MyService
{
private static readonly ILogger log = StaticLogManager.GetLogger();
public async Task DoWork()
{
log.Info("This will log through Azure Functions logger");
}
}
Or with ILoggerFactory:
public class MyFunction
{
private readonly ILoggerFactory _loggerFactory;
public MyFunction(ILoggerFactory loggerFactory)
{
_loggerFactory = loggerFactory;
}
[FunctionName("MyFunction")]
public async Task Run([TimerTrigger("0 */5 * * * *")] TimerInfo timer)
{
StaticLogManagerSetup.ConfigureForSingleThread(_loggerFactory);
// All Plinth components can now log
var myService = new MyService();
await myService.DoWork();
}
}
Configure file logging behavior:
public class FileLoggerOptions
{
// Append to existing file (true) or overwrite (false)
public bool Append { get; set; } = true;
// Enable rolling files when file reaches this size (0 = disabled)
public long FileSizeLimitBytes { get; set; } = 0;
// Maximum number of rolling files to keep (0 = unlimited)
public int MaxRollingFiles { get; set; } = 0;
}
When FileSizeLimitBytes is set:
app.log, app1.log, app2.log, etc.When MaxRollingFiles is also set:
MaxRollingFiles = 3: creates app.log, app1.log, app2.log, then overwrites app.logAll loggers configured with StaticLogManagerSetup work seamlessly with the Plinth ecosystem:
using Plinth.Logging.Host;
class Program
{
static void Main()
{
var log = StaticLogManagerSetup.ConfigureForConsoleLogging();
log.Info("Application started");
// Use Plinth's logging extensions
using (log.LogExecTiming("Initialization"))
{
// Timed code block
Initialize();
}
log.Info("Application ready");
}
}
Choose the Right Logger
Azure Functions
ConfigureForSingleThread() to integrate with Azure Functions loggingConfiguration
Log Levels
Information as the default minimum level for productionDebug for development environmentsTrace in production (too verbose)Rolling Files
MaxRollingFiles when using FileSizeLimitBytes