diff --git a/docs/articles/utilities/serilog.md b/docs/articles/utilities/serilog.md index 78f5a52ae78..579a6687820 100644 --- a/docs/articles/utilities/serilog.md +++ b/docs/articles/utilities/serilog.md @@ -14,6 +14,38 @@ Install the package __Akka.Logger.Serilog__ via nuget to utilize PM> Install-Package Akka.Logger.Serilog ``` +### ASP.NET Core + +If you are using Akka.NET in an ASP.NET Core application, follow the [Serilog guide](https://github.com/serilog/serilog-aspnetcore) to setup a logger injection. Akka.NET also works with Serilog configured using [two-stage initialization](https://github.com/serilog/serilog-aspnetcore#two-stage-initialization). + +It is important to remember that logging level is first controlled by a Serilog sink, and only then by Akka.NET, as described in this [document](https://github.com/serilog/serilog/wiki/Configuration-Basics): + +```text +Logger vs. sink minimums - it is important to realize that the logging level can only be raised for sinks, not lowered. So, if the logger's MinimumLevel is set to Information then a sink with Debug as its specified level will still only see Information level events. +``` + +Thus, if Akka.NET log level is `DEBUG`, but a Serilog sink log level is `INFO`, all debug messages from Akka will be filtered out. You can set the default logging level on host startup: + +```csharp +public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) + .UseSerilog((hostingContext, services, loggerConfiguration) => + (Environment.GetEnvironmentVariable("MY_AKKA_APP__DEFAULT_LOG_LEVEL") switch + { + "INFO" => loggerConfiguration.MinimumLevel.Information(), + "WARN" => loggerConfiguration.MinimumLevel.Warning(), + "ERROR" => loggerConfiguration.MinimumLevel.Error(), + "DEBUG" => loggerConfiguration.MinimumLevel.Debug(), + _ => loggerConfiguration.MinimumLevel.Information() + }) + .WriteTo.Console() + .ReadFrom.Services(services) + .Enrich.FromLogContext() + .EnrichWithCommonProperties(...) + ... // continue logger configuration); +``` + +`Serilog.Log.Logger` will be initialized during host bootstrap and Akka.NET will use the final configured logger. + ## Example The following example uses Serilog's __Console__ sink available via nuget, there are wide range of other sinks available depending on your needs, for example a rolling log file sink. See serilog's documentation for details on these.