Nlog | JsonLayout | includeAllProperties='true' => How can i change datetime format in object?
See original GitHub issueHi!
I have a little problem about DateTime format in my class?
NLog version: 4.6.7
Platform: .Net 4.6.1
Current NLog config
<target name="ijson" xsi:type="File" fileName="${basedir}/logs/Trace/i_${date:format=yyyyMMddHH}.log" >
<layout type="JsonLayout">
<attribute name="Time" layout="${date:format=O}" />
<attribute name="Level" layout="${level:upperCase=true}"/>
<attribute name="Properties" encode="false" >
<layout type='JsonLayout' includeAllProperties="true" excludeProperties="EventId_Id,EventId_Name,EventId" maxRecursionLimit="5"/>
</attribute>
</layout>
</target>
My Object Class
public class ActivityLogModel
{
public DateTime StartTime { get; set; }
public DateTime EndTime { get; set; }
public double ProcessTime { get; set; }
}
My Code
public static void WriteNLog(object obj)
{
Logger _logger = NLog.LogManager.GetCurrentClassLogger();
_logger.Info("{Object}", obj);
}
Output
{
"Time": "2019-08-29T11:42:33.9779998+07:00",
"Level": "INFO",
"Properties": {
"Object": {
"ActivityLog": {
"StartTime": "2019-08-29T04:41:15.9925922Z",
"EndTime": "2019-08-29T04:41:16.8197183Z",
"ProcessTime": 827.1261
}
}
}
}
Can i change DateTime format in object like <attribute name="Time" layout="${date:format=O}" />
?
Thanks you
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
NLog time formatting
3 Answers. According to the NLog documentation, you can use C# DateTime format string. Another alternative to the format suggested by harriyott ...
Read more >Config options for NLog's configuration
Date and time. ${date} - Current date and time. ${longdate} - The date and time in a long, sortable format `yyyy-MM-dd HH:mm:ss.ffff`.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Thanks for replying. Finally , My code is
And When i use
Thanks for this solution!!
If you override the IJsonConverter before loading NLog-config or creating NLog-Logger, then this should work:
You can also replace it completely using Json.Net (and inject your own datetime-format).
https://github.com/NLog/NLog/wiki/How-to-use-structured-logging#i-like-to-use-jsonnet-for-creating-json