private StreamWriter OpenFileForWriting( string folderPath, HourlyLogFileDescription logFileDescription, Encoding encoding) { EnsureDirectoryCreated(folderPath); try { var fullPath = Path.Combine(folderPath, logFileDescription.FileName); var stream = File.Open(fullPath, FileMode.Append, FileAccess.Write, FileShare.Read); return(new StreamWriter(stream, encoding ?? Encoding.UTF8)); } catch (IOException ex) { // Unfortuantely the exception doesn't have a code to check so need to check the message instead if (!ex.Message.StartsWith("The process cannot access the file")) { throw; } } catch (UnauthorizedAccessException) { if (exceptionAlreadyThrown) { throw; } exceptionAlreadyThrown = true; } return(OpenFileForWriting(folderPath, logFileDescription.Next(), encoding)); }
private HourlyFileSink NextFileSink(DateTime dateTimeUtc) { var next = new HourlyLogFileDescription(dateTimeUtc); this.currentSink.Dispose(); return(new HourlyFileSink(this.formatter, this.logDirectory, next, this.encoding)); }
private HourlyFileSink NextFileSink(DateTime dateTimeUtc) { HourlyLogFileDescription next = this.currentSink.LogFileDescription.Next(); ApplyRetentionPolicy(); this.currentSink.Dispose(); return(new HourlyFileSink(this.formatter, this.logDirectory, next, this.encoding)); }
private StreamWriter OpenFileForWriting( string folderPath, HourlyLogFileDescription logFileDescription, Encoding encoding) { EnsureDirectoryCreated(folderPath); var fullPath = Path.Combine(folderPath, logFileDescription.FileName); var stream = File.Open(fullPath, FileMode.Append, FileAccess.Write, FileShare.Read); return(new StreamWriter(stream, encoding ?? Encoding.UTF8)); }
internal HourlyFileSink( ITextFormatter formatter, string logRootDirectory, HourlyLogFileDescription hourlyLogFileDescription, Encoding encoding = null) { this.formatter = formatter; this.hourlyLogFileDescription = hourlyLogFileDescription; string logDir = Path.Combine(logRootDirectory, hourlyLogFileDescription.Date.ToString("yyyy-MM-dd")); this.output = this.OpenFileForWriting(logDir, hourlyLogFileDescription, encoding ?? Encoding.UTF8); }