Skip to content

Commit 6ab6787

Browse files
committed
Just structured logging to output
1 parent 07dca6d commit 6ab6787

5 files changed

+28
-7
lines changed

src/AspireManifestGen.csproj

+4
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
<ItemGroup>
4848
<Compile Include="Commands\InfraSynth.cs" />
4949
<Compile Include="Options\General.cs" />
50+
<Compile Include="OutputWindowManager.cs" />
5051
<Compile Include="Properties\AssemblyInfo.cs" />
5152
<Compile Include="Commands\ManifestGen.cs" />
5253
<Compile Include="AspireManifestGenPackage.cs" />
@@ -94,6 +95,9 @@
9495
<PackageReference Include="Community.VisualStudio.Toolkit.17" Version="17.0.507" ExcludeAssets="Runtime">
9596
<IncludeAssets>compile; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
9697
</PackageReference>
98+
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions">
99+
<Version>8.0.0</Version>
100+
</PackageReference>
97101
<PackageReference Include="Microsoft.VSSDK.BuildTools" Version="17.8.2365">
98102
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
99103
<PrivateAssets>all</PrivateAssets>

src/AspireManifestGenPackage.cs

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public sealed class AspireManifestGenPackage : ToolkitPackage
2424

2525
protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress<ServiceProgressData> progress)
2626
{
27+
OutputWindowManager.AspireOutputPane = await VS.Windows.CreateOutputWindowPaneAsync(".NET Aspire", true);
2728
await this.RegisterCommandsAsync();
2829
}
2930
}

src/Commands/InfraSynth.cs

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using AspireManifestGen.Options;
22
using CliWrap;
3+
using Microsoft.Extensions.Logging;
34
using System.Collections.Generic;
45
using System.Diagnostics;
56
using System.IO;
@@ -22,13 +23,14 @@ protected override async Task ExecuteAsync(OleMenuCmdEventArgs e, Project projec
2223
{
2324
var stdOutBuffer = new StringBuilder();
2425
var stdErrBuffer = new StringBuilder();
25-
OutputWindowPane pane = await VS.Windows.GetOutputWindowPaneAsync(Community.VisualStudio.Toolkit.Windows.VSOutputWindowPane.General);
26+
OutputWindowPane pane = OutputWindowManager.AspireOutputPane;
2627
var options = await General.GetLiveInstanceAsync();
2728

2829
var projectPath = FindAzureYaml(project.FullPath);
2930

3031
await VS.StatusBar.StartAnimationAsync(StatusAnimation.Sync);
3132
await VS.StatusBar.ShowProgressAsync(STATUS_MESSAGE, 1, 2);
33+
await pane.WriteLineAsync(OutputWindowManager.GenerateOutputMessage(STATUS_MESSAGE, "InfraSynth", LogLevel.Information));
3234

3335
var command = $"infra synth --force --no-prompt" + (options.AzdDebug ? " --debug" : "");
3436

@@ -44,13 +46,13 @@ protected override async Task ExecuteAsync(OleMenuCmdEventArgs e, Project projec
4446

4547
if (result.ExitCode != 0)
4648
{
47-
await pane.WriteLineAsync($"[AZD]: Unable to synthesize infrastructure:{stdErr}:{result.ExitCode}");
49+
await pane.WriteLineAsync(OutputWindowManager.GenerateOutputMessage($"Unable to synthesize infrastructure:{stdErr}:{result.ExitCode}", "InfraSynth", LogLevel.Error));
4850
goto Cleanup;
4951
}
5052
else
5153
{
52-
await pane.WriteLineAsync($"[AZD]: infra synth completed");
53-
await pane.WriteLineAsync($"[AZD]: {stdOutBuffer}");
54+
await pane.WriteLineAsync(OutputWindowManager.GenerateOutputMessage($"Infra synth completed", "InfraSynth", LogLevel.Information));
55+
await pane.WriteLineAsync(OutputWindowManager.GenerateOutputMessage($"{stdOutBuffer}", "InfraSynth", LogLevel.Information));
5456
}
5557

5658
await VS.Documents.OpenAsync(Path.Combine(projectPath, "infra", "resources.bicep"));

src/Commands/ManifestGen.cs

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using AspireManifestGen.Options;
22
using CliWrap;
3+
using Microsoft.Extensions.Logging;
34
using System.Collections.Generic;
45
using System.Diagnostics;
56
using System.IO;
@@ -22,7 +23,7 @@ protected override async Task ExecuteAsync(OleMenuCmdEventArgs e, Project projec
2223
{
2324
var stdOutBuffer = new StringBuilder();
2425
var stdErrBuffer = new StringBuilder();
25-
OutputWindowPane pane = await VS.Windows.GetOutputWindowPaneAsync(Community.VisualStudio.Toolkit.Windows.VSOutputWindowPane.General);
26+
OutputWindowPane pane = OutputWindowManager.AspireOutputPane;
2627

2728
var projectPath = Path.GetDirectoryName(project.FullPath);
2829

@@ -41,6 +42,7 @@ protected override async Task ExecuteAsync(OleMenuCmdEventArgs e, Project projec
4142

4243
await VS.StatusBar.StartAnimationAsync(StatusAnimation.Build);
4344
await VS.StatusBar.ShowProgressAsync(STATUS_MESSAGE, 1, 2);
45+
await pane.WriteLineAsync(OutputWindowManager.GenerateOutputMessage(STATUS_MESSAGE, "ManifestGen", LogLevel.Information));
4446

4547
var result = await Cli.Wrap("dotnet")
4648
.WithArguments($"msbuild /t:GenerateAspireManifest /p:AspireManifestPublishOutputPath={manifestPath}")
@@ -55,12 +57,12 @@ protected override async Task ExecuteAsync(OleMenuCmdEventArgs e, Project projec
5557
// TODO: Need better error handling, issue #3
5658
if (result.ExitCode != 0)
5759
{
58-
await pane.WriteLineAsync($"[.NET Aspire]: Unable to create manifest:{stdErr}:{result.ExitCode}");
60+
await pane.WriteLineAsync(OutputWindowManager.GenerateOutputMessage($"Unable to create manifest:{stdErr}:{result.ExitCode}", "ManifestGen", LogLevel.Error));
5961
goto Cleanup;
6062
}
6163
else
6264
{
63-
await pane.WriteLineAsync($"[.NET Aspire]: Manifest created at {manifestPath}");
65+
await pane.WriteLineAsync(OutputWindowManager.GenerateOutputMessage($"Manifest created at {manifestPath}", "ManifestGen", LogLevel.Information));
6466
}
6567

6668
await VS.Documents.OpenAsync(manifestPath);

src/OutputWindowManager.cs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using Microsoft.Extensions.Logging;
2+
3+
public static class OutputWindowManager
4+
{
5+
public static OutputWindowPane AspireOutputPane { get; set; }
6+
7+
public static string GenerateOutputMessage(string message, string source, LogLevel logLevel)
8+
{
9+
var timestamp = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss.fffZ");
10+
return $"[{logLevel.ToString().ToUpperInvariant()}] [{source.ToUpperInvariant()}] [{timestamp}]: {message}";
11+
}
12+
}

0 commit comments

Comments
 (0)