NLog dropping messages after adding KeepFileOpen="true"

0

Switching over from log4net to NLog, while testing noticed that Nlog would drop few messages when writing message every 10ms with KeepFileOpen="true" and running on a network filer, however with this setting turned off it seems to log everything fine.

On reading their documentation, it seems like they recommend setting this to "true" for better performance.

I have code example below, i am able to re-create the issue with just toggling the setting, unable to re-create when I try to write on my local machine (SSD) '''

    var fileTarget = new FileTarget()
     {
        Name = "DefaultLog",
        Layout = "${date:format=HH\\:mm\\:ss.ffff}(${level:uppercase=true})(${threadid})${message}",
        FileName = @"{network_share_location}${date:format=yyyyMMdd}.${processid}.DebugLog.txt", 
        KeepFileOpen =  true
     };

     var config = new LoggingConfiguration();
     config.AddRule(LogLevel.Debug, LogLevel.Fatal, fileTarget, "*");
     
     var factory = new LogFactory(config);
     _logger = factory.GetLogger("DefaultLogger");

'''

Edit: After suggestions from the comment, i enabled internallogging and i see following errors, is there a flag to make the file readonly for other users.

2020-09-16 10:01:02.1919 Warn Attempt #1 to open {FilerPath} failed. Sleeping for 1ms
2020-09-16 10:01:02.1919 Warn Attempt #2 to open {FilerPath} failed. Sleeping for 0ms

2020-09-16 10:01:00.9871 Error FileTarget(Name=): Failed write to file '{FilerPath}'. Exception: System.IO.FileLoadException: The process cannot access the file because it is being used by another process. (Exception from HRESULT: 0x80070020)
   at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
   at NLog.Internal.FileAppenders.BaseFileAppender.WindowsCreateFile(String fileName, Boolean allowFileSharedWriting, Int32 overrideBufferSize)
   at NLog.Internal.FileAppenders.BaseFileAppender.TryCreateFileStream(Boolean allowFileSharedWriting, Int32 overrideBufferSize)
   at NLog.Internal.FileAppenders.BaseFileAppender.CreateFileStream(Boolean allowFileSharedWriting, Int32 overrideBufferSize)
   at NLog.Internal.FileAppenders.RetryingMultiProcessFileAppender.Write(Byte[] bytes, Int32 offset, Int32 count)
   at NLog.Targets.FileTarget.WriteToFile(String fileName, ArraySegment`1 bytes, Boolean initializedNewFile)

c#
logging
nlog
asked on Stack Overflow Sep 16, 2020 by s2211 • edited Sep 16, 2020 by s2211

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0