|
| 1 | +#if ATOMIC_APPEND |
| 2 | + |
| 3 | +using System; |
| 4 | +using System.IO; |
| 5 | +using Xunit; |
| 6 | +using Serilog.Formatting.Json; |
| 7 | +using Serilog.Sinks.File.Tests.Support; |
| 8 | +using Serilog.Sinks.File; |
| 9 | +using Serilog.Tests.Support; |
| 10 | + |
| 11 | +namespace Serilog.Sinks.File.Tests |
| 12 | +{ |
| 13 | + public class SharedFileSinkTests |
| 14 | + { |
| 15 | + [Fact] |
| 16 | + public void FileIsWrittenIfNonexistent() |
| 17 | + { |
| 18 | + using (var tmp = TempFolder.ForCaller()) |
| 19 | + { |
| 20 | + var nonexistent = tmp.AllocateFilename("txt"); |
| 21 | + var evt = Some.LogEvent("Hello, world!"); |
| 22 | + |
| 23 | + using (var sink = new SharedFileSink(nonexistent, new JsonFormatter(), null)) |
| 24 | + { |
| 25 | + sink.Emit(evt); |
| 26 | + } |
| 27 | + |
| 28 | + var lines = System.IO.File.ReadAllLines(nonexistent); |
| 29 | + Assert.Contains("Hello, world!", lines[0]); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + [Fact] |
| 34 | + public void FileIsAppendedToWhenAlreadyCreated() |
| 35 | + { |
| 36 | + using (var tmp = TempFolder.ForCaller()) |
| 37 | + { |
| 38 | + var path = tmp.AllocateFilename("txt"); |
| 39 | + var evt = Some.LogEvent("Hello, world!"); |
| 40 | + |
| 41 | + using (var sink = new SharedFileSink(path, new JsonFormatter(), null)) |
| 42 | + { |
| 43 | + sink.Emit(evt); |
| 44 | + } |
| 45 | + |
| 46 | + using (var sink = new SharedFileSink(path, new JsonFormatter(), null)) |
| 47 | + { |
| 48 | + sink.Emit(evt); |
| 49 | + } |
| 50 | + |
| 51 | + var lines = System.IO.File.ReadAllLines(path); |
| 52 | + Assert.Contains("Hello, world!", lines[0]); |
| 53 | + Assert.Contains("Hello, world!", lines[1]); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + [Fact] |
| 58 | + public void WhenLimitIsSpecifiedFileSizeIsRestricted() |
| 59 | + { |
| 60 | + const int maxBytes = 5000; |
| 61 | + const int eventsToLimit = 10; |
| 62 | + |
| 63 | + using (var tmp = TempFolder.ForCaller()) |
| 64 | + { |
| 65 | + var path = tmp.AllocateFilename("txt"); |
| 66 | + var evt = Some.LogEvent(new string('n', maxBytes / eventsToLimit)); |
| 67 | + |
| 68 | + using (var sink = new SharedFileSink(path, new JsonFormatter(), maxBytes)) |
| 69 | + { |
| 70 | + for (var i = 0; i < eventsToLimit * 2; i++) |
| 71 | + { |
| 72 | + sink.Emit(evt); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + var size = new FileInfo(path).Length; |
| 77 | + Assert.True(size > maxBytes); |
| 78 | + Assert.True(size < maxBytes * 2); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + [Fact] |
| 83 | + public void WhenLimitIsNotSpecifiedFileSizeIsNotRestricted() |
| 84 | + { |
| 85 | + const int maxBytes = 5000; |
| 86 | + const int eventsToLimit = 10; |
| 87 | + |
| 88 | + using (var tmp = TempFolder.ForCaller()) |
| 89 | + { |
| 90 | + var path = tmp.AllocateFilename("txt"); |
| 91 | + var evt = Some.LogEvent(new string('n', maxBytes / eventsToLimit)); |
| 92 | + |
| 93 | + using (var sink = new SharedFileSink(path, new JsonFormatter(), null)) |
| 94 | + { |
| 95 | + for (var i = 0; i < eventsToLimit * 2; i++) |
| 96 | + { |
| 97 | + sink.Emit(evt); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + var size = new FileInfo(path).Length; |
| 102 | + Assert.True(size > maxBytes * 2); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +#endif |
0 commit comments