Skip to content

Commit cad86bd

Browse files
refactor "ChainTo" to instance method "Then"
1 parent 76af134 commit cad86bd

File tree

3 files changed

+63
-70
lines changed

3 files changed

+63
-70
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright 2019 Serilog Contributors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
using System;
16+
using System.IO;
17+
using System.Text;
18+
19+
namespace Serilog.Sinks.File
20+
{
21+
class FileLifeCycleHookChain : FileLifecycleHooks
22+
{
23+
private readonly FileLifecycleHooks _first;
24+
private readonly FileLifecycleHooks _second;
25+
26+
public FileLifeCycleHookChain(FileLifecycleHooks first, FileLifecycleHooks second)
27+
{
28+
_first = first ?? throw new ArgumentNullException(nameof(first));
29+
_second = second ?? throw new ArgumentNullException(nameof(second));
30+
}
31+
32+
public override Stream OnFileOpened(Stream underlyingStream, Encoding encoding)
33+
{
34+
var firstStreamResult = _first.OnFileOpened(underlyingStream, encoding);
35+
var secondStreamResult = _second.OnFileOpened(firstStreamResult, encoding);
36+
37+
return secondStreamResult;
38+
}
39+
40+
public override void OnFileDeleting(string path)
41+
{
42+
_first.OnFileDeleting(path);
43+
_second.OnFileDeleting(path);
44+
}
45+
}
46+
}

src/Serilog.Sinks.File/Sinks/File/FileLifecycleHooks.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,22 @@ public abstract class FileLifecycleHooks
4343
/// </summary>
4444
/// <param name="path">The full path to the file being deleted.</param>
4545
public virtual void OnFileDeleting(string path) {}
46+
47+
/// <summary>
48+
/// Creates a chain of <see cref="FileLifecycleHooks"/> that have their methods called sequentially
49+
/// Can be used to compose <see cref="FileLifecycleHooks"/> together; e.g. add header information to each log file and
50+
/// compress it.
51+
/// </summary>
52+
/// <example>
53+
/// <code>
54+
/// var hooks = new GZipHooks().Then(new HeaderWriter("File Header"));
55+
/// </code>
56+
/// </example>
57+
/// <param name="next">The next <see cref="FileLifecycleHooks"/> to have its methods called in the chain</param>
58+
/// <returns></returns>
59+
public FileLifecycleHooks Then(FileLifecycleHooks next)
60+
{
61+
return new FileLifeCycleHookChain(this, next);
62+
}
4663
}
4764
}

src/Serilog.Sinks.File/Sinks/File/FileLifecycleHooksExtensions.cs

Lines changed: 0 additions & 70 deletions
This file was deleted.

0 commit comments

Comments
 (0)