Skip to content
代码片段 群组 项目
未验证 提交 f9e44c92 编辑于 作者: William Godbe's avatar William Godbe 提交者: GitHub
浏览文件

[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: default avatarBenedikt <benedikt.achatz@gmail.com>
Co-authored-by: default avatarDoug Bunting <6431421+dougbu@users.noreply.github.com>
上级 5ad4faf6
No related branches found
No related tags found
无相关合并请求
......@@ -30,7 +30,7 @@ namespace Microsoft.AspNetCore.HttpLogging
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;
......@@ -40,6 +40,9 @@ namespace Microsoft.AspNetCore.HttpLogging
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)
......@@ -98,6 +101,8 @@ namespace Microsoft.AspNetCore.HttpLogging
}
});
_today = SystemDateTime.Now;
// Start message queue processor
_cancellationTokenSource = new CancellationTokenSource();
_outputTask = Task.Run(ProcessLogQueue);
......@@ -155,7 +160,7 @@ namespace Microsoft.AspNetCore.HttpLogging
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;
if (!TryCreateDirectory())
{
......
// 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; }
}
}
// 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;
}
}
// 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 MockSystemDateTime : ISystemDateTime
{
public DateTime Now { get; set; }
}
}
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册