log json with serilog
Asked Answered
C

2

6

I would like to log any json into serilog properties. I have read a lot about serilog and json, but it is a structured json logging, with a message template. So instead of logging this Log.Info("{@text}",text); I would like to do this:

var json = "{ \"text\": \"hello\" }"; //a json string or a Json object
Log.Info(json);

The main difference is that with the first approach, you have a message template and always you have a "text" property. I would like to have json with different structures, for example if then I have:

var json = "{ \"text\": \"hello\", \"text2\": \"hello2\" }"; //a json string or a Json object
Log.Info(json);

I would like to obtain 2 properties, that is, 1 property for each json property, without having to define a message Template. Is this possible? do I have to implement my own JsonFormatter?

Claire answered 3/1, 2017 at 17:30 Comment(3)
It should be noted that JSON by definition is a string based interchange-format. There's not concept of JSON as a string versus an object...Arsenious
thanks for your answer. I have a log repository and I would like to search by property, so with a message template i would have all my properties defined in advance. That is the main reason I am asking this.Claire
Do you have the JSON as a JSON.NET JObject? If so, github.com/destructurama/json-net should be what you're after.Measly
C
0

I have used a in log4net to send to Seq. So, when emiting the message, I have added into the threadContext a property for every JProperty in the Json, at the first level only. Now in Seq, I can search by any of the json property. This is an example (take care that message variable should be a Json string before calling this methods).

private void AddProperties(string message)
        {
            var parsedObject = JObject.Parse(message);
            foreach (JProperty prop in parsedObject.Properties())
            {
                AddProperty(prop);
            }


        }
        private void AddProperty(JProperty message)
        {
            log4net.ThreadContext.Properties[message.Name] = message.Value.ToString();
        }
Claire answered 3/1, 2017 at 19:44 Comment(0)
H
3

You have to configure your logger to use Json.JsonFormatter and just pass the object when logging info, Serilog will serialize the object.

Configuration

Log.Logger = new LoggerConfiguration()
            .WriteTo.RollingFile(new JsonFormatter(), Path.Combine(@"c:\log", "[filename]-{Date}.txt"))
            .CreateLogger()
Hinny answered 3/2, 2017 at 11:16 Comment(0)
C
0

I have used a in log4net to send to Seq. So, when emiting the message, I have added into the threadContext a property for every JProperty in the Json, at the first level only. Now in Seq, I can search by any of the json property. This is an example (take care that message variable should be a Json string before calling this methods).

private void AddProperties(string message)
        {
            var parsedObject = JObject.Parse(message);
            foreach (JProperty prop in parsedObject.Properties())
            {
                AddProperty(prop);
            }


        }
        private void AddProperty(JProperty message)
        {
            log4net.ThreadContext.Properties[message.Name] = message.Value.ToString();
        }
Claire answered 3/1, 2017 at 19:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.