Skip to content

Commit

Permalink
Merge pull request #141 from EamonHetherton/dev
Browse files Browse the repository at this point in the history
Add extension method to chain FileLifecycleHooks together
  • Loading branch information
nblumhardt authored Apr 20, 2020
2 parents 74b09e8 + cad86bd commit 7106445
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/Serilog.Sinks.File/Sinks/File/FileLifeCycleHookChain.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
17 changes: 17 additions & 0 deletions src/Serilog.Sinks.File/Sinks/File/FileLifecycleHooks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,22 @@ public abstract class FileLifecycleHooks
/// </summary>
/// <param name="path">The full path to the file being deleted.</param>
public virtual void OnFileDeleting(string path) {}

/// <summary>
/// Creates a chain of <see cref="FileLifecycleHooks"/> that have their methods called sequentially
/// Can be used to compose <see cref="FileLifecycleHooks"/> together; e.g. add header information to each log file and
/// compress it.
/// </summary>
/// <example>
/// <code>
/// var hooks = new GZipHooks().Then(new HeaderWriter("File Header"));
/// </code>
/// </example>
/// <param name="next">The next <see cref="FileLifecycleHooks"/> to have its methods called in the chain</param>
/// <returns></returns>
public FileLifecycleHooks Then(FileLifecycleHooks next)
{
return new FileLifeCycleHookChain(this, next);
}
}
}

0 comments on commit 7106445

Please sign in to comment.