|
4 | 4 | using Serilog.Sinks.File.Tests.Support; |
5 | 5 | using Serilog.Configuration; |
6 | 6 | using Serilog.Core; |
| 7 | +using Serilog.Debugging; |
7 | 8 | using Xunit.Abstractions; |
8 | 9 |
|
9 | 10 | namespace Serilog.Sinks.File.Tests; |
@@ -196,85 +197,71 @@ public void WhenFirstOpeningFailedWithLockRetryDelayed30Minutes() |
196 | 197 | { |
197 | 198 | var fileName = Some.String() + ".txt"; |
198 | 199 | using var temp = new TempFolder(); |
| 200 | + using var log = new LoggerConfiguration() |
| 201 | + .WriteTo.File(Path.Combine(temp.Path, fileName), rollOnFileSizeLimit: true, fileSizeLimitBytes: 1, rollingInterval: RollingInterval.Hour, hooks: new FailOpeningHook(true, 2, 3, 4)) |
| 202 | + .CreateLogger(); |
199 | 203 | LogEvent e1 = Some.InformationEvent(new DateTime(2012, 10, 28)), |
200 | 204 | e2 = Some.InformationEvent(e1.Timestamp.AddSeconds(1)), |
201 | 205 | e3 = Some.InformationEvent(e1.Timestamp.AddMinutes(5)), |
202 | 206 | e4 = Some.InformationEvent(e1.Timestamp.AddMinutes(31)); |
203 | 207 | LogEvent[] logEvents = new[] { e1, e2, e3, e4 }; |
204 | | - |
205 | | - using (var log = new LoggerConfiguration() |
206 | | - .WriteTo.File(Path.Combine(temp.Path, fileName), rollOnFileSizeLimit: true, fileSizeLimitBytes: 1, |
207 | | - rollingInterval: RollingInterval.Hour, hooks: new FailOpeningHook(true, 2, 3, 4)) |
208 | | - .CreateLogger()) |
| 208 | + |
| 209 | + SelfLog.Enable(_testOutputHelper.WriteLine); |
| 210 | + foreach (var logEvent in logEvents) |
209 | 211 | { |
210 | | - foreach (var logEvent in logEvents) |
211 | | - { |
212 | | - Clock.SetTestDateTimeNow(logEvent.Timestamp.DateTime); |
213 | | - log.Write(logEvent); |
214 | | - } |
| 212 | + Clock.SetTestDateTimeNow(logEvent.Timestamp.DateTime); |
| 213 | + log.Write(logEvent); |
215 | 214 | } |
216 | 215 |
|
217 | 216 | var files = Directory.GetFiles(temp.Path) |
218 | 217 | .OrderBy(p => p, StringComparer.OrdinalIgnoreCase) |
219 | 218 | .ToArray(); |
220 | 219 | var pattern = "yyyyMMddHH"; |
221 | 220 |
|
222 | | - foreach (var file in files) |
223 | | - { |
224 | | - _testOutputHelper.WriteLine(file + ": " + System.IO.File.ReadAllText(file)); |
225 | | - } |
226 | | - Assert.Equal(5, files.Length); |
| 221 | + Assert.Equal(4, files.Length); |
227 | 222 | // Successful write of e1: |
228 | 223 | Assert.True(files[0].EndsWith(ExpectedFileName(fileName, e1.Timestamp, pattern)), files[0]); |
229 | 224 | // Failing writes for e2, will be dropped and logged to SelfLog; on lock it will try it three times: |
230 | 225 | Assert.True(files[1].EndsWith("_001.txt"), files[1]); |
231 | 226 | Assert.True(files[2].EndsWith("_002.txt"), files[2]); |
232 | | - Assert.True(files[3].EndsWith("_003.txt"), files[3]); |
233 | 227 | /* e3 will be dropped and logged to SelfLog without new file as it's in the 30 minutes cooldown and roller only starts on next hour! */ |
234 | | - // Successful write of e4: |
235 | | - Assert.True(files[4].EndsWith("_004.txt"), files[4]); |
| 228 | + // Successful write of e4, the third file will be retried after failing initially: |
| 229 | + Assert.True(files[3].EndsWith("_003.txt"), files[3]); |
236 | 230 | } |
237 | 231 |
|
238 | 232 | [Fact] |
239 | 233 | public void WhenFirstOpeningFailedWithoutLockRetryDelayed30Minutes() |
240 | 234 | { |
241 | 235 | var fileName = Some.String() + ".txt"; |
242 | 236 | using var temp = new TempFolder(); |
| 237 | + using var log = new LoggerConfiguration() |
| 238 | + .WriteTo.File(Path.Combine(temp.Path, fileName), rollOnFileSizeLimit: true, fileSizeLimitBytes: 1, rollingInterval: RollingInterval.Hour, hooks: new FailOpeningHook(false, 2)) |
| 239 | + .CreateLogger(); |
243 | 240 | LogEvent e1 = Some.InformationEvent(new DateTime(2012, 10, 28)), |
244 | 241 | e2 = Some.InformationEvent(e1.Timestamp.AddSeconds(1)), |
245 | 242 | e3 = Some.InformationEvent(e1.Timestamp.AddMinutes(5)), |
246 | 243 | e4 = Some.InformationEvent(e1.Timestamp.AddMinutes(31)); |
247 | 244 | LogEvent[] logEvents = new[] { e1, e2, e3, e4 }; |
248 | | - |
249 | | - using (var log = new LoggerConfiguration() |
250 | | - .WriteTo.File(Path.Combine(temp.Path, fileName), rollOnFileSizeLimit: true, fileSizeLimitBytes: 1, |
251 | | - rollingInterval: RollingInterval.Hour, hooks: new FailOpeningHook(false, 2)) |
252 | | - .CreateLogger()) |
| 245 | + |
| 246 | + SelfLog.Enable(_testOutputHelper.WriteLine); |
| 247 | + foreach (var logEvent in logEvents) |
253 | 248 | { |
254 | | - foreach (var logEvent in logEvents) |
255 | | - { |
256 | | - Clock.SetTestDateTimeNow(logEvent.Timestamp.DateTime); |
257 | | - log.Write(logEvent); |
258 | | - } |
| 249 | + Clock.SetTestDateTimeNow(logEvent.Timestamp.DateTime); |
| 250 | + log.Write(logEvent); |
259 | 251 | } |
260 | 252 |
|
261 | 253 | var files = Directory.GetFiles(temp.Path) |
262 | 254 | .OrderBy(p => p, StringComparer.OrdinalIgnoreCase) |
263 | 255 | .ToArray(); |
264 | 256 | var pattern = "yyyyMMddHH"; |
265 | 257 |
|
266 | | - foreach (var file in files) |
267 | | - { |
268 | | - _testOutputHelper.WriteLine(file + ": " + System.IO.File.ReadAllText(file)); |
269 | | - } |
270 | | - Assert.Equal(3, files.Length); |
| 258 | + Assert.Equal(2, files.Length); |
271 | 259 | // Successful write of e1: |
272 | 260 | Assert.True(files[0].EndsWith(ExpectedFileName(fileName, e1.Timestamp, pattern)), files[0]); |
273 | | - // Failing writes for e2, will be dropped and logged to SelfLog; on non-lock it will try it once: |
274 | | - Assert.True(files[1].EndsWith("_001.txt"), files[1]); |
| 261 | + /* Failing writes for e2, will be dropped and logged to SelfLog; on non-lock it will try it once */ |
275 | 262 | /* e3 will be dropped and logged to SelfLog without new file as it's in the 30 minutes cooldown and roller only starts on next hour! */ |
276 | | - // Successful write of e4: |
277 | | - Assert.True(files[2].EndsWith("_002.txt"), files[2]); |
| 263 | + // Successful write of e4, the file will be retried after failing initially: |
| 264 | + Assert.True(files[1].EndsWith("_001.txt"), files[1]); |
278 | 265 | } |
279 | 266 |
|
280 | 267 | [Fact] |
|
0 commit comments