diff --git a/DCGEngine.sln b/DCGEngine.sln index 23f6ffc5..11967822 100644 --- a/DCGEngine.sln +++ b/DCGEngine.sln @@ -17,6 +17,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SVSim.BattleEngine.Tests", EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SVSim.ContentServer", "SVSim.ContentServer\SVSim.ContentServer.csproj", "{AC3897E1-7745-49D0-BFA1-7D859D317C07}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SVSim.Hosting", "SVSim.Hosting\SVSim.Hosting.csproj", "{70D7E6B2-A812-497F-A254-CDD643F99A8A}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -55,5 +57,9 @@ Global {AC3897E1-7745-49D0-BFA1-7D859D317C07}.Debug|Any CPU.Build.0 = Debug|Any CPU {AC3897E1-7745-49D0-BFA1-7D859D317C07}.Release|Any CPU.ActiveCfg = Release|Any CPU {AC3897E1-7745-49D0-BFA1-7D859D317C07}.Release|Any CPU.Build.0 = Release|Any CPU + {70D7E6B2-A812-497F-A254-CDD643F99A8A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {70D7E6B2-A812-497F-A254-CDD643F99A8A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {70D7E6B2-A812-497F-A254-CDD643F99A8A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {70D7E6B2-A812-497F-A254-CDD643F99A8A}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal diff --git a/SVSim.Hosting/SVSim.Hosting.csproj b/SVSim.Hosting/SVSim.Hosting.csproj new file mode 100644 index 00000000..13547689 --- /dev/null +++ b/SVSim.Hosting/SVSim.Hosting.csproj @@ -0,0 +1,16 @@ + + + + net8.0 + enable + enable + + + + + + + + + + diff --git a/SVSim.Hosting/SerilogHostingExtensions.cs b/SVSim.Hosting/SerilogHostingExtensions.cs new file mode 100644 index 00000000..b6f91eb9 --- /dev/null +++ b/SVSim.Hosting/SerilogHostingExtensions.cs @@ -0,0 +1,42 @@ +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); + }); + } +}