feat: add -D/-V/-DV debug/trace CLI flags and file logging support

This commit is contained in:
Joseph Doherty
2026-02-23 00:41:49 -05:00
parent 0ec5583422
commit 4ad821394b
3 changed files with 40 additions and 6 deletions

View File

@@ -1,12 +1,6 @@
using NATS.Server;
using Serilog;
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.Enrich.FromLogContext()
.WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}")
.CreateLogger();
var options = new NatsOptions();
// Simple CLI argument parsing
@@ -55,9 +49,47 @@ for (int i = 0; i < args.Length; i++)
case "--tlsverify":
options.TlsVerify = true;
break;
case "-D" or "--debug":
options.Debug = true;
break;
case "-V" or "--trace":
options.Trace = true;
break;
case "-DV":
options.Debug = true;
options.Trace = true;
break;
case "-l" or "--log" when i + 1 < args.Length:
options.LogFile = args[++i];
break;
case "--log_size_limit" when i + 1 < args.Length:
options.LogSizeLimit = long.Parse(args[++i]);
break;
}
}
// Configure Serilog based on options
var logConfig = new LoggerConfiguration()
.Enrich.FromLogContext()
.WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}");
if (options.Trace)
logConfig.MinimumLevel.Verbose();
else if (options.Debug)
logConfig.MinimumLevel.Debug();
else
logConfig.MinimumLevel.Information();
if (options.LogFile != null)
{
logConfig.WriteTo.File(
options.LogFile,
fileSizeLimitBytes: options.LogSizeLimit > 0 ? options.LogSizeLimit : null,
rollOnFileSizeLimit: options.LogSizeLimit > 0);
}
Log.Logger = logConfig.CreateLogger();
using var loggerFactory = new Serilog.Extensions.Logging.SerilogLoggerFactory(Log.Logger);
using var server = new NatsServer(options, loggerFactory);