|  | 
| 4 | 4 | using Serilog.Formatting.Json; | 
| 5 | 5 | using Serilog.Sinks.File.Tests.Support; | 
| 6 | 6 | using Serilog.Tests.Support; | 
|  | 7 | +using System.Text; | 
|  | 8 | +using Serilog.Tests; | 
| 7 | 9 | 
 | 
| 8 | 10 | namespace Serilog.Sinks.File.Tests | 
| 9 | 11 | { | 
| @@ -99,6 +101,75 @@ public void WhenLimitIsNotSpecifiedFileSizeIsNotRestricted() | 
| 99 | 101 |                 Assert.True(size > maxBytes * 2); | 
| 100 | 102 |             } | 
| 101 | 103 |         } | 
|  | 104 | + | 
|  | 105 | + | 
|  | 106 | +        [Fact] | 
|  | 107 | +        public void WhenLimitIsSpecifiedAndEncodingHasPreambleDataIsCorrectlyAppendedToFileSink() | 
|  | 108 | +        { | 
|  | 109 | +            long? maxBytes = 5000; | 
|  | 110 | +            var encoding = Encoding.UTF8; | 
|  | 111 | + | 
|  | 112 | +            Assert.True(encoding.GetPreamble().Length > 0); | 
|  | 113 | +            WriteTwoEventsAndCheckOutputFileLength(maxBytes, encoding); | 
|  | 114 | +        } | 
|  | 115 | + | 
|  | 116 | +        [Fact] | 
|  | 117 | +        public void WhenLimitIsNotSpecifiedAndEncodingHasPreambleDataIsCorrectlyAppendedToFileSink() | 
|  | 118 | +        { | 
|  | 119 | +            long? maxBytes = null; | 
|  | 120 | +            var encoding = Encoding.UTF8; | 
|  | 121 | + | 
|  | 122 | +            Assert.True(encoding.GetPreamble().Length > 0); | 
|  | 123 | +            WriteTwoEventsAndCheckOutputFileLength(maxBytes, encoding); | 
|  | 124 | +        } | 
|  | 125 | + | 
|  | 126 | +        [Fact] | 
|  | 127 | +        public void WhenLimitIsSpecifiedAndEncodingHasNoPreambleDataIsCorrectlyAppendedToFileSink() | 
|  | 128 | +        { | 
|  | 129 | +            long? maxBytes = 5000; | 
|  | 130 | +            var encoding = new UTF8Encoding(false); | 
|  | 131 | + | 
|  | 132 | +            Assert.Equal(0, encoding.GetPreamble().Length); | 
|  | 133 | +            WriteTwoEventsAndCheckOutputFileLength(maxBytes, encoding); | 
|  | 134 | +        } | 
|  | 135 | + | 
|  | 136 | +        [Fact] | 
|  | 137 | +        public void WhenLimitIsNotSpecifiedAndEncodingHasNoPreambleDataIsCorrectlyAppendedToFileSink() | 
|  | 138 | +        { | 
|  | 139 | +            long? maxBytes = null; | 
|  | 140 | +            var encoding = new UTF8Encoding(false); | 
|  | 141 | + | 
|  | 142 | +            Assert.Equal(0, encoding.GetPreamble().Length); | 
|  | 143 | +            WriteTwoEventsAndCheckOutputFileLength(maxBytes, encoding); | 
|  | 144 | +        } | 
|  | 145 | + | 
|  | 146 | +        static void WriteTwoEventsAndCheckOutputFileLength(long? maxBytes, Encoding encoding) | 
|  | 147 | +        { | 
|  | 148 | +            using (var tmp = TempFolder.ForCaller()) | 
|  | 149 | +            { | 
|  | 150 | +                var path = tmp.AllocateFilename("txt"); | 
|  | 151 | +                var evt = Some.LogEvent("Irrelevant as it will be replaced by the formatter"); | 
|  | 152 | +                var actualEventOutput = "x"; | 
|  | 153 | +                var formatter = new FixedOutputFormatter(actualEventOutput); | 
|  | 154 | +                var eventOuputLength = encoding.GetByteCount(actualEventOutput); | 
|  | 155 | + | 
|  | 156 | +                using (var sink = new FileSink(path, formatter, maxBytes, encoding: encoding)) | 
|  | 157 | +                { | 
|  | 158 | +                    sink.Emit(evt); | 
|  | 159 | +                } | 
|  | 160 | +                var size = new FileInfo(path).Length; | 
|  | 161 | +                Assert.Equal(encoding.GetPreamble().Length + eventOuputLength, size); | 
|  | 162 | + | 
|  | 163 | +                //write a second event to the same file | 
|  | 164 | +                using (var sink = new FileSink(path, formatter, maxBytes, encoding: encoding)) | 
|  | 165 | +                { | 
|  | 166 | +                    sink.Emit(evt); | 
|  | 167 | +                } | 
|  | 168 | + | 
|  | 169 | +                size = new FileInfo(path).Length; | 
|  | 170 | +                Assert.Equal(encoding.GetPreamble().Length + eventOuputLength * 2, size); | 
|  | 171 | +            } | 
|  | 172 | +        } | 
| 102 | 173 |     } | 
| 103 | 174 | } | 
| 104 | 175 | 
 | 
0 commit comments