-
Notifications
You must be signed in to change notification settings - Fork 324
Add Serilog example #107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
TGrannen
wants to merge
3
commits into
dotnet:main
Choose a base branch
from
TGrannen:tgrannen/aspire-serilog-example
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add Serilog example #107
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
samples/AspireWithSerilog/AspireWithSerilog.ApiService/AspireWithSerilog.ApiService.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\AspireWithSerilog.ServiceDefaults\AspireWithSerilog.ServiceDefaults.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
43 changes: 43 additions & 0 deletions
43
samples/AspireWithSerilog/AspireWithSerilog.ApiService/Program.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| using Serilog; | ||
|
|
||
| var builder = WebApplication.CreateBuilder(args); | ||
|
|
||
| // Add service defaults & Aspire components. | ||
| builder.AddServiceDefaults(); | ||
|
|
||
| // Add services to the container. | ||
| builder.Services.AddProblemDetails(); | ||
|
|
||
| var app = builder.Build(); | ||
|
|
||
| // Configure the HTTP request pipeline. | ||
| app.UseExceptionHandler(); | ||
|
|
||
| app.UseSerilogRequestLogging(); | ||
|
|
||
| var summaries = new[] | ||
| { | ||
| "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" | ||
| }; | ||
|
|
||
| app.MapGet("/weatherforecast", () => | ||
| { | ||
| var forecast = Enumerable.Range(1, 5).Select(index => | ||
| new WeatherForecast | ||
| ( | ||
| DateOnly.FromDateTime(DateTime.Now.AddDays(index)), | ||
| Random.Shared.Next(-20, 55), | ||
| summaries[Random.Shared.Next(summaries.Length)] | ||
| )) | ||
| .ToArray(); | ||
| return forecast; | ||
| }); | ||
|
|
||
| app.MapDefaultEndpoints(); | ||
|
|
||
| app.Run(); | ||
|
|
||
| record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary) | ||
| { | ||
| public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); | ||
| } |
15 changes: 15 additions & 0 deletions
15
samples/AspireWithSerilog/AspireWithSerilog.ApiService/Properties/launchSettings.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| { | ||
| "$schema": "http://json.schemastore.org/launchsettings.json", | ||
| "profiles": { | ||
| "http": { | ||
| "commandName": "Project", | ||
| "dotnetRunMessages": true, | ||
| "launchBrowser": true, | ||
| "launchUrl": "weatherforecast", | ||
| "applicationUrl": "http://localhost:5387", | ||
| "environmentVariables": { | ||
| "ASPNETCORE_ENVIRONMENT": "Development" | ||
| } | ||
| } | ||
| } | ||
| } |
8 changes: 8 additions & 0 deletions
8
samples/AspireWithSerilog/AspireWithSerilog.ApiService/appsettings.Development.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "Logging": { | ||
| "LogLevel": { | ||
| "Default": "Information", | ||
| "Microsoft.AspNetCore": "Warning" | ||
| } | ||
| } | ||
| } |
20 changes: 20 additions & 0 deletions
20
samples/AspireWithSerilog/AspireWithSerilog.ApiService/appsettings.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| { | ||
| "Logging": { | ||
| "LogLevel": { | ||
| "Default": "Information", | ||
| "Microsoft.AspNetCore": "Warning" | ||
| } | ||
| }, | ||
| "Serilog": { | ||
| "MinimumLevel": { | ||
| "Default": "Information", | ||
| "Override": { | ||
| "Microsoft.AspNetCore": "Warning" | ||
| } | ||
| }, | ||
| "Enrich": [ | ||
| "FromLogContext" | ||
| ] | ||
| }, | ||
| "AllowedHosts": "*" | ||
| } |
20 changes: 20 additions & 0 deletions
20
samples/AspireWithSerilog/AspireWithSerilog.AppHost/AspireWithSerilog.AppHost.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| <IsAspireHost>true</IsAspireHost> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\AspireWithSerilog.ApiService\AspireWithSerilog.ApiService.csproj" /> | ||
| <ProjectReference Include="..\AspireWithSerilog.Web\AspireWithSerilog.Web.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Aspire.Hosting" Version="8.0.0-preview.2.23619.3" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
22 changes: 22 additions & 0 deletions
22
samples/AspireWithSerilog/AspireWithSerilog.AppHost/Program.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| using Serilog; | ||
| using Serilog.Events; | ||
|
|
||
| var builder = DistributedApplication.CreateBuilder(args); | ||
|
|
||
| builder.Services.AddSerilog((_, loggerConfiguration) => | ||
| { | ||
| // Configure Serilog as desired here for AppHost logs (or use IConfiguration) | ||
| loggerConfiguration | ||
| .ReadFrom.Configuration(builder.Configuration) | ||
| .MinimumLevel.Information() | ||
| .MinimumLevel.Override("Aspire.Hosting.Dcp", LogEventLevel.Warning) | ||
| .Enrich.FromLogContext() | ||
| .WriteTo.Console(); | ||
| }); | ||
|
|
||
| var apiService = builder.AddProject<Projects.AspireWithSerilog_ApiService>("apiservice"); | ||
|
|
||
| builder.AddProject<Projects.AspireWithSerilog_Web>("webfrontend") | ||
| .WithReference(apiService); | ||
|
|
||
| builder.Build().Run(); | ||
16 changes: 16 additions & 0 deletions
16
samples/AspireWithSerilog/AspireWithSerilog.AppHost/Properties/launchSettings.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| { | ||
| "$schema": "http://json.schemastore.org/launchsettings.json", | ||
| "profiles": { | ||
| "http": { | ||
| "commandName": "Project", | ||
| "dotnetRunMessages": true, | ||
| "launchBrowser": true, | ||
| "applicationUrl": "http://localhost:15118", | ||
| "environmentVariables": { | ||
| "ASPNETCORE_ENVIRONMENT": "Development", | ||
| "DOTNET_ENVIRONMENT": "Development", | ||
| "DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:16257" | ||
| } | ||
| } | ||
| } | ||
| } |
8 changes: 8 additions & 0 deletions
8
samples/AspireWithSerilog/AspireWithSerilog.AppHost/appsettings.Development.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "Logging": { | ||
| "LogLevel": { | ||
| "Default": "Information", | ||
| "Microsoft.AspNetCore": "Warning" | ||
| } | ||
| } | ||
| } |
21 changes: 21 additions & 0 deletions
21
samples/AspireWithSerilog/AspireWithSerilog.AppHost/appsettings.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| { | ||
| "Logging": { | ||
| "LogLevel": { | ||
| "Default": "Information", | ||
| "Microsoft.AspNetCore": "Warning", | ||
| "Aspire.Hosting.Dcp": "Warning" | ||
| } | ||
| }, | ||
| "Serilog": { | ||
| "MinimumLevel": { | ||
| "Default": "Information", | ||
| "Override": { | ||
| "Microsoft.AspNetCore": "Warning", | ||
| "Aspire.Hosting.Dcp": "Warning" | ||
| } | ||
| }, | ||
| "Enrich": [ | ||
| "FromLogContext" | ||
| ] | ||
| } | ||
| } |
25 changes: 25 additions & 0 deletions
25
...ireWithSerilog/AspireWithSerilog.ServiceDefaults/AspireWithSerilog.ServiceDefaults.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Library</OutputType> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| <IsAspireSharedProject>true</IsAspireSharedProject> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <FrameworkReference Include="Microsoft.AspNetCore.App" /> | ||
|
|
||
| <PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="8.0.0" /> | ||
| <PackageReference Include="Microsoft.Extensions.ServiceDiscovery" Version="8.0.0-preview.2.23619.3" /> | ||
| <PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.7.0-alpha.1" /> | ||
| <PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.7.0-alpha.1" /> | ||
| <PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.6.0-beta.2" /> | ||
| <PackageReference Include="OpenTelemetry.Instrumentation.GrpcNetClient" Version="1.6.0-beta.2" /> | ||
| <PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.6.0-beta.2" /> | ||
| <PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.5.1" /> | ||
| <PackageReference Include="Serilog.AspNetCore" Version="8.0.1"/> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
121 changes: 121 additions & 0 deletions
121
samples/AspireWithSerilog/AspireWithSerilog.ServiceDefaults/Extensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| using Microsoft.AspNetCore.Builder; | ||
| using Microsoft.AspNetCore.Diagnostics.HealthChecks; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
| using Microsoft.Extensions.Logging; | ||
| using OpenTelemetry.Logs; | ||
| using OpenTelemetry.Metrics; | ||
| using OpenTelemetry.Trace; | ||
|
|
||
| namespace Microsoft.Extensions.Hosting; | ||
|
|
||
| public static class Extensions | ||
| { | ||
| public static IHostApplicationBuilder AddServiceDefaults(this IHostApplicationBuilder builder) | ||
| { | ||
| builder.ConfigureSerilog(); | ||
|
|
||
| builder.ConfigureOpenTelemetry(); | ||
|
|
||
| builder.AddDefaultHealthChecks(); | ||
|
|
||
| builder.Services.AddServiceDiscovery(); | ||
|
|
||
| builder.Services.ConfigureHttpClientDefaults(http => | ||
| { | ||
| // Turn on resilience by default | ||
| http.AddStandardResilienceHandler(); | ||
|
|
||
| // Turn on service discovery by default | ||
| http.UseServiceDiscovery(); | ||
| }); | ||
|
|
||
| return builder; | ||
| } | ||
|
|
||
| public static IHostApplicationBuilder ConfigureOpenTelemetry(this IHostApplicationBuilder builder) | ||
| { | ||
| builder.Logging.AddOpenTelemetry(logging => | ||
| { | ||
| logging.IncludeFormattedMessage = true; | ||
| logging.IncludeScopes = true; | ||
| }); | ||
|
|
||
| builder.Services.AddOpenTelemetry() | ||
| .WithMetrics(metrics => | ||
| { | ||
| metrics.AddRuntimeInstrumentation() | ||
| .AddBuiltInMeters(); | ||
| }) | ||
| .WithTracing(tracing => | ||
| { | ||
| if (builder.Environment.IsDevelopment()) | ||
| { | ||
| // We want to view all traces in development | ||
| tracing.SetSampler(new AlwaysOnSampler()); | ||
| } | ||
|
|
||
| tracing.AddAspNetCoreInstrumentation() | ||
| .AddGrpcClientInstrumentation() | ||
| .AddHttpClientInstrumentation(); | ||
| }); | ||
|
|
||
| builder.AddOpenTelemetryExporters(); | ||
|
|
||
| return builder; | ||
| } | ||
|
|
||
| private static IHostApplicationBuilder AddOpenTelemetryExporters(this IHostApplicationBuilder builder) | ||
| { | ||
| var useOtlpExporter = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]); | ||
|
|
||
| if (useOtlpExporter) | ||
| { | ||
| builder.Services.Configure<OpenTelemetryLoggerOptions>(logging => logging.AddOtlpExporter()); | ||
| builder.Services.ConfigureOpenTelemetryMeterProvider(metrics => metrics.AddOtlpExporter()); | ||
| builder.Services.ConfigureOpenTelemetryTracerProvider(tracing => tracing.AddOtlpExporter()); | ||
| } | ||
|
|
||
| // Uncomment the following lines to enable the Prometheus exporter (requires the OpenTelemetry.Exporter.Prometheus.AspNetCore package) | ||
| // builder.Services.AddOpenTelemetry() | ||
| // .WithMetrics(metrics => metrics.AddPrometheusExporter()); | ||
|
|
||
| // Uncomment the following lines to enable the Azure Monitor exporter (requires the Azure.Monitor.OpenTelemetry.Exporter package) | ||
| // builder.Services.AddOpenTelemetry() | ||
| // .UseAzureMonitor(); | ||
|
|
||
| return builder; | ||
| } | ||
|
|
||
| public static IHostApplicationBuilder AddDefaultHealthChecks(this IHostApplicationBuilder builder) | ||
| { | ||
| builder.Services.AddHealthChecks() | ||
| // Add a default liveness check to ensure app is responsive | ||
| .AddCheck("self", () => HealthCheckResult.Healthy(), ["live"]); | ||
|
|
||
| return builder; | ||
| } | ||
|
|
||
| public static WebApplication MapDefaultEndpoints(this WebApplication app) | ||
| { | ||
| // Uncomment the following line to enable the Prometheus endpoint (requires the OpenTelemetry.Exporter.Prometheus.AspNetCore package) | ||
| // app.MapPrometheusScrapingEndpoint(); | ||
|
|
||
| // All health checks must pass for app to be considered ready to accept traffic after starting | ||
| app.MapHealthChecks("/health"); | ||
|
|
||
| // Only health checks tagged with the "live" tag must pass for app to be considered alive | ||
| app.MapHealthChecks("/alive", new HealthCheckOptions | ||
| { | ||
| Predicate = r => r.Tags.Contains("live") | ||
| }); | ||
|
|
||
| return app; | ||
| } | ||
|
|
||
| private static MeterProviderBuilder AddBuiltInMeters(this MeterProviderBuilder meterProviderBuilder) => | ||
| meterProviderBuilder.AddMeter( | ||
| "Microsoft.AspNetCore.Hosting", | ||
| "Microsoft.AspNetCore.Server.Kestrel", | ||
| "System.Net.Http"); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Though I've seen 1 person ask. I'm not sure what the point of pushing logs the AppHost logs through serilog.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ya, I also saw it in one of the comments and figured I'd include it. But I'm not really sure why it would be super useful since the AppHost is really just wiring everything up.
I also included this block in the Readme if it makes more sense to just have it as a reference.