My application uses nlog. It needs to send logs to a nxlog process running in the same host (which I also have control over).
I thought of sending json lines using TCP or UDP from nlog to nxlog. That's a straightforward design but it is not fault tolerant AFAIK. If nxlog goes down momentarily, nlog may even keep retrying but if my app exits at that moment, log messages are lost.
Alternatively I can set nlog file target to write json lines to. It will work as a persisted buffer. Nxlog would read from that file. The problem becomes how to setup file rolling settings. If I don't set rolling, nlog may truncate the file before nxlog can read the last log lines. If I set rolling, nxlog will see the new archived file as a new set of logs and "replay" them generating duplicates.
So, my question is: how do I setup nlog and nxlog to be fault tolerant and prevent duplicates?
I took b0ti's suggestion and came up with:
var file = new FileTarget
{
FileName = @"C:\Logs\file.log",
ArchiveFileName = @"C:\Logs\file.{#}.log",
ArchiveEvery = FileArchivePeriod.Minute,
ArchiveAboveSize = 2 * 1024 * 1024, // 2MB.
ArchiveNumbering = ArchiveNumberingMode.Sequence,
MaxArchiveFiles = 60,
};
Nxlog watches only the archived file: C:\Logs\file.{#}.log
NLog will add a new file for Nxlog every minute or whenever it becomes large enough (2MB in the example above). That's an acceptable delay in my case.
It keeps 1 hour of logs if my app is not logging much. If it is logging like crazy, history will be shorter but no grater than 122MB (2 + 2 * 60).