NLog.Targets.FileTarget.ReplaceFileContent C# (CSharp) Method

ReplaceFileContent() private method

Creates the file specified in fileName and writes the file content in each entirety i.e. Header, Content and Footer.
This method is used when the content of the log file is re-written on every write.
private ReplaceFileContent ( string fileName, byte bytes, bool firstAttempt ) : void
fileName string The name of the file to be written.
bytes byte Sequence of to be written in the content section of the file.
firstAttempt bool First attempt to write?
return void
        private void ReplaceFileContent(string fileName, byte[] bytes, bool firstAttempt)
        {
            try
            {
                using (FileStream fs = File.Create(fileName))
                {
                    byte[] headerBytes = this.GetHeaderBytes();
                    if (headerBytes != null)
                    {
                        fs.Write(headerBytes, 0, headerBytes.Length);
                    }

                    fs.Write(bytes, 0, bytes.Length);

                    byte[] footerBytes = this.GetFooterBytes();
                    if (footerBytes != null)
                    {
                        fs.Write(footerBytes, 0, footerBytes.Length);
                    }
                }
            }
            catch (DirectoryNotFoundException)
            {
                if (!this.CreateDirs || !firstAttempt)
                {
                    throw;
                }
                Directory.CreateDirectory(Path.GetDirectoryName(fileName));
                //retry.
                ReplaceFileContent(fileName, bytes, false);
            }
        }