diff --git a/docs/articles/utilities/serilog.md b/docs/articles/utilities/serilog.md index a22bae6a104..61d86c2bc97 100644 --- a/docs/articles/utilities/serilog.md +++ b/docs/articles/utilities/serilog.md @@ -6,14 +6,20 @@ title: Serilog # Using Serilog ## Setup -Install the package __Akka.Logger.Serilog__ to utilize -[Serilog](http://serilog.net/) +Install the package __Akka.Logger.Serilog__ via nuget to utilize +[Serilog](https://serilog.net/), this will also install the required Serilog package dependencies. ``` PM> Install-Package Akka.Logger.Serilog ``` -This will also install the required Serilog packages. +## Example + +The following example uses Serilogs __Colored 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 serilogs documentation for details on these. + +``` +PM> Install-Package Serilog.Sinks.ColoredConsole +``` Next, you'll need to configure the global `Log.Logger` and also specify to use the logger in the config when creating the system, for example like this: @@ -22,7 +28,9 @@ var logger = new LoggerConfiguration() .WriteTo.ColoredConsole() .MinimumLevel.Information() .CreateLogger(); + Serilog.Log.Logger = logger; + var system = ActorSystem.Create("my-test-system", "akka { loglevel=INFO, loggers=[\"Akka.Logger.Serilog.SerilogLogger, Akka.Logger.Serilog\"]}"); ``` @@ -35,9 +43,9 @@ var log = Context.GetLogger(); log.Info("The value is {0}", counter); ``` -To log using Serilog syntax you need to use the `SerilogLogMessageFormatter`: +Or alternatively ```csharp -var log = Context.GetLogger(new SerilogLogMessageFormatter()); +var log = Context.GetLogger(); ... log.Info("The value is {Counter}", counter); ``` @@ -55,15 +63,23 @@ private readonly ILoggingAdapter _logger = Context.GetLogger()`. +If the configured output template is, for example, `"[{CorrelationId}] {Message}{NewLine}"`, and the parameter `correlationId` is `"1234"` then the resulting log would contain the line `[1234] Processing message`. + +```csharp +// configure sink with an output template +var logger = new LoggerConfiguration() + .WriteTo.ColoredConsole(outputTemplate: "[{CorrelationId}] {Message}{NewLine}") + .MinimumLevel.Information() + .CreateLogger(); +``` ## HOCON configuration -In order to be able to change log level without the need to recompile, we need to employ some sort of configuration. To use Serilog via HOCON configuration, add the following to the App.config +In order to be able to change log level without the need to recompile, we need to employ some sort of application configuration. To use Serilog via HOCON configuration, add the following to the __App.config__ of the project. ```xml @@ -85,13 +101,16 @@ In order to be able to change log level without the need to recompile, we need t ``` -The code can then be updated as follows removing the inline HOCON. Additionally, in this example, we use Serilog's ability to configure itself through the App.config. For further information see [Serilog AppSettings](https://github.com/serilog/serilog/wiki/AppSettings) +The code can then be updated as follows removing the inline HOCON from the actor system creation code. Note in the following example, if a minimum level is not specfied, Information level events and higher will be processed. Please read the documentation for [Serilog](https://serilog.net/) configuration for more details on this. It is also possible to move serilog configuration to the application configuration, for example if using a rolling log file sink, again, browsing the serilog documentation is the best place for details on that feature. ```csharp var logger = new LoggerConfiguration() - .ReadFrom.AppSettings() - .CreateLogger(); + .WriteTo.ColoredConsole() + .MinimumLevel.Information() + .CreateLogger(); + Serilog.Log.Logger = logger; + var system = ActorSystem.Create("my-test-system"); ```