public RollingFileAlternativeSink(ITextFormatter formatter, TemplatedPathRoller roller, long fileSizeLimitBytes, RollingLogFile rollingLogFile, Encoding encoding = null) { _formatter = formatter; _roller = roller; _fileSizeLimitBytes = fileSizeLimitBytes; EnableLevelLogging = roller.PathIncludesLevel; _output = OpenFileForWriting(roller.LogFileDirectory, rollingLogFile, encoding ?? Encoding.UTF8); }
private StreamWriter OpenFileForWriting(string folderPath, RollingLogFile rollingLogFile, Encoding encoding) { EnsureDirectoryCreated(folderPath); try { LogFile = 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; } }