8

I'm trying to use a couple of enrichers (machine name and thread ID for now) in conjunction with a rolling file sink and a Loggly sink. Whilst the Loggly events correctly contain the machine name and thread ID properties, I can't see these in the rolling file events.

Here is my xml/code configuration:

<add key="serilog:minimum-level" value="Information" /> 
<add key="serilog:write-to:RollingFile.pathFormat" value="C:\Foo\bar-{Date}.txt" />
<add key="serilog:using" value="Serilog.Sinks.Loggly" />
<add key="serilog:write-to:Loggly.inputKey" value="redacted Loggly key" /> 

new LoggerConfiguration()
    .ReadAppSettings()
    .Enrich.WithMachineName()
    .Enrich.WithThreadId()
    .CreateLogger()

Did anyone manage to do this? Could this behaviour be by design or are these enrichers not supported for rolling file sinks?

CyberDude
  • 8,541
  • 5
  • 29
  • 47

2 Answers2

16

The machinename and threadid are added as properties to all log events. They are not in the messageformat so serilog does not convert them to a textual representation. They will however be send to the sinks. The Loggly sink will pick all the properties (including the thread id etc) and convert those to something Loggly understands (as it can accept any kind of data).

If you want the RollingFile sink to also output the machinename etc, you need to adjust te output template. So setting it to for example this:

outputTemplate: "{Timestamp:HH:mm} [{Level}] {MachineName} ({ThreadId}) {Message}{NewLine}{Exception}"

See also https://github.com/serilog/serilog/wiki/Configuration-Basics#enrichers

Since the rolling file sink has no way to output all the properties, you only get the rendered message which by default does not contain those properties.

Michiel
  • 662
  • 5
  • 13
  • This makes perfect sense now. I guess I'm still mentally transitioning from the classic logging to this "object logging" concept. – CyberDude Jul 01 '14 at 15:11
  • The RollingFile sink is a quick way to output stuff, the real power is when you store it into Seq, ElasticSearch or some online log provider like Loggly. Then you can access all the metadata, do analytics, searches etc. – Michiel Jul 02 '14 at 15:40
  • Yes, we are using Loggly as well but it seems a bit slow and difficult to set-up so you can find out quickly what you're looking for. – CyberDude Jul 02 '14 at 19:12
  • Try SEQ https://getseq.net/ which will receive all properties and has nice search and alert functions. It's from the same authors of Serilog. – Calvin Mar 18 '15 at 21:03
  • Machine Name is client machine name or host server machine name? – Sras Feb 22 '21 at 07:23
0

If you are not seeing properties added by enriches like so

.Enrich.WithProperty("Assembly", assemblyName.Name)

The issue could be with outputTemplated as @Michel suggested. You need to add a outputTemplate for this.

Another way to add output template is to add formatter. First install Serilog.Formatting.Compact. Then you have to specify a formatter. I used the formatter in the appsettings as follows.

"Serilog": {
  "WriteTo": [
    {
      "Name": "Seq",
      "Args": {
        "serverUrl": "http://localhost:5341",
        "formatter": "Serilog.Formatting.Json.JsonFormatter, Serilog"
      }
    }
  ]
},

The JsonFormatter you see is a formatter available our form the nuget. Now all of my enrichers are working WITHOUT any outputTemplate anywhere.

For more details see the following page. https://github.com/serilog/serilog/wiki/Formatting-Output

VivekDev
  • 20,868
  • 27
  • 132
  • 202