From: js6pak Date: Fri, 3 Jun 2022 18:31:52 +0000 (+0200) Subject: Add Serilog.Settings.Configuration (#468) X-Git-Tag: v1.7.0~3 X-Git-Url: https://git.deb.at/?a=commitdiff_plain;h=6e4bb71289343d6dc5d24f56934a6474a04d7fd8;p=rhonda%2Fimpostor.git Add Serilog.Settings.Configuration (#468) * Add Serilog.Settings.Configuration * Add docs section for logging * Elaborate Serilog documentation Co-authored-by: miniduikboot --- diff --git a/docs/Server-configuration.md b/docs/Server-configuration.md index 8ae1d07..58ec29e 100644 --- a/docs/Server-configuration.md +++ b/docs/Server-configuration.md @@ -53,7 +53,7 @@ The Debug configuration is used to enable the game recorder. This is mostly usef | Key | Default | Value | | ----------------------- | ------- | -------------------------------------------- | | **GameRecorderEnabled** | `false` | Enables the Game Recorder. | -| **GameRecorderPath** | *empty* | Path where the recorded games will be saved. | +| **GameRecorderPath** | _empty_ | Path where the recorded games will be saved. | ### ServerRedirector @@ -68,6 +68,41 @@ In a multi-node setup these values need to be specified. Note that most people d | **>UdpMasterEndpoint** | | On the master, this value acts as a listen ip and port. On a node, this should be the public ip and port of the master. Format `127.0.0.1:32320`. | | **Nodes** | | An array containing public ips and ports of nodes. Only needs to be set on the master. See above for an example. | +### Serilog (Logging) + +Impostor's log framework, Serilog, can be configured in the config file. You can change its default log level and you can add additional sinks. + +| Key | Default | Value | +| ---------------- | ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| **MinimumLevel** | `Information` | Minimum log level for a message to be printed. If a log entry is as severe or more severe than this level, it will be printed. From most severe to least severe: `Fatal`,`Error`, `Warning`, `Information`, `Debug`, `Verbose` | +| **Using** | `[]` | List of additional Serilog Sinks assemblies to load. | +| **WriteTo** | `[]` | Additional logging sinks. See the Serilog documentation or the example in this section. Serilog | + +For more information, check the [Serilog.Settings.Configuration](https://github.com/serilog/serilog-settings-configuration) documentation. + +For example, to add logging to a file, you should add the following snippet to your configuration: + +```json +"Serilog": { + "Using": [ + "Serilog.Sinks.File" + ], + "WriteTo": [ + { + "Name": "File", + "Args": { + "path": "logs/log.txt", + "rollingInterval": "Day" + } + } + ] +} +``` + +Next to that, you also need to copy over Serilog.Sinks.File.dll from [NuGet](https://www.nuget.org/packages/Serilog.Sinks.File/). See the [Serilog.Sinks.File documentation](https://github.com/serilog/serilog-sinks-file#json-appsettingsjson-configuration) for a list of parameters that can be configured. + +Other Serilog sinks are also supported, but are out of scope for this documentation. + ## Config providers ### File diff --git a/src/Impostor.Server/Impostor.Server.csproj b/src/Impostor.Server/Impostor.Server.csproj index 03ceb76..f05e3c7 100644 --- a/src/Impostor.Server/Impostor.Server.csproj +++ b/src/Impostor.Server/Impostor.Server.csproj @@ -29,6 +29,7 @@ + all diff --git a/src/Impostor.Server/Program.cs b/src/Impostor.Server/Program.cs index 96cca63..219620b 100644 --- a/src/Impostor.Server/Program.cs +++ b/src/Impostor.Server/Program.cs @@ -1,6 +1,8 @@ using System; using System.IO; using System.Linq; +using System.Reflection; +using System.Runtime.Loader; using Impostor.Api.Events.Managers; using Impostor.Api.Games; using Impostor.Api.Games.Managers; @@ -26,6 +28,7 @@ using Microsoft.Extensions.Hosting; using Microsoft.Extensions.ObjectPool; using Serilog; using Serilog.Events; +using Serilog.Settings.Configuration; namespace Impostor.Server { @@ -33,31 +36,9 @@ namespace Impostor.Server { private static int Main(string[] args) { -#if DEBUG - var logLevel = LogEventLevel.Debug; -#else - var logLevel = LogEventLevel.Information; -#endif - - if (args.Contains("--verbose")) - { - logLevel = LogEventLevel.Verbose; - } - else if (args.Contains("--errors-only")) - { - logLevel = LogEventLevel.Error; - } - Log.Logger = new LoggerConfiguration() - .MinimumLevel.Is(logLevel) -#if DEBUG - .MinimumLevel.Override("Microsoft", LogEventLevel.Debug) -#else - .MinimumLevel.Override("Microsoft", LogEventLevel.Information) -#endif - .Enrich.FromLogContext() .WriteTo.Console() - .CreateLogger(); + .CreateBootstrapLogger(); try { @@ -209,7 +190,55 @@ namespace Impostor.Server services.AddSingleton(); services.AddHostedService(); }) - .UseSerilog() + .UseSerilog((context, loggerConfiguration) => + { +#if DEBUG + var logLevel = LogEventLevel.Debug; +#else + var logLevel = LogEventLevel.Information; +#endif + + if (args.Contains("--verbose")) + { + logLevel = LogEventLevel.Verbose; + } + else if (args.Contains("--errors-only")) + { + logLevel = LogEventLevel.Error; + } + + static Assembly? LoadSerilogAssembly(AssemblyLoadContext loadContext, AssemblyName name) + { + var paths = new[] { AppDomain.CurrentDomain.BaseDirectory, Directory.GetCurrentDirectory() }; + foreach (var path in paths) + { + try + { + return loadContext.LoadFromAssemblyPath(Path.Combine(path, name.Name + ".dll")); + } + catch (FileNotFoundException) + { + } + } + + return null; + } + + AssemblyLoadContext.Default.Resolving += LoadSerilogAssembly; + + loggerConfiguration + .MinimumLevel.Is(logLevel) +#if DEBUG + .MinimumLevel.Override("Microsoft", LogEventLevel.Debug) +#else + .MinimumLevel.Override("Microsoft", LogEventLevel.Information) +#endif + .Enrich.FromLogContext() + .WriteTo.Console() + .ReadFrom.Configuration(context.Configuration, ConfigurationAssemblySource.AlwaysScanDllFiles); + + AssemblyLoadContext.Default.Resolving -= LoadSerilogAssembly; + }) .UseConsoleLifetime() .UsePluginLoader(pluginConfig); }