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
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public bool TryDequeue(out LogMessageEntry item)
Monitor.Wait(_messageQueue);
}

if (_messageQueue.Count > 0 && !_isAddingCompleted)
if (_messageQueue.Count > 0)
{
item = _messageQueue.Dequeue();
if (_messageQueue.Count == MaxQueueLength - 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,32 @@ public void LogAfterDisposeWritesLog()
Assert.Equal(2, sink.Writes.Count);
}

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
public void LogsFlushedAfterDispose()
{
// Arrange
var sink = new ConsoleSink();
var console = new TestConsole(sink);
var processor = new ConsoleLoggerProcessor(console, null!, ConsoleLoggerQueueFullMode.Wait, 1024);

var logger = new ConsoleLogger(_loggerName, loggerProcessor: processor,
new SimpleConsoleFormatter(new TestFormatterOptionsMonitor<SimpleConsoleFormatterOptions>(new SimpleConsoleFormatterOptions())),
null, new ConsoleLoggerOptions());
Assert.Null(logger.Options.FormatterName);
UpdateFormatterOptions(logger.Formatter, logger.Options);

// Act
const int repetitions = 1000;
for (int i = 0; i < repetitions; i++)
{
logger.LogInformation("Message #{i}", i);
}
processor.Dispose();

// Assert
Assert.Equal(2*repetitions, sink.Writes.Count); // 2 writes per message (category and msg)
}

[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
[InlineData(-1)]
[InlineData(0)]
Expand Down Expand Up @@ -120,7 +146,6 @@ public void Write(string message)

[OuterLoop]
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/71242")]
public void ThrowDuringProcessLog_ShutsDownGracefully()
{
var console = new TimesWriteCalledConsole();
Expand All @@ -147,8 +172,10 @@ public void ThrowDuringProcessLog_ShutsDownGracefully()
while (console.TimesWriteCalled != 2); // wait until the logs are processed
Assert.Equal(2, console.TimesWriteCalled);
logger.LogError("Causing exception to throw in {ClassName} using {DesiredConsole}", nameof(ConsoleLoggerProcessor), nameof(WriteThrowingConsole));
logger.LogInformation("After the write logic threw exception, {ClassName} stopped gracefully, no longer processing next logs", nameof(ConsoleLoggerProcessor));
Assert.Equal(2, console.TimesWriteCalled);
logger.LogInformation("After the write logic threw exception, {ClassName} stopped gracefully, finish processing queued logs", nameof(ConsoleLoggerProcessor));
// disposing makes sure that all queued messages are flushed
processor.Dispose();
Assert.Equal(3, console.TimesWriteCalled);
}

private static void UpdateFormatterOptions(ConsoleFormatter formatter, ConsoleLoggerOptions deprecatedFromOptions)
Expand Down