Skip to content

Commit 09d3380

Browse files
committed
Make FileLoggerProcessTests systemtime independent
1 parent 88c720f commit 09d3380

File tree

5 files changed

+91
-84
lines changed

5 files changed

+91
-84
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
var fullName = GetFullName(today);
160165
// Don't write to an incomplete file left around by a previous FileLoggerProcessor
161166
if (_firstFile)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
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+
internal interface ISystemDateTime {
5+
DateTime Now { get; }
6+
}
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)