{"record":{"id":"211682ef4a9f9cd8","repo":"microsoft/aspire","slug":"getting-all-logs-requires-the-resourceloggerservice-instance","errorCode":null,"errorMessage":"Getting all logs requires the ResourceLoggerService instance created by DI.","messagePattern":"Getting all logs requires the ResourceLoggerService instance created by DI\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting/ApplicationModel/ResourceLoggerService.cs","lineNumber":899,"sourceCode":"            return;\n        }\n\n        // Complete all loggers to signal that no more logs will be written.\n        foreach (var logger in _loggers)\n        {\n            logger.Value.Complete();\n        }\n\n        // Cancel but don't dispose - other methods may still be accessing _disposing.Token\n        // The CTS will be garbage collected with the service.\n        _disposing.Cancel();\n    }\n\n    private sealed class FakeConsoleLogsService : IConsoleLogsService\n    {\n        public IAsyncEnumerable<IReadOnlyList<LogEntry>> GetAllLogsAsync(string resourceName, CancellationToken cancellationToken)\n        {\n            throw new InvalidOperationException($\"Getting all logs requires the {nameof(ResourceLoggerService)} instance created by DI.\");\n        }\n    }\n}\n\n/// <summary>\n/// Represents a log subscriber for a resource.\n/// </summary>\n/// <param name=\"Name\">The the resource name.</param>\n/// <param name=\"AnySubscribers\">Determines if there are any subscribers.</param>\npublic readonly record struct LogSubscriber(string Name, bool AnySubscribers);\n","sourceCodeStart":881,"sourceCodeEnd":910,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting/ApplicationModel/ResourceLoggerService.cs#L881-L910","documentation":"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.","triggerScenarios":"Calling GetAllLogsAsync on an IConsoleLogsService obtained without DI — e.g., manually constructing the service or resolving it from an empty/non-Aspire service provider.","commonSituations":"Unit tests or tools instantiating log services by hand; retrieving console logs before the AppHost's DI container is built.","solutions":["Resolve IConsoleLogsService from the Aspire host's DI container","Use ResourceLoggerService directly when operating outside the host lifecycle","In tests, register a real or fake ResourceLoggerService-backed implementation in the test DI container"],"exampleFix":"// before\nvar logsService = new FakeConsoleLogsService(); // throws on use\n// after\nvar logsService = serviceProvider.GetRequiredService<IConsoleLogsService>();","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { await logsService.GetAllLogsAsync(name, ct); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"created by DI\")) { /* resolve the service from the host DI container instead */ }","preventionTips":["Always obtain IConsoleLogsService via dependency injection","Never construct log service placeholders manually in production code"],"tags":["aspire","logging","dependency-injection"],"backgroundTag":"missing-dependency","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}