Exemple #1
0
        private static TextWriter TryOpenFileOnce(FilePath file, FileLogSettings settings)
        {
            try
            {
                var directory = Path.GetDirectoryName(file.NormalizedPath);
                if (directory != null && !Directory.Exists(directory))
                {
                    Directory.CreateDirectory(directory);
                }

                var fileMode = settings.FileOpenMode == FileOpenMode.Append ? FileMode.Append : FileMode.Create;
                var stream   = new FileStream(file.NormalizedPath, fileMode, FileAccess.Write, settings.FileShare, 1);

                return(new StreamWriter(stream, settings.Encoding, settings.OutputBufferSize, false));
            }
            catch (Exception error)
            {
                SafeConsole.ReportError($"Failed to open log file '{file}':", error);
                return(null);
            }
        }
Exemple #2
0
        public TextWriter OpenFile(FilePath file, FileOpenMode fileOpenMode, Encoding encoding, int bufferSize)
        {
            try
            {
                var directory = Path.GetDirectoryName(file.NormalizedPath);
                if (directory != null && !Directory.Exists(directory))
                {
                    Directory.CreateDirectory(directory);
                }

                var fileMode = fileOpenMode == FileOpenMode.Append ? FileMode.Append : FileMode.Create;
                var stream   = new FileStream(file.NormalizedPath, fileMode, FileAccess.Write, FileShare.ReadWrite, 1);

                return(new StreamWriter(stream, encoding, bufferSize, false));
            }
            catch (Exception error)
            {
                SafeConsole.ReportError($"Failed to open log file '{file}':", error);
                return(null);
            }
        }