]> git.deb.at Git - rhonda/impostor.git/commitdiff
Add Serilog.Settings.Configuration (#468)
authorjs6pak <kubastaron@hotmail.com>
Fri, 3 Jun 2022 18:31:52 +0000 (20:31 +0200)
committerGitHub <noreply@github.com>
Fri, 3 Jun 2022 18:31:52 +0000 (20:31 +0200)
* Add Serilog.Settings.Configuration

* Add docs section for logging

* Elaborate Serilog documentation

Co-authored-by: miniduikboot <mini@duikbo.at>
docs/Server-configuration.md
src/Impostor.Server/Impostor.Server.csproj
src/Impostor.Server/Program.cs

index 8ae1d0782a5a685e15ce8858fc5febc381ecfee6..58ec29e077a16f9c4c0615c4c310a6028baf75b6 100644 (file)
@@ -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
index 03ceb76f468aacd575786eedb7660be51f3e0656..f05e3c7b30690426e31bf1708de50e09a1824269 100644 (file)
@@ -29,6 +29,7 @@
     <PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
     <PackageReference Include="Microsoft.Extensions.ObjectPool" Version="6.0.5" />
     <PackageReference Include="Serilog.Extensions.Hosting" Version="4.2.0" />
+    <PackageReference Include="Serilog.Settings.Configuration" Version="3.3.0" />
     <PackageReference Include="Serilog.Sinks.Console" Version="4.0.1" />
     <PackageReference Include="StyleCop.Analyzers.Unstable" Version="1.2.0.435">
       <PrivateAssets>all</PrivateAssets>
index 96cca63f0ffbc5baba34c9b9e36ddf31ca659e63..219620b01faeb69f3d0445c8715b134d707ae562 100644 (file)
@@ -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<Matchmaker>();
                     services.AddHostedService<MatchmakerService>();
                 })
-                .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);
         }