From 6df7cae82db3a10ba52441196417a8ff28dccbb3 Mon Sep 17 00:00:00 2001 From: Eamon Hetherton Date: Fri, 17 Apr 2020 13:19:09 +1000 Subject: [PATCH 1/3] Added way to chain FileLifeCycleHooks together --- .../File/FileLifecycleHooksExtensions.cs | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 src/Serilog.Sinks.File/Sinks/File/FileLifecycleHooksExtensions.cs diff --git a/src/Serilog.Sinks.File/Sinks/File/FileLifecycleHooksExtensions.cs b/src/Serilog.Sinks.File/Sinks/File/FileLifecycleHooksExtensions.cs new file mode 100644 index 0000000..acc5513 --- /dev/null +++ b/src/Serilog.Sinks.File/Sinks/File/FileLifecycleHooksExtensions.cs @@ -0,0 +1,71 @@ +// 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[] hooks; + + public FileLifeCycleHookChain(params FileLifecycleHooks[] hooks) + { + this.hooks = hooks ?? throw new ArgumentNullException(nameof(hooks)); + } + + public override Stream OnFileOpened(Stream underlyingStream, Encoding encoding) + { + for (int i = 0; i < hooks.Length; i++) + { + underlyingStream = hooks[i].OnFileOpened(underlyingStream, encoding); + } + return underlyingStream; + } + + public override void OnFileDeleting(string path) + { + for (int i = 0; i < hooks.Length; i++) + { + hooks[i].OnFileDeleting(path); + } + } + } + } +} From 1af50969e0dafe596b41d4f8c514ca32acbbce02 Mon Sep 17 00:00:00 2001 From: Eamon Hetherton Date: Fri, 17 Apr 2020 13:21:09 +1000 Subject: [PATCH 2/3] stop using params when current implementation only ever has two values --- .../File/FileLifecycleHooksExtensions.cs | 23 +++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/Serilog.Sinks.File/Sinks/File/FileLifecycleHooksExtensions.cs b/src/Serilog.Sinks.File/Sinks/File/FileLifecycleHooksExtensions.cs index acc5513..f79c768 100644 --- a/src/Serilog.Sinks.File/Sinks/File/FileLifecycleHooksExtensions.cs +++ b/src/Serilog.Sinks.File/Sinks/File/FileLifecycleHooksExtensions.cs @@ -43,28 +43,27 @@ public static FileLifecycleHooks ChainTo(this FileLifecycleHooks first, FileLife class FileLifeCycleHookChain : FileLifecycleHooks { - private readonly FileLifecycleHooks[] hooks; + private readonly FileLifecycleHooks _first; + private readonly FileLifecycleHooks _second; - public FileLifeCycleHookChain(params FileLifecycleHooks[] hooks) + public FileLifeCycleHookChain(FileLifecycleHooks first, FileLifecycleHooks second) { - this.hooks = hooks ?? throw new ArgumentNullException(nameof(hooks)); + _first = first ?? throw new ArgumentNullException(nameof(first)); + _second = second ?? throw new ArgumentNullException(nameof(second)); } public override Stream OnFileOpened(Stream underlyingStream, Encoding encoding) { - for (int i = 0; i < hooks.Length; i++) - { - underlyingStream = hooks[i].OnFileOpened(underlyingStream, encoding); - } - return underlyingStream; + var firstStreamResult = _first.OnFileOpened(underlyingStream, encoding); + var secondStreamResult = _second.OnFileOpened(firstStreamResult, encoding); + + return secondStreamResult; } public override void OnFileDeleting(string path) { - for (int i = 0; i < hooks.Length; i++) - { - hooks[i].OnFileDeleting(path); - } + _first.OnFileDeleting(path); + _second.OnFileDeleting(path); } } } From cad86bd4ae68ecdb6a476f6a53e8ca1d3bd1cc95 Mon Sep 17 00:00:00 2001 From: Eamon Hetherton Date: Fri, 17 Apr 2020 16:21:49 +1000 Subject: [PATCH 3/3] refactor "ChainTo" to instance method "Then" --- .../Sinks/File/FileLifeCycleHookChain.cs | 46 ++++++++++++ .../Sinks/File/FileLifecycleHooks.cs | 17 +++++ .../File/FileLifecycleHooksExtensions.cs | 70 ------------------- 3 files changed, 63 insertions(+), 70 deletions(-) create mode 100644 src/Serilog.Sinks.File/Sinks/File/FileLifeCycleHookChain.cs delete mode 100644 src/Serilog.Sinks.File/Sinks/File/FileLifecycleHooksExtensions.cs 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); - } - } - } -}