Exemple #1
0
        public RollingFileSink(string path,
                               ITextFormatter textFormatter,
                               long?fileSizeLimitBytes,
                               int?retainedFileCountLimit,
                               Encoding encoding,
                               bool buffered,
                               bool shared,
                               RollingInterval rollingInterval,
                               bool rollOnFileSizeLimit,
                               FileLifecycleHooks hooks)
        {
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }
            if (fileSizeLimitBytes.HasValue && fileSizeLimitBytes < 0)
            {
                throw new ArgumentException("Negative value provided; file size limit must be non-negative.");
            }
            if (retainedFileCountLimit.HasValue && retainedFileCountLimit < 1)
            {
                throw new ArgumentException("Zero or negative value provided; retained file count limit must be at least 1.");
            }

            _roller                 = new PathRoller(path, rollingInterval);
            _textFormatter          = textFormatter;
            _fileSizeLimitBytes     = fileSizeLimitBytes;
            _retainedFileCountLimit = retainedFileCountLimit;
            _encoding               = encoding;
            _buffered               = buffered;
            _shared                 = shared;
            _rollOnFileSizeLimit    = rollOnFileSizeLimit;
            _hooks = hooks;
        }
Exemple #2
0
        // This overload should be used internally; the overload above maintains compatibility with the earlier public API.
        internal FileSink(
            string path,
            ITextFormatter textFormatter,
            long?fileSizeLimitBytes,
            Encoding encoding,
            bool buffered,
            FileLifecycleHooks hooks)
        {
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }
            if (fileSizeLimitBytes.HasValue && fileSizeLimitBytes < 0)
            {
                throw new ArgumentException("Negative value provided; file size limit must be non-negative.");
            }
            _textFormatter      = textFormatter ?? throw new ArgumentNullException(nameof(textFormatter));
            _fileSizeLimitBytes = fileSizeLimitBytes;
            _buffered           = buffered;

            var directory = Path.GetDirectoryName(path);

            if (!string.IsNullOrWhiteSpace(directory) && !Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }

            Stream outputStream = _underlyingStream = System.IO.File.Open(path, FileMode.Append, FileAccess.Write, FileShare.Read);

            if (_fileSizeLimitBytes != null)
            {
                outputStream = _countingStreamWrapper = new WriteCountingStream(_underlyingStream);
            }

            // Parameter reassignment.
            encoding = encoding ?? new UTF8Encoding(encoderShouldEmitUTF8Identifier: false);

            if (hooks != null)
            {
                outputStream = hooks.OnFileOpened(outputStream, encoding) ??
                               throw new InvalidOperationException($"The file lifecycle hook `{nameof(FileLifecycleHooks.OnFileOpened)}(...)` returned `null`.");
            }

            _output = new StreamWriter(outputStream, encoding);
        }