Skip to content

Commit 9a0b0c5

Browse files
committed
Updated startup
1 parent 7d7d51c commit 9a0b0c5

4 files changed

Lines changed: 87 additions & 61 deletions

File tree

AspNetCoreServiceBusApi2/AspNetCoreServiceBusApi2.csproj

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,18 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="Serilog.Extensions.Hosting" Version="8.0.0" />
12-
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
1311
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
1412
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.6" />
1513
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.6">
1614
<PrivateAssets>all</PrivateAssets>
1715
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1816
</PackageReference>
17+
18+
<PackageReference Include="Serilog.AspNetCore" Version="8.0.1" />
19+
<PackageReference Include="Serilog.Enrichers.Environment" Version="3.0.1" />
20+
<PackageReference Include="Serilog.Enrichers.Thread" Version="4.0.0" />
21+
<PackageReference Include="Serilog.Sinks.Async" Version="2.0.0" />
22+
<PackageReference Include="Serilog.Sinks.ApplicationInsights" Version="4.0.0" />
1923
</ItemGroup>
2024

2125
<ItemGroup>
Lines changed: 32 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,32 @@
1-
using Serilog;
2-
using Serilog.Events;
3-
4-
namespace AspNetCoreServiceBusApi2;
5-
6-
public class Program
7-
{
8-
public static int Main(string[] args)
9-
{
10-
Log.Logger = new LoggerConfiguration()
11-
.MinimumLevel.Information()
12-
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
13-
.Enrich.FromLogContext()
14-
.WriteTo.Console()
15-
.CreateLogger();
16-
17-
try
18-
{
19-
Log.Information("Starting host");
20-
CreateHostBuilder(args).Build().Run();
21-
return 0;
22-
}
23-
catch (Exception ex)
24-
{
25-
Log.Fatal(ex, "Host terminated unexpectedly");
26-
return 1;
27-
}
28-
finally
29-
{
30-
Log.CloseAndFlush();
31-
}
32-
}
33-
34-
public static IHostBuilder CreateHostBuilder(string[] args) =>
35-
Host.CreateDefaultBuilder(args)
36-
.ConfigureWebHostDefaults(webBuilder =>
37-
{
38-
webBuilder.UseStartup<Startup>();
39-
})
40-
.UseSerilog();
41-
}
1+
using AspNetCoreServiceBusApi2;
2+
using Serilog;
3+
4+
Log.Logger = new LoggerConfiguration()
5+
.WriteTo.Console()
6+
.CreateBootstrapLogger();
7+
8+
Log.Information("Starting up OpeniddictServer");
9+
10+
try
11+
{
12+
var builder = WebApplication.CreateBuilder(args);
13+
14+
builder.Host.UseSerilog((context, loggerConfiguration) => loggerConfiguration
15+
.WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level}] {SourceContext}{NewLine}{Message:lj}{NewLine}{Exception}{NewLine}")
16+
.ReadFrom.Configuration(context.Configuration));
17+
18+
var app = builder
19+
.ConfigureServices()
20+
.Configure();
21+
22+
app.Run();
23+
}
24+
catch (Exception ex) when (ex.GetType().Name is not "StopTheHostException" && ex.GetType().Name is not "HostAbortedException")
25+
{
26+
Log.Fatal(ex, "Unhandled exception");
27+
}
28+
finally
29+
{
30+
Log.Information("Shut down complete");
31+
Log.CloseAndFlush();
32+
}

AspNetCoreServiceBusApi2/Startup.cs renamed to AspNetCoreServiceBusApi2/StartupExtensions.cs

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
1-
using AspNetCoreServiceBusApi2.Model;
2-
using Microsoft.EntityFrameworkCore;
1+
32
using Microsoft.OpenApi.Models;
43
using ServiceBusMessaging;
4+
using Serilog;
5+
using AspNetCoreServiceBusApi2.Model;
6+
using Microsoft.EntityFrameworkCore;
57

68
namespace AspNetCoreServiceBusApi2;
79

8-
public class Startup
10+
internal static class StartupExtensions
911
{
10-
public Startup(IConfiguration configuration)
12+
public static WebApplication ConfigureServices(this WebApplicationBuilder builder)
1113
{
12-
Configuration = configuration;
13-
}
14-
15-
public IConfiguration Configuration { get; }
14+
var services = builder.Services;
15+
var configuration = builder.Configuration;
1616

17-
public void ConfigureServices(IServiceCollection services)
18-
{
1917
services.AddControllers();
2018

21-
var connection = Configuration.GetConnectionString("DefaultConnection");
19+
var connection = configuration.GetConnectionString("DefaultConnection");
2220

2321
services.AddDbContext<PayloadContext>(options =>
2422
options.UseSqlite(connection));
@@ -37,11 +35,15 @@ public void ConfigureServices(IServiceCollection services)
3735
Title = "Payload API",
3836
});
3937
});
38+
39+
return builder.Build();
4040
}
4141

42-
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
42+
public static WebApplication Configure(this WebApplication app)
4343
{
44-
if (env.IsDevelopment())
44+
app.UseSerilogRequestLogging();
45+
46+
if (app.Environment.IsDevelopment())
4547
{
4648
app.UseDeveloperExceptionPage();
4749
}
@@ -58,15 +60,14 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
5860
app.UseAuthorization();
5961
app.UseCors();
6062

61-
app.UseEndpoints(endpoints =>
62-
{
63-
endpoints.MapControllers();
64-
});
63+
app.MapControllers();
6564

6665
app.UseSwagger();
6766
app.UseSwaggerUI(c =>
6867
{
6968
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Payload Management API V1");
7069
});
70+
71+
return app;
7172
}
72-
}
73+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,35 @@
11
{
22
"ConnectionStrings": {
33
"DefaultConnection": "Data Source=payloads.sqlite"
4+
},
5+
"Serilog": {
6+
"MinimumLevel": {
7+
"Default": "Debug",
8+
"Override": {
9+
"Microsoft": "Debug",
10+
"Microsoft.Hosting.Lifetime": "Debug",
11+
"Microsoft.AspNetCore.Authentication": "Debug",
12+
"System": "Debug"
13+
}
14+
},
15+
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ],
16+
"WriteTo": [
17+
{
18+
"Name": "ApplicationInsights",
19+
"Args": {
20+
"telemetryConverter": "Serilog.Sinks.ApplicationInsights.TelemetryConverters.TraceTelemetryConverter, Serilog.Sinks.ApplicationInsights"
21+
}
22+
},
23+
{
24+
"Name": "File",
25+
"Args": {
26+
"path": "../_logs-AspNetCoreServiceBusApi2.txt",
27+
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] [{SourceContext}] [{EventId}] {Message}{NewLine}{Exception}",
28+
"rollOnFileSizeLimit": true,
29+
"fileSizeLimitBytes": 4194304,
30+
"retainedFileCountLimit": 5
31+
}
32+
}
33+
]
434
}
535
}

0 commit comments

Comments
 (0)