public SizeLimitedFileSink(ITextFormatter formatter, TemplatedPathRoller roller, long fileSizeLimitBytes,
     RollingLogFile rollingLogFile, Encoding encoding = null)
 {
     this.formatter = formatter;
     this.roller = roller;
     this.fileSizeLimitBytes = fileSizeLimitBytes;
     this.EnableLevelLogging = roller.PathIncludesLevel;
     this.output = OpenFileForWriting(roller.LogFileDirectory, rollingLogFile, encoding ?? Encoding.UTF8);
 }
        private StreamWriter OpenFileForWriting(string folderPath, RollingLogFile rollingLogFile, Encoding encoding)
        {
            EnsureDirectoryCreated(folderPath);
            try
            {
                currentLogFile = rollingLogFile;
                var fullPath = Path.Combine(folderPath, rollingLogFile.Filename);
                var stream = File.Open(fullPath, FileMode.Append, FileAccess.Write, FileShare.Read);

                return new StreamWriter(stream, encoding ?? Encoding.UTF8);
            }
            catch (IOException ex)
            {
                SelfLog.WriteLine("Error {0} while opening obsolete file {1}", ex, rollingLogFile.Filename);

                return OpenFileForWriting(folderPath, rollingLogFile.Next(roller), encoding);
            }
            catch (Exception ex)
            {
                SelfLog.WriteLine("Error {0} while opening obsolete file {1}", ex, rollingLogFile.Filename);
                throw;
            }
        }