properties;
#pragma warning disable Serilog004 // Constant MessageTemplate verifier
- if (!log.BindMessageTemplate(messageTemplate, propertyValues, out template, out properties))
+ if (!log.BindMessageTemplate(messageTemplate, propertyValues, out var template, out var properties))
#pragma warning restore Serilog004 // Constant MessageTemplate verifier
{
throw new XunitException("Template could not be bound.");
@@ -65,7 +64,7 @@ public static LogEvent LogEvent(DateTimeOffset? timestamp = null, LogEventLevel
public static LogEvent InformationEvent(DateTimeOffset? timestamp = null)
{
- return LogEvent(timestamp, LogEventLevel.Information);
+ return LogEvent(timestamp);
}
public static LogEvent DebugEvent(DateTimeOffset? timestamp = null)
diff --git a/test/Serilog.Sinks.File.Tests/Support/TempFolder.cs b/test/Serilog.Sinks.File.Tests/Support/TempFolder.cs
index 7ff90f8..29682e0 100644
--- a/test/Serilog.Sinks.File.Tests/Support/TempFolder.cs
+++ b/test/Serilog.Sinks.File.Tests/Support/TempFolder.cs
@@ -11,7 +11,7 @@ class TempFolder : IDisposable
readonly string _tempFolder;
- public TempFolder(string name = null)
+ public TempFolder(string? name = null)
{
_tempFolder = System.IO.Path.Combine(
Environment.GetEnvironmentVariable("TMP") ?? Environment.GetEnvironmentVariable("TMPDIR") ?? "/tmp",
@@ -37,7 +37,7 @@ public void Dispose()
}
}
- public static TempFolder ForCaller([CallerMemberName] string caller = null, [CallerFilePath] string sourceFileName = "")
+ public static TempFolder ForCaller([CallerMemberName] string? caller = null, [CallerFilePath] string sourceFileName = "")
{
if (caller == null) throw new ArgumentNullException(nameof(caller));
if (sourceFileName == null) throw new ArgumentNullException(nameof(sourceFileName));
@@ -47,7 +47,7 @@ public static TempFolder ForCaller([CallerMemberName] string caller = null, [Cal
return new TempFolder(folderName);
}
- public string AllocateFilename(string ext = null)
+ public string AllocateFilename(string? ext = null)
{
return System.IO.Path.Combine(Path, Guid.NewGuid().ToString("n") + "." + (ext ?? "tmp"));
}
diff --git a/test/Serilog.Sinks.File.Tests/Support/TruncateFileHook.cs b/test/Serilog.Sinks.File.Tests/Support/TruncateFileHook.cs
new file mode 100644
index 0000000..63f8497
--- /dev/null
+++ b/test/Serilog.Sinks.File.Tests/Support/TruncateFileHook.cs
@@ -0,0 +1,18 @@
+using System.IO;
+using System.Text;
+
+namespace Serilog.Sinks.File.Tests.Support
+{
+ ///
+ ///
+ /// Demonstrates the use of , by emptying the file before it's written to
+ ///
+ public class TruncateFileHook : FileLifecycleHooks
+ {
+ public override Stream OnFileOpened(Stream underlyingStream, Encoding encoding)
+ {
+ underlyingStream.SetLength(0);
+ return base.OnFileOpened(underlyingStream, encoding);
+ }
+ }
+}
diff --git a/test/Serilog.Sinks.File.Tests/TemplatedPathRollerTests.cs b/test/Serilog.Sinks.File.Tests/TemplatedPathRollerTests.cs
index 5e1b015..65a974c 100644
--- a/test/Serilog.Sinks.File.Tests/TemplatedPathRollerTests.cs
+++ b/test/Serilog.Sinks.File.Tests/TemplatedPathRollerTests.cs
@@ -12,8 +12,7 @@ public void TheLogFileIncludesDateToken()
{
var roller = new PathRoller(Path.Combine("Logs", "log-.txt"), RollingInterval.Day);
var now = new DateTime(2013, 7, 14, 3, 24, 9, 980);
- string path;
- roller.GetLogFilePath(now, null, out path);
+ roller.GetLogFilePath(now, null, out var path);
AssertEqualAbsolute(Path.Combine("Logs", "log-20130714.txt"), path);
}
@@ -22,8 +21,7 @@ public void ANonZeroIncrementIsIncludedAndPadded()
{
var roller = new PathRoller(Path.Combine("Logs", "log-.txt"), RollingInterval.Day);
var now = new DateTime(2013, 7, 14, 3, 24, 9, 980);
- string path;
- roller.GetLogFilePath(now, 12, out path);
+ roller.GetLogFilePath(now, 12, out var path);
AssertEqualAbsolute(Path.Combine("Logs", "log-20130714_012.txt"), path);
}
@@ -46,8 +44,7 @@ public void TheLogFileIsNotRequiredToIncludeAnExtension()
{
var roller = new PathRoller(Path.Combine("Logs", "log-"), RollingInterval.Day);
var now = new DateTime(2013, 7, 14, 3, 24, 9, 980);
- string path;
- roller.GetLogFilePath(now, null, out path);
+ roller.GetLogFilePath(now, null, out var path);
AssertEqualAbsolute(Path.Combine("Logs", "log-20130714"), path);
}
@@ -56,19 +53,18 @@ public void TheLogFileIsNotRequiredToIncludeADirectory()
{
var roller = new PathRoller("log-", RollingInterval.Day);
var now = new DateTime(2013, 7, 14, 3, 24, 9, 980);
- string path;
- roller.GetLogFilePath(now, null, out path);
+ roller.GetLogFilePath(now, null, out var path);
AssertEqualAbsolute("log-20130714", path);
}
[Fact]
- public void MatchingExcludesSimilarButNonmatchingFiles()
+ public void MatchingExcludesSimilarButNonMatchingFiles()
{
var roller = new PathRoller("log-.txt", RollingInterval.Day);
const string similar1 = "log-0.txt";
- const string similar2 = "log-helloyou.txt";
+ const string similar2 = "log-hello.txt";
var matched = roller.SelectMatches(new[] { similar1, similar2 });
- Assert.Equal(0, matched.Count());
+ Assert.Empty(matched);
}
[Fact]
@@ -86,7 +82,7 @@ public void MatchingSelectsFiles(string template, string zeroth, string thirtyFi
var roller = new PathRoller(template, interval);
var matched = roller.SelectMatches(new[] { zeroth, thirtyFirst }).ToArray();
Assert.Equal(2, matched.Length);
- Assert.Equal(null, matched[0].SequenceNumber);
+ Assert.Null(matched[0].SequenceNumber);
Assert.Equal(31, matched[1].SequenceNumber);
}
diff --git a/test/Serilog.Sinks.File.Tests/WriteCountingStreamTests.cs b/test/Serilog.Sinks.File.Tests/WriteCountingStreamTests.cs
new file mode 100644
index 0000000..887ffe2
--- /dev/null
+++ b/test/Serilog.Sinks.File.Tests/WriteCountingStreamTests.cs
@@ -0,0 +1,83 @@
+using System.IO;
+using System.Text;
+using Serilog.Sinks.File.Tests.Support;
+using Xunit;
+
+namespace Serilog.Sinks.File.Tests
+{
+ public class WriteCountingStreamTests
+ {
+ [Fact]
+ public void CountedLengthIsResetToStreamLengthIfNewSizeIsSmaller()
+ {
+ // If we counted 10 bytes written and SetLength was called with a smaller length (e.g. 5)
+ // we adjust the counter to the new byte count of the file to reflect reality
+
+ using (var tmp = TempFolder.ForCaller())
+ {
+ var path = tmp.AllocateFilename("txt");
+
+ long streamLengthAfterSetLength;
+ long countedLengthAfterSetLength;
+
+ using (var fileStream = System.IO.File.Open(path, FileMode.Create, FileAccess.Write, FileShare.Read))
+ using (var countingStream = new WriteCountingStream(fileStream))
+ using (var writer = new StreamWriter(countingStream, new UTF8Encoding(false)))
+ {
+ writer.WriteLine("Hello, world!");
+ writer.Flush();
+
+ countingStream.SetLength(5);
+ streamLengthAfterSetLength = countingStream.Length;
+ countedLengthAfterSetLength = countingStream.CountedLength;
+ }
+
+ Assert.Equal(5, streamLengthAfterSetLength);
+ Assert.Equal(5, countedLengthAfterSetLength);
+
+ var lines = System.IO.File.ReadAllLines(path);
+
+ Assert.Single(lines);
+ Assert.Equal("Hello", lines[0]);
+ }
+ }
+
+ [Fact]
+ public void CountedLengthRemainsTheSameIfNewSizeIsLarger()
+ {
+ // If we counted 10 bytes written and SetLength was called with a larger length (e.g. 100)
+ // we leave the counter intact because our position on the stream remains the same... The
+ // file just grew larger in size
+
+ using (var tmp = TempFolder.ForCaller())
+ {
+ var path = tmp.AllocateFilename("txt");
+
+ long streamLengthBeforeSetLength;
+ long streamLengthAfterSetLength;
+ long countedLengthAfterSetLength;
+
+ using (var fileStream = System.IO.File.Open(path, FileMode.Create, FileAccess.Write, FileShare.Read))
+ using (var countingStream = new WriteCountingStream(fileStream))
+ using (var writer = new StreamWriter(countingStream, new UTF8Encoding(false)))
+ {
+ writer.WriteLine("Hello, world!");
+ writer.Flush();
+
+ streamLengthBeforeSetLength = countingStream.CountedLength;
+ countingStream.SetLength(100);
+ streamLengthAfterSetLength = countingStream.Length;
+ countedLengthAfterSetLength = countingStream.CountedLength;
+ }
+
+ Assert.Equal(100, streamLengthAfterSetLength);
+ Assert.Equal(streamLengthBeforeSetLength, countedLengthAfterSetLength);
+
+ var lines = System.IO.File.ReadAllLines(path);
+
+ Assert.Equal(2, lines.Length);
+ Assert.Equal("Hello, world!", lines[0]);
+ }
+ }
+ }
+}
pFad - Phonifier reborn
Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies:
Alternative Proxy
pFad Proxy
pFad v3 Proxy
pFad v4 Proxy