Skip to content

Commit

Permalink
add program
Browse files Browse the repository at this point in the history
  • Loading branch information
scottsauber committed Nov 18, 2024
1 parent f1fca13 commit 189caeb
Showing 1 changed file with 67 additions and 18 deletions.
85 changes: 67 additions & 18 deletions src/WorkshopDemo/Program.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,78 @@
using Azure.Identity;
using Microsoft.ApplicationInsights.Extensibility;
using Serilog;
using Serilog.Events;
using Serilog.Sinks.SystemConsole.Themes;
using WorkshopDemo.Core.Common;
using WorkshopDemo.HealthChecks;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddOpenApi();
builder.Services.AddControllers();
builder.Services.AddHealthChecks()
.AddCheck<WorkshopDemoHealthCheck>(nameof(WorkshopDemoHealthCheck));
builder.Services.AddSingleton<IFileService, FileService>();
builder.Services.AddSingleton<IVersionService, VersionService>();
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.Enrich.FromLogContext()
.WriteTo.Console()
.CreateBootstrapLogger();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
try
{
app.MapOpenApi();
}
Log.Information("Starting web host");

// Add services to the container.
builder.Host.UseSerilog((ctx, lc) => lc
.MinimumLevel.Information()
.MinimumLevel.Override("WorkshopDemo", LogEventLevel.Debug)
.MinimumLevel.Override("System", LogEventLevel.Error)
.WriteTo.Console(
outputTemplate:
"[{Timestamp:HH:mm:ss} {Level}] {SourceContext}{NewLine}{Message:lj}{NewLine}{Exception}{NewLine}",
theme: AnsiConsoleTheme.Literate)
.WriteTo.ApplicationInsights(
new TelemetryConfiguration
{ ConnectionString = ctx.Configuration.GetConnectionString("ApplicationInsights") },
TelemetryConverter.Traces)
.Enrich.FromLogContext());

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddOpenApi();
builder.Services.AddControllers();
builder.Services.AddHealthChecks()
.AddCheck<WorkshopDemoHealthCheck>(nameof(WorkshopDemoHealthCheck));
builder.Services.AddSingleton<IFileService, FileService>();
builder.Services.AddSingleton<IVersionService, VersionService>();

builder.Configuration.AddAzureKeyVault(
new Uri($"https://kv-scottsauber-{builder.Environment.EnvironmentName}.vault.azure.net/"),
new ChainedTokenCredential(new AzureCliCredential(), new ManagedIdentityCredential()));

builder.Services.AddApplicationInsightsTelemetry(options =>
{
options.ConnectionString = builder.Configuration.GetConnectionString("ApplicationInsights");
});

var app = builder.Build();

app.UseSerilogRequestLogging();

app.UseHttpsRedirection();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
}

app.MapHealthChecks("/api/healthz");
app.UseHttpsRedirection();

app.MapControllers();
app.MapHealthChecks("/api/healthz");

app.Run();
app.MapControllers();

app.Run();
}
catch (Exception ex)
{
Log.Fatal(ex, "Application terminated");
}
finally
{
Log.CloseAndFlush();
}

0 comments on commit 189caeb

Please sign in to comment.