Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address Serilog documentation issues raised as per #3247 #3254

Merged
merged 3 commits into from
Jan 9, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 30 additions & 11 deletions docs/articles/utilities/serilog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good

```

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:
Expand All @@ -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\"]}");
```

Expand All @@ -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());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why remove this? Isn't that necessary to get the semantic Serilog-style formatting to work?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, I found when I ran the examples, that it did not seem to make a difference in output whether the formatter was included or not. I felt if it was not necessary or no longer necessary, it was probably best to remove from the example. It seems allot of this structuring of output is handled in serilog - https://github.com/serilog/serilog/wiki/Structured-Data#default-behaviour - some of this page seems to apply to 2.6.0 but the following seems to apply for 2.5.0

Log.Information("The value is {counter}", counter);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

funnily, the following works too

private void Handle(int count)
{
	var log = Context.GetLogger();
	log.Info("Count is {a} {b} {c}", count, 2, true);
}

2018-01-08 18:23:12 INF Count is 1 2 True

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct. The SerilogLogMessageFormatter is no longer needed.

var log = Context.GetLogger();
...
log.Info("The value is {Counter}", counter);
```
Expand All @@ -55,15 +63,23 @@ private readonly ILoggingAdapter _logger = Context.GetLogger<SerilogLoggingAdapt
private void ProcessMessage(string correlationId)
{
var contextLogger = _logger.ForContext("CorrelationId", correlationId);
contextLogger.Info("Processing message.");
contextLogger.Info("Processing message");
}
```

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.`. This can work for message templates as well, however, as specified in a previous section, a `SerilogLogMessageFormatter` instance would have to be provided to `GetLogger<SerilogLoggingAdapter>()`.
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
<configSections>
Expand All @@ -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");
```

Expand Down