diff --git a/src/Serilog.Sinks.File/Sinks/File/FileLifeCycleHookChain.cs b/src/Serilog.Sinks.File/Sinks/File/FileLifeCycleHookChain.cs new file mode 100644 index 0000000..cea5095 --- /dev/null +++ b/src/Serilog.Sinks.File/Sinks/File/FileLifeCycleHookChain.cs @@ -0,0 +1,46 @@ +// Copyright 2019 Serilog Contributors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using System; +using System.IO; +using System.Text; + +namespace Serilog.Sinks.File +{ + class FileLifeCycleHookChain : FileLifecycleHooks + { + private readonly FileLifecycleHooks _first; + private readonly FileLifecycleHooks _second; + + public FileLifeCycleHookChain(FileLifecycleHooks first, FileLifecycleHooks second) + { + _first = first ?? throw new ArgumentNullException(nameof(first)); + _second = second ?? throw new ArgumentNullException(nameof(second)); + } + + public override Stream OnFileOpened(Stream underlyingStream, Encoding encoding) + { + var firstStreamResult = _first.OnFileOpened(underlyingStream, encoding); + var secondStreamResult = _second.OnFileOpened(firstStreamResult, encoding); + + return secondStreamResult; + } + + public override void OnFileDeleting(string path) + { + _first.OnFileDeleting(path); + _second.OnFileDeleting(path); + } + } +} diff --git a/src/Serilog.Sinks.File/Sinks/File/FileLifecycleHooks.cs b/src/Serilog.Sinks.File/Sinks/File/FileLifecycleHooks.cs index fbaf133..e804cad 100644 --- a/src/Serilog.Sinks.File/Sinks/File/FileLifecycleHooks.cs +++ b/src/Serilog.Sinks.File/Sinks/File/FileLifecycleHooks.cs @@ -43,5 +43,22 @@ public abstract class FileLifecycleHooks /// /// The full path to the file being deleted. public virtual void OnFileDeleting(string path) {} + + /// + /// Creates a chain of that have their methods called sequentially + /// Can be used to compose together; e.g. add header information to each log file and + /// compress it. + /// + /// + /// + /// var hooks = new GZipHooks().Then(new HeaderWriter("File Header")); + /// + /// + /// The next to have its methods called in the chain + /// + public FileLifecycleHooks Then(FileLifecycleHooks next) + { + return new FileLifeCycleHookChain(this, next); + } } } diff --git a/src/Serilog.Sinks.File/Sinks/File/FileLifecycleHooksExtensions.cs b/src/Serilog.Sinks.File/Sinks/File/FileLifecycleHooksExtensions.cs deleted file mode 100644 index f79c768..0000000 --- a/src/Serilog.Sinks.File/Sinks/File/FileLifecycleHooksExtensions.cs +++ /dev/null @@ -1,70 +0,0 @@ -// Copyright 2019 Serilog Contributors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -using System; -using System.IO; -using System.Text; - -namespace Serilog.Sinks.File -{ - /// - /// FileLifecycleHooks extension methods - /// - public static class FileLifecycleHooksExtensions - { - /// - /// Creates a chain of that have their methods called sequentially - /// Can be used to compose together; e.g. add header information to each log file and - /// compress it. - /// - /// - /// - /// var hooks = new GZipHooks().ChainTo(new HeaderWriter("File Header")); - /// - /// - /// The first to have its methods called in the chain - /// The second to have its methods called in the chain - /// - public static FileLifecycleHooks ChainTo(this FileLifecycleHooks first, FileLifecycleHooks second) - { - return new FileLifeCycleHookChain(first, second); - } - - class FileLifeCycleHookChain : FileLifecycleHooks - { - private readonly FileLifecycleHooks _first; - private readonly FileLifecycleHooks _second; - - public FileLifeCycleHookChain(FileLifecycleHooks first, FileLifecycleHooks second) - { - _first = first ?? throw new ArgumentNullException(nameof(first)); - _second = second ?? throw new ArgumentNullException(nameof(second)); - } - - public override Stream OnFileOpened(Stream underlyingStream, Encoding encoding) - { - var firstStreamResult = _first.OnFileOpened(underlyingStream, encoding); - var secondStreamResult = _second.OnFileOpened(firstStreamResult, encoding); - - return secondStreamResult; - } - - public override void OnFileDeleting(string path) - { - _first.OnFileDeleting(path); - _second.OnFileDeleting(path); - } - } - } -}