Adds new SVSim.Hosting project carrying Serilog + Serilog.AspNetCore + Serilog.Settings.Configuration + enricher package refs, and a single IHostBuilder extension method UseSvSimSerilog(appName) that installs a colored console sink and a plain-text file sink rolling daily with a 100 MB per-file cap and 7-file retention. ReadFrom.Configuration is called last so appsettings can override minimum levels per category. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
43 lines
1.9 KiB
C#
43 lines
1.9 KiB
C#
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Serilog;
|
|
using Serilog.Events;
|
|
using Serilog.Sinks.SystemConsole.Themes;
|
|
|
|
namespace SVSim.Hosting;
|
|
|
|
public static class SerilogHostingExtensions
|
|
{
|
|
// Wires Serilog as the host's logging provider. Sinks, rotation, retention,
|
|
// and enrichment defaults are baked in here; per-category level overrides
|
|
// ride via appsettings under "Serilog:MinimumLevel:Override". The file sink
|
|
// rolls daily (or when a single file hits 100 MB) and retains the newest 7.
|
|
public static IHostBuilder UseSvSimSerilog(this IHostBuilder host, string appName)
|
|
{
|
|
var logDirectory = Path.Combine(AppContext.BaseDirectory, "logs");
|
|
var filePathBase = Path.Combine(logDirectory, $"{appName}-.log");
|
|
|
|
return host.UseSerilog((context, services, loggerConfig) =>
|
|
{
|
|
loggerConfig
|
|
.MinimumLevel.Information()
|
|
.MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning)
|
|
.MinimumLevel.Override("Microsoft.EntityFrameworkCore", LogEventLevel.Warning)
|
|
.Enrich.FromLogContext()
|
|
.Enrich.WithMachineName()
|
|
.Enrich.WithThreadId()
|
|
.WriteTo.Console(
|
|
theme: AnsiConsoleTheme.Code,
|
|
outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {SourceContext} {Message:lj}{NewLine}{Exception}")
|
|
.WriteTo.File(
|
|
path: filePathBase,
|
|
rollingInterval: RollingInterval.Day,
|
|
rollOnFileSizeLimit: true,
|
|
fileSizeLimitBytes: 100_000_000,
|
|
retainedFileCountLimit: 7,
|
|
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {SourceContext} {Message:lj}{NewLine}{Exception}")
|
|
.ReadFrom.Configuration(context.Configuration);
|
|
});
|
|
}
|
|
}
|