I would like to change nlog layout so it displays time between two entries. Is such layout possible?
This is my output now, and it would be great if next to the time there is number of seconds elapsed since entry before.
2012-05-20 19:18:41.0924|INFO|HashMatcher.LocalMatcherControl|99 archives discovered
2012-05-20 19:18:56.5267|INFO|HashMatcher.LocalMatcherControl|memory usage BEFORE loading samples 283.06 Mb
2012-05-20 19:18:56.5267|INFO|HashMatcher.LocalMatcherControl|performing sample scan
2012-05-20 19:19:08.9131|INFO|HashMatcher.LocalMatcherControl|memory usage BEFORE creating progress 285.43 Mb
2012-05-20 19:20:16.5804|INFO|HashMatcher.LocalMatcherControl|skipped progress 0
2012-05-20 19:20:17.6100|INFO|HashMatcher.LocalMatcherControl|memory usage BEFORE sorting progress 205.06 Mb
I don’t think that there is anything built into NLog that provides this capability “free”, but you should be able to implement it yourself pretty easily using a custom LayoutRenderer. You can find many examples here (in NLog’s source repository).
You might end up with something like this (neither compiled nor tested by me):
In essence the layout renderer remembers the time stamp of the last message logged. The value of this layout renderer is the difference between the last message logged and the time stamp of the current message. This layout renderer could probably use some enhancements. If you look at the DateTime you can see that it also has properties for CultureInfo and Format. Depending on your needs, you might even want to provide the option to give the elapsed time since application startup (or at least since the first message logged).
Also, since a member variable is being modified (lastTimeStamp), it probably should be protected with a lock statement.
Hopefully this will help you out in getting the capability that you need.
Good luck!