Skip to content

Commit 108d88a

Browse files
committed
Use High Performance Logging
Add Aspire demo project
1 parent 65c16b0 commit 108d88a

File tree

205 files changed

+94436
-1717
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

205 files changed

+94436
-1717
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project>
2+
<!-- See https://aka.ms/dotnet/msbuild/customize for more details on customizing your build -->
3+
<PropertyGroup>
4+
5+
<LangVersion>latest</LangVersion>
6+
7+
</PropertyGroup>
8+
</Project>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
var builder = DistributedApplication.CreateBuilder(args);
2+
3+
builder.AddProject<Projects.WebRTCAspire_Web>("webfrontend")
4+
//.WithEnvironment("Logging__LogLevel__Default", "Trace")
5+
.WithExternalHttpEndpoints();
6+
7+
builder.Build().Run();
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"$schema": "https://json.schemastore.org/launchsettings.json",
3+
"profiles": {
4+
"https": {
5+
"commandName": "Project",
6+
"dotnetRunMessages": true,
7+
"launchBrowser": true,
8+
"applicationUrl": "https://localhost:17217;http://localhost:15014",
9+
"environmentVariables": {
10+
"ASPNETCORE_ENVIRONMENT": "Development",
11+
"DOTNET_ENVIRONMENT": "Development",
12+
"DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21105",
13+
"DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22267"
14+
}
15+
},
16+
"http": {
17+
"commandName": "Project",
18+
"dotnetRunMessages": true,
19+
"launchBrowser": true,
20+
"applicationUrl": "http://localhost:15014",
21+
"environmentVariables": {
22+
"ASPNETCORE_ENVIRONMENT": "Development",
23+
"DOTNET_ENVIRONMENT": "Development",
24+
"ASPIRE_ALLOW_UNSECURED_TRANSPORT": "true",
25+
"DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:19196",
26+
"DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:20216"
27+
}
28+
}
29+
}
30+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<Sdk Name="Aspire.AppHost.Sdk" Version="9.1.0" />
4+
5+
<PropertyGroup>
6+
<OutputType>Exe</OutputType>
7+
<TargetFramework>net8.0</TargetFramework>
8+
<ImplicitUsings>enable</ImplicitUsings>
9+
<Nullable>enable</Nullable>
10+
<IsAspireHost>true</IsAspireHost>
11+
<UserSecretsId>df4d7e22-716b-43ca-a898-c5d18441f114</UserSecretsId>
12+
</PropertyGroup>
13+
14+
<ItemGroup>
15+
<ProjectReference Include="..\WebRTCAspire.Web\WebRTCAspire.Web.csproj" />
16+
</ItemGroup>
17+
18+
<ItemGroup>
19+
<PackageReference Include="Aspire.Hosting.AppHost" Version="9.1.0" />
20+
</ItemGroup>
21+
22+
</Project>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
}
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning",
6+
"Aspire.Hosting.Dcp": "Warning"
7+
}
8+
}
9+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
using Microsoft.AspNetCore.Builder;
2+
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
3+
using Microsoft.Extensions.DependencyInjection;
4+
using Microsoft.Extensions.Diagnostics.HealthChecks;
5+
using Microsoft.Extensions.Logging;
6+
using OpenTelemetry;
7+
using OpenTelemetry.Metrics;
8+
using OpenTelemetry.Trace;
9+
10+
namespace Microsoft.Extensions.Hosting;
11+
12+
// Adds common .NET Aspire services: service discovery, resilience, health checks, and OpenTelemetry.
13+
// This project should be referenced by each service project in your solution.
14+
// To learn more about using this project, see https://aka.ms/dotnet/aspire/service-defaults
15+
public static class Extensions
16+
{
17+
public static IHostApplicationBuilder AddServiceDefaults(this IHostApplicationBuilder builder)
18+
{
19+
builder.ConfigureOpenTelemetry();
20+
21+
builder.AddDefaultHealthChecks();
22+
23+
builder.Services.AddServiceDiscovery();
24+
25+
builder.Services.ConfigureHttpClientDefaults(http =>
26+
{
27+
// Turn on resilience by default
28+
http.AddStandardResilienceHandler();
29+
30+
// Turn on service discovery by default
31+
http.AddServiceDiscovery();
32+
});
33+
34+
return builder;
35+
}
36+
37+
public static IHostApplicationBuilder ConfigureOpenTelemetry(this IHostApplicationBuilder builder)
38+
{
39+
builder.Logging.AddOpenTelemetry(logging =>
40+
{
41+
logging.IncludeFormattedMessage = true;
42+
logging.IncludeScopes = true;
43+
});
44+
45+
builder.Services.AddOpenTelemetry()
46+
.WithMetrics(metrics =>
47+
{
48+
metrics.AddAspNetCoreInstrumentation()
49+
.AddHttpClientInstrumentation()
50+
.AddRuntimeInstrumentation();
51+
})
52+
.WithTracing(tracing =>
53+
{
54+
tracing.AddAspNetCoreInstrumentation()
55+
// Uncomment the following line to enable gRPC instrumentation (requires the OpenTelemetry.Instrumentation.GrpcNetClient package)
56+
//.AddGrpcClientInstrumentation()
57+
.AddHttpClientInstrumentation();
58+
});
59+
60+
builder.AddOpenTelemetryExporters();
61+
62+
return builder;
63+
}
64+
65+
private static IHostApplicationBuilder AddOpenTelemetryExporters(this IHostApplicationBuilder builder)
66+
{
67+
var useOtlpExporter = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]);
68+
69+
if (useOtlpExporter)
70+
{
71+
builder.Services.AddOpenTelemetry().UseOtlpExporter();
72+
}
73+
74+
// Uncomment the following lines to enable the Azure Monitor exporter (requires the Azure.Monitor.OpenTelemetry.AspNetCore package)
75+
//if (!string.IsNullOrEmpty(builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"]))
76+
//{
77+
// builder.Services.AddOpenTelemetry()
78+
// .UseAzureMonitor();
79+
//}
80+
81+
return builder;
82+
}
83+
84+
public static IHostApplicationBuilder AddDefaultHealthChecks(this IHostApplicationBuilder builder)
85+
{
86+
builder.Services.AddHealthChecks()
87+
// Add a default liveness check to ensure app is responsive
88+
.AddCheck("self", () => HealthCheckResult.Healthy(), ["live"]);
89+
90+
return builder;
91+
}
92+
93+
public static WebApplication MapDefaultEndpoints(this WebApplication app)
94+
{
95+
// Adding health checks endpoints to applications in non-development environments has security implications.
96+
// See https://aka.ms/dotnet/aspire/healthchecks for details before enabling these endpoints in non-development environments.
97+
if (app.Environment.IsDevelopment())
98+
{
99+
// All health checks must pass for app to be considered ready to accept traffic after starting
100+
app.MapHealthChecks("/health");
101+
102+
// Only health checks tagged with the "live" tag must pass for app to be considered alive
103+
app.MapHealthChecks("/alive", new HealthCheckOptions
104+
{
105+
Predicate = r => r.Tags.Contains("live")
106+
});
107+
}
108+
109+
return app;
110+
}
111+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
<IsAspireSharedProject>true</IsAspireSharedProject>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<FrameworkReference Include="Microsoft.AspNetCore.App" />
12+
13+
<PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="9.2.0" />
14+
<PackageReference Include="Microsoft.Extensions.ServiceDiscovery" Version="9.1.0" />
15+
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.11.1" />
16+
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.11.1" />
17+
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.11.0" />
18+
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.11.0" />
19+
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.11.0" />
20+
</ItemGroup>
21+
22+
</Project>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
@page
2+
@model ErrorModel
3+
@{
4+
ViewData["Title"] = "Error";
5+
}
6+
7+
<h1 class="text-danger">Error.</h1>
8+
<h2 class="text-danger">An error occurred while processing your request.</h2>
9+
10+
@if (Model.ShowRequestId)
11+
{
12+
<p>
13+
<strong>Request ID:</strong> <code>@Model.RequestId</code>
14+
</p>
15+
}
16+
17+
<h3>Development Mode</h3>
18+
<p>
19+
Swapping to the <strong>Development</strong> environment displays detailed information about the error that occurred.
20+
</p>
21+
<p>
22+
<strong>The Development environment shouldn't be enabled for deployed applications.</strong>
23+
It can result in displaying sensitive information from exceptions to end users.
24+
For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong>
25+
and restarting the app.
26+
</p>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System.Diagnostics;
2+
using Microsoft.AspNetCore.Mvc;
3+
using Microsoft.AspNetCore.Mvc.RazorPages;
4+
5+
namespace WebRTCAspire.Web.Pages;
6+
7+
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
8+
[IgnoreAntiforgeryToken]
9+
public class ErrorModel : PageModel
10+
{
11+
public string? RequestId { get; set; }
12+
13+
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
14+
15+
private readonly ILogger<ErrorModel> _logger;
16+
17+
public ErrorModel(ILogger<ErrorModel> logger)
18+
{
19+
_logger = logger;
20+
}
21+
22+
public void OnGet()
23+
{
24+
RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier;
25+
}
26+
}
27+

0 commit comments

Comments
 (0)