11

In the Serilog output in a file, I see that the default is

{
    "Timestamp": "2016-05-28T21:21:59.0932348+08:00",
    "Level": "Information",
    "MessageTemplate": "Processed {@Number} records in {@Time} ms",
    "Properties": {
        "Number": 500,
        "Time": 120
    }
}

Is there a way to remove the Timestamp, level, messagetemplate, and properties so that I"m left with this only

{
    "Number": 500,
    "Time": 120
}

The Log.Logger is assigned as such

Log.Logger = new LoggerConfiguration()
    .WriteTo.Sink(new FileSink(ConfigurationManager.AppSettings["serilogPath"], new JsonFormatter(), null))
    .CreateLogger();

Thanks

Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
Bran
  • 617
  • 8
  • 21

2 Answers2

6

From looking at the source code, it doesn't look like the JsonFormatter supports skipping those default properties. You could create your own ITextFormatter that does what you're looking for. Here's a quick example (that should not be used in production because it doesn't do any escaping -- it's just for demo purposes):

public class SOFormatter : ITextFormatter
{
    public void Format(LogEvent logEvent, TextWriter output)
    {
        output.Write("{");
        foreach (var p in logEvent.Properties)
        {
            output.Write("\"{0}\" : {1}, ", p.Key, p.Value);
        }
        output.Write("}");
    }
}
PatrickSteele
  • 14,489
  • 2
  • 51
  • 54
  • Perfect, just what I was looking for. Thanks – Bran May 30 '16 at 04:02
  • Hi, I'm using Serilog mongo pkg. Is it possible to remove the default property "MessageTemplate" from the mongo document generated? Thanks – pregoli Feb 13 '17 at 17:11
  • 1
    @pregoli Hey, this is a bit late, but you can definitely do that. You'll have to edit the original source code and remove that portion of the code that generates that key-value. – Bran Jun 30 '17 at 06:03
3

This question is old but now there is some easy solution for it, so I want to share them.

  1. Make sure you have this Serilog.Expressions Nuget Package (version 3.3 at least)

  2. You can now configure the ExpressionTemplate in code, here is an example:

    Log.Logger = new LoggerConfiguration()
        .WriteTo.Console(formatter: new ExpressionTemplate("{ {@t, @mt, @r, @l: if @l = 'Information' then undefined() else @l, @x, ..@p} }\n"))
        .CreateLogger();
    
  3. Or you can configure ExpressionTemplate in the appSettings.json, like this

    {
      "Name": "Console",
      "Args": {
        "formatter": {
          "type": "Serilog.Templates.ExpressionTemplate, Serilog.Expressions",
          "template": "[{@t:HH:mm:ss} {@l:u3} {Coalesce(SourceContext, '<none>')}] {@m}\n{@x}"
        }
      }
    }
    

Notes

  1. The formatter word in the appSettings is the name of an argument to a method, So it could be different depending on the sink you are using.
    For example, I used Mongodb Sink, so the name of the argument was mongoDBJsonFormatter for me

  2. I am not explaining the syntax of the ExpressionTemplate here, you can consult the following links for more info on that.

  3. here is a good article which explain this in more details

  4. here is Some documentation

Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131