Skip to content

Commit 76ef5ac

Browse files
authored
Suppress DCP log spew and manifest publishing lifetime logs. (dotnet#642)
* Suppress DCP log spew and manifest publishing lifetime logs.
1 parent b9de4b9 commit 76ef5ac

File tree

6 files changed

+35
-5
lines changed

6 files changed

+35
-5
lines changed

src/Aspire.Hosting/Dcp/DcpHostService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ private async Task LogSocketOutputAsync(Socket socket)
350350
{
351351
// loggerFactory.CreateLogger internally caches, but we may as well cache the logger as well as the string
352352
// for the lifetime of this socket
353-
loggerCache[hashValue] = logger = _loggerFactory.CreateLogger(Encoding.UTF8.GetString(category));
353+
loggerCache[hashValue] = logger = _loggerFactory.CreateLogger($"Aspire.Hosting.Dcp.{Encoding.UTF8.GetString(category)}");
354354
}
355355

356356
return (logger, logLevel, Encoding.UTF8.GetString(message));

src/Aspire.Hosting/DistributedApplication.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44
using System.Diagnostics;
55
using Aspire.Hosting.ApplicationModel;
66
using Aspire.Hosting.Lifecycle;
7+
using Aspire.Hosting.Publishing;
8+
using Microsoft.Extensions.Configuration;
79
using Microsoft.Extensions.DependencyInjection;
810
using Microsoft.Extensions.Hosting;
11+
using Microsoft.Extensions.Options;
912

1013
namespace Aspire.Hosting;
1114

@@ -65,8 +68,27 @@ public async Task StopAsync(CancellationToken cancellationToken = default)
6568
await _host.StopAsync(cancellationToken).ConfigureAwait(false);
6669
}
6770

71+
private void SuppressLifetimeLogsDuringManifestPublishing()
72+
{
73+
var config = (IConfigurationRoot)_host.Services.GetRequiredService<IConfiguration>();
74+
var options = _host.Services.GetRequiredService<IOptions<PublishingOptions>>();
75+
76+
if (options.Value?.Publisher != "manifest")
77+
{
78+
// If we aren't doing manifest pubilshing we want the logs
79+
// to be produced as normal.
80+
return;
81+
}
82+
83+
var hostingLifetimeLoggingLevelSection = config.GetSection("Logging:LogLevel:Microsoft.Hosting.Lifetime");
84+
hostingLifetimeLoggingLevelSection.Value = "Warning";
85+
86+
config.Reload();
87+
}
88+
6889
public async Task RunAsync(CancellationToken cancellationToken = default)
6990
{
91+
SuppressLifetimeLogsDuringManifestPublishing();
7092
await ExecuteBeforeStartHooksAsync(cancellationToken).ConfigureAwait(false);
7193
await _host.RunAsync(cancellationToken).ConfigureAwait(false);
7294
}

src/Aspire.Hosting/Publishing/ManifestPublisher.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
using System.Text.Json;
55
using Aspire.Hosting.ApplicationModel;
66
using Microsoft.Extensions.Hosting;
7+
using Microsoft.Extensions.Logging;
78
using Microsoft.Extensions.Options;
89

910
namespace Aspire.Hosting.Publishing;
1011

11-
public class ManifestPublisher(IOptions<PublishingOptions> options, IHostApplicationLifetime lifetime) : IDistributedApplicationPublisher
12+
public class ManifestPublisher(ILogger<ManifestPublisher> logger, IOptions<PublishingOptions> options, IHostApplicationLifetime lifetime) : IDistributedApplicationPublisher
1213
{
14+
private readonly ILogger<ManifestPublisher> _logger = logger;
1315
private readonly IOptions<PublishingOptions> _options = options;
1416
private readonly IHostApplicationLifetime _lifetime = lifetime;
1517

@@ -34,6 +36,9 @@ protected virtual async Task PublishInternalAsync(DistributedApplicationModel mo
3436
using var jsonWriter = JsonWriter ?? new Utf8JsonWriter(stream, new JsonWriterOptions { Indented = true });
3537

3638
await WriteManifestAsync(model, jsonWriter, cancellationToken).ConfigureAwait(false);
39+
40+
var fullyQualifiedPath = Path.GetFullPath(_options.Value.OutputPath);
41+
_logger.LogInformation("Published manifest to: {manifestPath}", fullyQualifiedPath);
3742
}
3843

3944
protected async Task WriteManifestAsync(DistributedApplicationModel model, Utf8JsonWriter jsonWriter, CancellationToken cancellationToken)

src/Aspire.ProjectTemplates/templates/aspire-empty/AspireApplication-1.AppHost/appsettings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"Logging": {
33
"LogLevel": {
44
"Default": "Information",
5-
"Microsoft.AspNetCore": "Warning"
5+
"Microsoft.AspNetCore": "Warning",
6+
"Aspire.Hosting.Dcp": "Warning"
67
}
78
}
89
}

src/Aspire.ProjectTemplates/templates/aspire-starter/AspireStarterApplication-1.AppHost/appsettings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"Logging": {
33
"LogLevel": {
44
"Default": "Information",
5-
"Microsoft.AspNetCore": "Warning"
5+
"Microsoft.AspNetCore": "Warning",
6+
"Aspire.Hosting.Dcp": "Warning"
67
}
78
}
89
}

tests/Aspire.Hosting.Tests/Helpers/JsonDocumentManifestPublisher.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
using Aspire.Hosting.Publishing;
66
using Microsoft.Extensions.DependencyInjection;
77
using Microsoft.Extensions.Hosting;
8+
using Microsoft.Extensions.Logging;
89
using Microsoft.Extensions.Options;
910

1011
namespace Aspire.Hosting.Tests.Helpers;
1112

12-
internal sealed class JsonDocumentManifestPublisher(IOptions<PublishingOptions> options, IHostApplicationLifetime lifetime) : ManifestPublisher(options, lifetime)
13+
internal sealed class JsonDocumentManifestPublisher(ILogger<ManifestPublisher> logger, IOptions<PublishingOptions> options, IHostApplicationLifetime lifetime) : ManifestPublisher(logger, options, lifetime)
1314
{
1415
protected override async Task PublishInternalAsync(DistributedApplicationModel model, CancellationToken cancellationToken)
1516
{

0 commit comments

Comments
 (0)