-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #107 from nogard111/OnFileRemovingHook
Add OnFileRemoving life cycle hook #106
- Loading branch information
Showing
4 changed files
with
67 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
test/Serilog.Sinks.File.Tests/Support/ArchiveOldLogsHook.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System; | ||
using System.IO; | ||
using System.Text; | ||
|
||
namespace Serilog.Sinks.File.Tests.Support | ||
{ | ||
internal class ArchiveOldLogsHook : FileLifecycleHooks | ||
{ | ||
private readonly string _relativeArchiveDir; | ||
|
||
public ArchiveOldLogsHook(string relativeArchiveDir) | ||
{ | ||
_relativeArchiveDir = relativeArchiveDir; | ||
} | ||
|
||
public override void OnFileDeleting(string path) | ||
{ | ||
base.OnFileDeleting(path); | ||
var newFile = AddTopDirectory(path, _relativeArchiveDir, true); | ||
System.IO.File.Copy(path, newFile, false); | ||
} | ||
|
||
public static string AddTopDirectory(string path, string directoryToAdd, bool createOnNonExist = false) | ||
{ | ||
string file = Path.GetFileName(path); | ||
string directory = Path.Combine(Path.GetDirectoryName(path) ?? throw new InvalidOperationException(), directoryToAdd); | ||
|
||
if (createOnNonExist && !Directory.Exists(directory)) | ||
{ | ||
Directory.CreateDirectory(directory); | ||
} | ||
return Path.Combine(directory, file); | ||
} | ||
} | ||
} |