microsoft/aspire · error · InvalidOperationException

Getting all logs requires the ResourceLoggerService…

Error message

Getting all logs requires the ResourceLoggerService instance created by DI.

What it means

The IConsoleLogsService resolved outside DI is a FakeConsoleLogsService placeholder whose GetAllLogsAsync always throws. Real log retrieval requires the ResourceLoggerService instance registered in the dependency injection container.

Solutions

  1. Resolve IConsoleLogsService from the Aspire host's DI container
  2. Use ResourceLoggerService directly when operating outside the host lifecycle
  3. In tests, register a real or fake ResourceLoggerService-backed implementation in the test DI container

Example fix

// before
var logsService = new FakeConsoleLogsService(); // throws on use
// after
var logsService = serviceProvider.GetRequiredService<IConsoleLogsService>();
Defensive patterns

Strategy: try-catch

Try / catch

try { await logsService.GetAllLogsAsync(name, ct); }
catch (InvalidOperationException ex) when (ex.Message.Contains("created by DI")) { /* resolve the service from the host DI container instead */ }

Prevention

When it happens

Trigger: Calling GetAllLogsAsync on an IConsoleLogsService obtained without DI — e.g., manually constructing the service or resolving it from an empty/non-Aspire service provider.

Common situations: Unit tests or tools instantiating log services by hand; retrieving console logs before the AppHost's DI container is built.

Understand the failure class

Background: "not installed", "pip install", "required for": how missing-dependency errors surface across open-source libraries — this error's family across 34 libraries.

Related errors


AI-assisted analysis of microsoft/aspire@25830f84bd (2026-09-16). Data as JSON: /api/errors/211682ef4a9f9cd8. Report an issue: GitHub.

Appendix: source

Thrown at src/Aspire.Hosting/ApplicationModel/ResourceLoggerService.cs:899

            return;
        }

        // Complete all loggers to signal that no more logs will be written.
        foreach (var logger in _loggers)
        {
            logger.Value.Complete();
        }

        // Cancel but don't dispose - other methods may still be accessing _disposing.Token
        // The CTS will be garbage collected with the service.
        _disposing.Cancel();
    }

    private sealed class FakeConsoleLogsService : IConsoleLogsService
    {
        public IAsyncEnumerable<IReadOnlyList<LogEntry>> GetAllLogsAsync(string resourceName, CancellationToken cancellationToken)
        {
            throw new InvalidOperationException($"Getting all logs requires the {nameof(ResourceLoggerService)} instance created by DI.");
        }
    }
}

/// <summary>
/// Represents a log subscriber for a resource.
/// </summary>
/// <param name="Name">The the resource name.</param>
/// <param name="AnySubscribers">Determines if there are any subscribers.</param>
public readonly record struct LogSubscriber(string Name, bool AnySubscribers);

View on GitHub (pinned to 25830f84bd)