Skip to content

Commit

Permalink
Just structured logging to output
Browse files Browse the repository at this point in the history
  • Loading branch information
timheuer committed Feb 28, 2024
1 parent 07dca6d commit 6ab6787
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 7 deletions.
4 changes: 4 additions & 0 deletions src/AspireManifestGen.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
<ItemGroup>
<Compile Include="Commands\InfraSynth.cs" />
<Compile Include="Options\General.cs" />
<Compile Include="OutputWindowManager.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Commands\ManifestGen.cs" />
<Compile Include="AspireManifestGenPackage.cs" />
Expand Down Expand Up @@ -94,6 +95,9 @@
<PackageReference Include="Community.VisualStudio.Toolkit.17" Version="17.0.507" ExcludeAssets="Runtime">
<IncludeAssets>compile; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions">
<Version>8.0.0</Version>
</PackageReference>
<PackageReference Include="Microsoft.VSSDK.BuildTools" Version="17.8.2365">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand Down
1 change: 1 addition & 0 deletions src/AspireManifestGenPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public sealed class AspireManifestGenPackage : ToolkitPackage

protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress<ServiceProgressData> progress)
{
OutputWindowManager.AspireOutputPane = await VS.Windows.CreateOutputWindowPaneAsync(".NET Aspire", true);
await this.RegisterCommandsAsync();
}
}
10 changes: 6 additions & 4 deletions src/Commands/InfraSynth.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using AspireManifestGen.Options;
using CliWrap;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
Expand All @@ -22,13 +23,14 @@ protected override async Task ExecuteAsync(OleMenuCmdEventArgs e, Project projec
{
var stdOutBuffer = new StringBuilder();
var stdErrBuffer = new StringBuilder();
OutputWindowPane pane = await VS.Windows.GetOutputWindowPaneAsync(Community.VisualStudio.Toolkit.Windows.VSOutputWindowPane.General);
OutputWindowPane pane = OutputWindowManager.AspireOutputPane;
var options = await General.GetLiveInstanceAsync();

var projectPath = FindAzureYaml(project.FullPath);

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

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

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

if (result.ExitCode != 0)
{
await pane.WriteLineAsync($"[AZD]: Unable to synthesize infrastructure:{stdErr}:{result.ExitCode}");
await pane.WriteLineAsync(OutputWindowManager.GenerateOutputMessage($"Unable to synthesize infrastructure:{stdErr}:{result.ExitCode}", "InfraSynth", LogLevel.Error));
goto Cleanup;
}
else
{
await pane.WriteLineAsync($"[AZD]: infra synth completed");
await pane.WriteLineAsync($"[AZD]: {stdOutBuffer}");
await pane.WriteLineAsync(OutputWindowManager.GenerateOutputMessage($"Infra synth completed", "InfraSynth", LogLevel.Information));
await pane.WriteLineAsync(OutputWindowManager.GenerateOutputMessage($"{stdOutBuffer}", "InfraSynth", LogLevel.Information));
}

await VS.Documents.OpenAsync(Path.Combine(projectPath, "infra", "resources.bicep"));
Expand Down
8 changes: 5 additions & 3 deletions src/Commands/ManifestGen.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using AspireManifestGen.Options;
using CliWrap;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
Expand All @@ -22,7 +23,7 @@ protected override async Task ExecuteAsync(OleMenuCmdEventArgs e, Project projec
{
var stdOutBuffer = new StringBuilder();
var stdErrBuffer = new StringBuilder();
OutputWindowPane pane = await VS.Windows.GetOutputWindowPaneAsync(Community.VisualStudio.Toolkit.Windows.VSOutputWindowPane.General);
OutputWindowPane pane = OutputWindowManager.AspireOutputPane;

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

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

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

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

await VS.Documents.OpenAsync(manifestPath);
Expand Down
12 changes: 12 additions & 0 deletions src/OutputWindowManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Microsoft.Extensions.Logging;

public static class OutputWindowManager
{
public static OutputWindowPane AspireOutputPane { get; set; }

public static string GenerateOutputMessage(string message, string source, LogLevel logLevel)
{
var timestamp = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss.fffZ");
return $"[{logLevel.ToString().ToUpperInvariant()}] [{source.ToUpperInvariant()}] [{timestamp}]: {message}";
}
}

0 comments on commit 6ab6787

Please sign in to comment.