Skip to content

Commit f9e44c9

Browse files
wtgodbebenedikt257dougbu
authored
[release/6.0] Make tests for FileLoggerProcessor system time independent (#42115)
* Fix conflicts * Fixup * Fixup * Update src/Middleware/HttpLogging/test/FileLoggerProcessorTests.cs Co-authored-by: Benedikt <[email protected]> Co-authored-by: Doug Bunting <[email protected]>
1 parent 5ad4faf commit f9e44c9

File tree

5 files changed

+156
-86
lines changed

5 files changed

+156
-86
lines changed

src/Middleware/HttpLogging/src/FileLoggerProcessor.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ internal partial class FileLoggerProcessor : IAsyncDisposable
3030
private bool _maxFilesReached;
3131
private TimeSpan _flushInterval;
3232
private W3CLoggingFields _fields;
33-
private DateTime _today = DateTime.Now;
33+
private DateTime _today;
3434
private bool _firstFile = true;
3535

3636
private readonly IOptionsMonitor<W3CLoggerOptions> _options;
@@ -40,6 +40,9 @@ internal partial class FileLoggerProcessor : IAsyncDisposable
4040
private readonly Task _outputTask;
4141
private readonly CancellationTokenSource _cancellationTokenSource;
4242

43+
// Internal to allow for testing
44+
internal ISystemDateTime SystemDateTime {get; set;} = new SystemDateTime();
45+
4346
private readonly object _pathLock = new object();
4447

4548
public FileLoggerProcessor(IOptionsMonitor<W3CLoggerOptions> options, IHostEnvironment environment, ILoggerFactory factory)
@@ -98,6 +101,8 @@ public FileLoggerProcessor(IOptionsMonitor<W3CLoggerOptions> options, IHostEnvir
98101
}
99102
});
100103

104+
_today = SystemDateTime.Now;
105+
101106
// Start message queue processor
102107
_cancellationTokenSource = new CancellationTokenSource();
103108
_outputTask = Task.Run(ProcessLogQueue);
@@ -155,7 +160,7 @@ private async Task ProcessLogQueue()
155160
private async Task WriteMessagesAsync(List<string> messages, CancellationToken cancellationToken)
156161
{
157162
// Files are written up to _maxFileSize before rolling to a new file
158-
DateTime today = DateTime.Now;
163+
DateTime today = SystemDateTime.Now;
159164

160165
if (!TryCreateDirectory())
161166
{
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace Microsoft.AspNetCore.HttpLogging
5+
{
6+
internal interface ISystemDateTime
7+
{
8+
/// <summary>
9+
/// Retrieves the date and time currently set for this machine.
10+
/// </summary>
11+
DateTime Now { get; }
12+
}
13+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace Microsoft.AspNetCore.HttpLogging
5+
{
6+
internal class SystemDateTime : ISystemDateTime
7+
{
8+
public DateTime Now => DateTime.Now;
9+
}
10+
}

0 commit comments

Comments
 (0)