Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Update
  • Loading branch information
JamesNK committed Aug 27, 2024
commit db0ec0a6f791edf5f7f4db1fb2d3bf680d98bccf
25 changes: 9 additions & 16 deletions src/Aspire.Hosting/ApplicationModel/ResourceLoggerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,22 +246,12 @@ public async IAsyncEnumerable<IReadOnlyList<LogLine>> WatchAsync([EnumeratorCanc

using var _ = _logStreamCts.Token.Register(() => channel.Writer.TryComplete());

LogEntry[]? backlogSnapshot = null;
void Log(LogEntry log)
{
lock (_lock)
{
// Don't write to the channel until the backlog snapshot is accessed.
// This prevents duplicate logs in result.
if (backlogSnapshot != null)
{
channel.Writer.TryWrite(log);
}
}
}
void Log(LogEntry log) => channel.Writer.TryWrite(log);

LogEntry[] backlogSnapshot;
lock (_lock)
{
// Get back
backlogSnapshot = GetBacklogSnapshot();
OnNewLog += Log;
}
Expand Down Expand Up @@ -315,7 +305,9 @@ private event Action<LogEntry> OnNewLog
{
Debug.Assert(Monitor.IsEntered(_lock));

var raiseSubscribersChanged = _onNewLog is null; // is this the first subscriber?
// When this is the first subscriber, raise event so WatchAnySubscribersAsync publishes an update.
// Is this the first subscriber?
var raiseSubscribersChanged = _onNewLog is null;

_onNewLog += value;

Expand All @@ -330,8 +322,9 @@ private event Action<LogEntry> OnNewLog

_onNewLog -= value;

var raiseSubscribersChanged = _onNewLog is null; // is this the last subscriber?

// When there are no more subscribers, raise event so WatchAnySubscribersAsync publishes an update.
// Is this the last subscriber?
var raiseSubscribersChanged = _onNewLog is null;
if (raiseSubscribersChanged)
{
// Clear backlog immediately.
Expand Down