Search code examples
c#loggingnlogmilliseconds

NLog avoid repeated lines and set delta time


I am using Nlog and couldn't find the features here below. Of course I could make them by myself but since NLog usually takes care of all needs perhaps might have all this built-in.


Delta seconds between each entry

I have the layout as "[${date:format=dd.MM.yyyy HH\:mm\:ss.fff}] (${level:uppercase=true}): ${message}" So that I get something like:

[20.05.2021 11:53:33.667] (INFO): --- 20210520_1153 Starting ---
[20.05.2021 11:53:33.784] (INFO): *.cfg not found going for TRIAL LICENCE
[20.05.2021 11:53:33.784] (INFO): Reg Hive found. Verifying
[20.05.2021 11:53:33.784] (INFO): Key length = 3

Is it possible to automaticalla add delta time? so like here below

[20.05.2021 11:53:33.667 - 0.000] (INFO): --- 20210520_1153 Starting ---
[20.05.2021 11:53:33.784 - 0.117] (INFO): *.cfg not found going for TRIAL LICENCE
[20.05.2021 11:53:33.784 - 0.000] (INFO): Reg Hive found. Verifying
[20.05.2021 11:53:33.784 - 0.000] (INFO): Key length = 3

Avoid repeated lines (at will)

[20.05.2021 11:53:33.667] (INFO): AAA
[20.05.2021 11:53:33.784] (INFO): AAA
[20.05.2021 11:53:33.784] (INFO): AAA
[20.05.2021 11:53:33.784] (INFO): AAA

to become something like

[20.05.2021 11:53:33.667] (INFO): AAA
...

And to be able to extend that also to more than one level

[20.05.2021 11:53:33.667] (INFO): AAA
[20.05.2021 11:53:33.784] (INFO): BBB
[20.05.2021 11:53:33.784] (INFO): AAA
[20.05.2021 11:53:33.784] (INFO): BBB

to become

[20.05.2021 11:53:33.667] (INFO): AAA
[20.05.2021 11:53:33.784] (INFO): BBB
...

That would save a lot of chars

Thanks in advance

Patrick


Solution

  • You can use ${processtime} to see time since program was started. See also: https://github.com/NLog/NLog/wiki/Processtime-Layout-Renderer (Useful for seeing deltas).

    You can use WhenRepeated-filter to block duplicate logevents. But it is better to avoid doing repeated logging, when possible (And ensure logging-rules have been properly configured). See also: https://github.com/NLog/NLog/wiki/WhenRepeated-Filter