-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't log an error when a BackgroundService is canceled due to the ho…
…st being stopped. (#57005) * Don't log an error when a BackgroundService is canceled due to the host being stopped. Fix #56032 * Add volatile to stopping field. * Convert HostTests to use a logger instead of depending on EventSource. * Make it obvious the test is using the default worker template
- Loading branch information
Showing
5 changed files
with
160 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 0 additions & 40 deletions
40
src/libraries/Microsoft.Extensions.Hosting/tests/UnitTests/TestEventListener.cs
This file was deleted.
Oops, something went wrong.
61 changes: 61 additions & 0 deletions
61
src/libraries/Microsoft.Extensions.Hosting/tests/UnitTests/TestLoggerProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Collections.Concurrent; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Microsoft.Extensions.Hosting.Tests | ||
{ | ||
internal class TestLoggerProvider : ILoggerProvider | ||
{ | ||
private readonly TestLogger _logger = new(); | ||
|
||
/// <summary> | ||
/// Provides a snapshot of the current events. | ||
/// </summary> | ||
public LogEvent[] GetEvents() => _logger.GetEvents(); | ||
|
||
public ILogger CreateLogger(string categoryName) | ||
{ | ||
return _logger; | ||
} | ||
|
||
public void Dispose() { } | ||
|
||
private class TestLogger : ILogger | ||
{ | ||
private readonly ConcurrentQueue<LogEvent> _events = new(); | ||
|
||
internal LogEvent[] GetEvents() => _events.ToArray(); | ||
|
||
public IDisposable BeginScope<TState>(TState state) => new Scope(); | ||
|
||
public bool IsEnabled(LogLevel logLevel) => true; | ||
|
||
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter) | ||
{ | ||
_events.Enqueue(new LogEvent() | ||
{ | ||
LogLevel = logLevel, | ||
EventId = eventId, | ||
Message = formatter(state, exception) | ||
}); | ||
} | ||
|
||
private class Scope : IDisposable | ||
{ | ||
public void Dispose() | ||
{ | ||
} | ||
} | ||
} | ||
} | ||
|
||
internal class LogEvent | ||
{ | ||
public LogLevel LogLevel { get; set; } | ||
public EventId EventId { get; set; } | ||
public string Message { get; set; } | ||
} | ||
} |