event context date format

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

event context date format

codyzu
I am attempting to use LogEventInfo.Properties in my code to log custom data to the renderer. One of the objects I am adding is a DateTime object. In the event context layout renderer, I would like to be able to specify the date format string that the DateTime object should be rendered with. Is this possible?

To back up... my goal is to log several custom data fields and allow the user to choose what fields get rendered in the log (and how in the case of DateTime) with the NLog configuration. As best as I can tell, using the LogEventInfo is the easiest way to log custom data. How can I do this, and control the date format string in the NLog configuration?

Thank you.
Reply | Threaded
Open this post in threaded view
|

Re: event context date format

codyzu
OK, so my current solution is to implement my own EventContextLayoutRenderer. It is mostly identical to the original one but has an additional format property and checks if the LayoutEventInfo property value is a DateTime object. If the value is a DateTime object it formats the DateTime according the format property and appends that to the string builder. This was the only solution that I could come up with... please let me know if there is a more elegant solution. I would prefer not to have the extensions element in my NLog configuration (adds clutter), but that appears to be required with my solution.
Reply | Threaded
Open this post in threaded view
|

Re: event context date format

Jarek Kowalski
Administrator
For now implementing your own LR is probably the only solution.
I'll see if it can be generalized in future versions of NLog.

Jarek
NLog Blog