Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified docs/core/diagnostics/media/resource-monitoring-output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 15 additions & 12 deletions docs/core/diagnostics/snippets/resource-monitoring/Program.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
// <setup>
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.ResourceMonitoring;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Spectre.Console;

var services = new ServiceCollection()
.AddLogging(static builder => builder.AddConsole())
.AddResourceMonitoring();

var provider = services.BuildServiceProvider();
var app = Host.CreateDefaultBuilder()
.ConfigureServices(services =>
{
services.AddLogging(static builder => builder.AddConsole())
.AddResourceMonitoring();
})
.Build();

var monitor = provider.GetRequiredService<IResourceMonitor>();
var monitor = app.Services.GetRequiredService<IResourceMonitor>();
await app.StartAsync();
// </setup>

using var cancellationTokenSource = new CancellationTokenSource();
var token = cancellationTokenSource.Token;
Console.CancelKeyPress += (_, e) =>
{
e.Cancel = true;
cancellationTokenSource.Cancel();
};

// <monitor>
await StartMonitoringAsync(monitor, token);
Expand Down Expand Up @@ -60,11 +69,5 @@ await AnsiConsole.Live(table)
await Task.Delay(window);
}
});

Console.CancelKeyPress += (_, e) =>
{
e.Cancel = true;
cancellationTokenSource.Cancel();
};
}
// </monitor>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
Expand All @@ -11,6 +11,7 @@
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="9.0.6" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.6" />
<PackageReference Include="Microsoft.Extensions.Diagnostics.ResourceMonitoring" Version="9.6.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.6" />
<PackageReference Include="Spectre.Console" Version="0.50.0" />
</ItemGroup>

Expand Down