Skip to content

Commit 7cf84eb

Browse files
authored
Merge pull request #189 from augustoproiete-forks/capture-log-file-path-in-hooks
Add ability to capture path of log file opened, via FileLifecycleHooks
2 parents 2f6f1b6 + 4c8c3d4 commit 7cf84eb

File tree

5 files changed

+56
-4
lines changed

5 files changed

+56
-4
lines changed

src/Serilog.Sinks.File/Sinks/File/FileLifeCycleHookChain.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ public FileLifeCycleHookChain(FileLifecycleHooks first, FileLifecycleHooks secon
2929
_second = second ?? throw new ArgumentNullException(nameof(second));
3030
}
3131

32-
public override Stream OnFileOpened(Stream underlyingStream, Encoding encoding)
32+
public override Stream OnFileOpened(string path, Stream underlyingStream, Encoding encoding)
3333
{
34-
var firstStreamResult = _first.OnFileOpened(underlyingStream, encoding);
35-
var secondStreamResult = _second.OnFileOpened(firstStreamResult, encoding);
34+
var firstStreamResult = _first.OnFileOpened(path, underlyingStream, encoding);
35+
var secondStreamResult = _second.OnFileOpened(path, firstStreamResult, encoding);
3636

3737
return secondStreamResult;
3838
}

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

+15
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,21 @@ namespace Serilog.Sinks.File
2323
/// </summary>
2424
public abstract class FileLifecycleHooks
2525
{
26+
/// <summary>
27+
/// Initialize or wrap the <paramref name="underlyingStream"/> opened on the log file. This can be used to write
28+
/// file headers, or wrap the stream in another that adds buffering, compression, encryption, etc. The underlying
29+
/// file may or may not be empty when this method is called.
30+
/// </summary>
31+
/// <remarks>
32+
/// A value must be returned from overrides of this method. Serilog will flush and/or dispose the returned value, but will not
33+
/// dispose the stream initially passed in unless it is itself returned.
34+
/// </remarks>
35+
/// <param name="path">The full path to the log file.</param>
36+
/// <param name="underlyingStream">The underlying <see cref="Stream"/> opened on the log file.</param>
37+
/// <param name="encoding">The encoding to use when reading/writing to the stream.</param>
38+
/// <returns>The <see cref="Stream"/> Serilog should use when writing events to the log file.</returns>
39+
public virtual Stream OnFileOpened(string path, Stream underlyingStream, Encoding encoding) => OnFileOpened(underlyingStream, encoding);
40+
2641
/// <summary>
2742
/// Initialize or wrap the <paramref name="underlyingStream"/> opened on the log file. This can be used to write
2843
/// file headers, or wrap the stream in another that adds buffering, compression, encryption, etc. The underlying

src/Serilog.Sinks.File/Sinks/File/FileSink.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ internal FileSink(
9191

9292
if (hooks != null)
9393
{
94-
outputStream = hooks.OnFileOpened(outputStream, encoding) ??
94+
outputStream = hooks.OnFileOpened(path, outputStream, encoding) ??
9595
throw new InvalidOperationException($"The file lifecycle hook `{nameof(FileLifecycleHooks.OnFileOpened)}(...)` returned `null`.");
9696
}
9797

test/Serilog.Sinks.File.Tests/FileSinkTests.cs

+17
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,23 @@ public static void OnOpenedLifecycleHookCanWriteFileHeader()
206206
}
207207
}
208208

209+
[Fact]
210+
public static void OnOpenedLifecycleHookCanCaptureFilePath()
211+
{
212+
using (var tmp = TempFolder.ForCaller())
213+
{
214+
var capturePath = new CaptureFilePathHook();
215+
216+
var path = tmp.AllocateFilename("txt");
217+
using (new FileSink(path, new JsonFormatter(), null, new UTF8Encoding(false), false, capturePath))
218+
{
219+
// Open and capture the log file path
220+
}
221+
222+
Assert.Equal(path, capturePath.Path);
223+
}
224+
}
225+
209226
static void WriteTwoEventsAndCheckOutputFileLength(long? maxBytes, Encoding encoding)
210227
{
211228
using (var tmp = TempFolder.ForCaller())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System.IO;
2+
using System.Text;
3+
4+
namespace Serilog.Sinks.File.Tests.Support
5+
{
6+
/// <inheritdoc />
7+
/// <summary>
8+
/// Demonstrates the use of <seealso cref="T:Serilog.FileLifecycleHooks" />, by capturing the log file path
9+
/// </summary>
10+
class CaptureFilePathHook : FileLifecycleHooks
11+
{
12+
public string Path { get; private set; }
13+
14+
public override Stream OnFileOpened(string path, Stream _, Encoding __)
15+
{
16+
Path = path;
17+
return base.OnFileOpened(path, _, __);
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)