Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/Middleware/HttpLogging/src/FileLoggerProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ internal partial class FileLoggerProcessor : IAsyncDisposable
private bool _maxFilesReached;
private TimeSpan _flushInterval;
private W3CLoggingFields _fields;
private DateTime _today = DateTime.Now;
private DateTime _today;
private bool _firstFile = true;

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

// Internal to allow for testing
internal ISystemDateTime SystemDateTime {get; set;} = new SystemDateTime();

private readonly object _pathLock = new object();

public FileLoggerProcessor(IOptionsMonitor<W3CLoggerOptions> options, IHostEnvironment environment, ILoggerFactory factory)
Expand Down Expand Up @@ -98,6 +101,8 @@ public FileLoggerProcessor(IOptionsMonitor<W3CLoggerOptions> options, IHostEnvir
}
});

_today = SystemDateTime.Now;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reckon this initialization is kind of redundant here - the first invocation of a write will set the date to the current datetime anyhow when the absolute difference between _today and it is positive.
Kept it because it makes it consistent with the previous implementation though - let me know if you think it should be removed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like explicitly initializing it when the Processor starts, rather than when the first message is written (that is, don't remove this)


// Start message queue processor
_cancellationTokenSource = new CancellationTokenSource();
_outputTask = Task.Run(ProcessLogQueue);
Expand Down Expand Up @@ -155,7 +160,7 @@ private async Task ProcessLogQueue()
private async Task WriteMessagesAsync(List<string> messages, CancellationToken cancellationToken)
{
// Files are written up to _maxFileSize before rolling to a new file
DateTime today = DateTime.Now;
DateTime today = SystemDateTime.Now;
var fullName = GetFullName(today);
// Don't write to an incomplete file left around by a previous FileLoggerProcessor
if (_firstFile)
Expand Down
13 changes: 13 additions & 0 deletions src/Middleware/HttpLogging/src/ISystemDateTime.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.AspNetCore.HttpLogging
{
internal interface ISystemDateTime
{
/// <summary>
/// Retrieves the date and time currently set for this machine.
/// </summary>
DateTime Now { get; }
}
}
10 changes: 10 additions & 0 deletions src/Middleware/HttpLogging/src/SystemDateTime.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.AspNetCore.HttpLogging
{
internal class SystemDateTime : ISystemDateTime
{
public DateTime Now => DateTime.Now;
}
}
Loading