1

I have a format that I need to follow for logging. I am writing out to the FileSink with a

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

This generates a per-line JSON object looking like (I formatted it, it is actually one line in the log file):

{
    "Timestamp": "2016-05-29T02:49:33.2153863+08:00",
    "Level": "Error",
    "MessageTemplate": "my template",
    "Properties": {
        <MyPropertiesHere>
    }
}

I have asked from another question that I need to specify my own json formater to remove the timestamp/level/messagetemplate, which I can do, but ultimately, what I need is something like a combination of the output of a File and a FileSink with JSON (I formatted this as well. It should be all in one line):

2016-06-03 11:19:33.343 +08:00 [Information] {
    "MyCustomKeyName": {
        "key1": "val1",
        "key2": "val2"
    }
}

With my current configuration, the JSON object is showing the correct serialized form of the object, but I need the timestamp, [Information] field (could be error/warning as well), and the ability to rename the "Properties" key to "MyCustomKeyName".

The timestamp and the [Information] information is what I get by default when using a WriteTo.File, but my goal is to obtain that from using a FileSink with json formatter. With the FileSink, I am getting one whole JSON object with the fields encapsulated inside as key/val pairs. I want to have the timestamp and level information preceding the JSON object. The goal is to have logstash parse it.

Thank you.

Bran
  • 617
  • 8
  • 21

1 Answers1

1

Implementing your own Serilog.Formatting.ITextFormatter is the way to go here; it should be fairly straightforward to reuse the Serilog JsonFormatter either directly or by copying the code, for the JSON part.

When you have implemented the formatter you can pass it through WriteTo.File() in place of the built-in JsonFormatter.

Nicholas Blumhardt
  • 30,271
  • 4
  • 90
  • 101
  • I'm a total scrub when it comes to C#. I just realized from your comment I could copy and paste your JsonFormatter and remove those entries, and add the text behind the JSON string to be written! Thanks a bunch, I really like how you support the package so well! – Bran Jun 03 '16 at 10:16
  • Great, glad to hear :-) – Nicholas Blumhardt Jun 03 '16 at 10:20