Answered by:
How to log json inside json at correct format

Question
-
In log4net.config file i am having the following appender.
I was using JsonLogging appender for logging using log4net. If i try to log json string then json is logged like string not as an json. How can i handle this scenario ? The message inside message attribute contains json as string. I want to log that as json.
<appender name="JsonLogging" type="log4net.Appender.RollingFileAppender"> <file value="E:\\april.log" /> <appendToFile value="true" /> <staticLogFileName value="true" /> <rollingStyle value="Date" /> <datePattern value="'_'yyyy_MM_dd"/> <PreserveLogFileNameExtension value="true" /> <lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> <layout type="log4net.Layout.SerializedLayout, log4net.Ext.Json"> <decorator type="log4net.Layout.Decorators.StandardTypesDecorator, log4net.Ext.Json" /> <default /> <remove value="message" /> <remove value="ndc"/> <member value="message:messageobject"/> </layout> </appender>
{ "date":"2019-04-02T13:17:02.6719961+05:30", "level":"INFO", "appname":"Test.exe", "logger":"Test.Program", "thread":"1", "message":{ "message":"{\"Consumer\":{\"os\":\"Microsoft Windows\",\"Test\":\"test\",\"name\":null,\"machine\":\"testmachine\"}}", "customer":"Customername" } }
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); log.Info(message(jsonstring)); public static object message(object msg) { return new { message = msg, customer = CustomerName }; }
Tuesday, April 2, 2019 8:05 AM
Answers
-
Hi AishwaryaKasi,
Thank you for feedback.
The data you provided is correct format of json.
How do you define the log4net layout? Like the link below?
https://github.com/Litee/log4net.Layout.Json
I do not test. But if you do not want the double quote, change the type.
Best Regards,
Wendy
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.- Proposed as answer by Wendy ZangMicrosoft contingent staff Wednesday, April 3, 2019 7:33 AM
- Marked as answer by AishwaryaKasi Thursday, April 4, 2019 1:14 PM
Tuesday, April 2, 2019 9:36 AM
All replies
-
Hi AishwaryaKasi,
Thank you for posting here.
For your question, you could log4net.Ext.Json to create json log.
You could download from the NuGet.
https://www.nuget.org/packages/log4net.Ext.Json
For more details, you could follow the steps in the StackOverFlow below.
https://stackoverflow.com/questions/41924684/can-log4net-output-json
Best Regards,
Wendy
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.Tuesday, April 2, 2019 9:08 AM -
Hi Wendy Zang ,
Thanks for the reply. Actually i was using log4net.Layout.SerializedLayout, log4net.Ext.Json to log in json format. Actually the issue was message inside the message attribute in the example i was posted in that ,
"message":"{\"Consumer\" contains double quotes around json value this was the issue i am facing . How can i log json as json not as an string .
- Edited by AishwaryaKasi Tuesday, April 2, 2019 9:22 AM
Tuesday, April 2, 2019 9:22 AM -
Hi AishwaryaKasi,
Thank you for feedback.
The data you provided is correct format of json.
How do you define the log4net layout? Like the link below?
https://github.com/Litee/log4net.Layout.Json
I do not test. But if you do not want the double quote, change the type.
Best Regards,
Wendy
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.- Proposed as answer by Wendy ZangMicrosoft contingent staff Wednesday, April 3, 2019 7:33 AM
- Marked as answer by AishwaryaKasi Thursday, April 4, 2019 1:14 PM
Tuesday, April 2, 2019 9:36 AM