Skip to content

Commit 7d7d51c

Browse files
committed
Updated startup logging
1 parent ec7378a commit 7d7d51c

5 files changed

Lines changed: 85 additions & 72 deletions

File tree

AspNetCoreServiceBusApi1/AspNetCoreServiceBusApi1.csproj

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@
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" />
12+
13+
<PackageReference Include="Serilog.AspNetCore" Version="8.0.1" />
14+
<PackageReference Include="Serilog.Enrichers.Environment" Version="3.0.1" />
15+
<PackageReference Include="Serilog.Enrichers.Thread" Version="4.0.0" />
16+
<PackageReference Include="Serilog.Sinks.Async" Version="2.0.0" />
17+
<PackageReference Include="Serilog.Sinks.ApplicationInsights" Version="4.0.0" />
1418
</ItemGroup>
1519

1620
<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 AspNetCoreServiceBusApi1;
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 AspNetCoreServiceBusApi1;
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+
}
Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
1+
2+
using Microsoft.AspNetCore.Identity;
13
using Microsoft.OpenApi.Models;
24
using ServiceBusMessaging;
3-
5+
using Serilog;
46
namespace AspNetCoreServiceBusApi1;
57

6-
public class Startup
8+
internal static class StartupExtensions
79
{
8-
public Startup(IConfiguration configuration)
10+
public static WebApplication ConfigureServices(this WebApplicationBuilder builder)
911
{
10-
Configuration = configuration;
11-
}
12+
var services = builder.Services;
13+
var configuration = builder.Configuration;
1214

13-
public IConfiguration Configuration { get; }
14-
15-
public void ConfigureServices(IServiceCollection services)
16-
{
1715
services.AddControllers();
1816

1917
services.AddScoped<ServiceBusSender>();
@@ -27,17 +25,21 @@ public void ConfigureServices(IServiceCollection services)
2725
Title = "Payload View API",
2826
});
2927
});
28+
29+
return builder.Build();
3030
}
3131

32-
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
32+
public static WebApplication Configure(this WebApplication app)
3333
{
34-
if (env.IsDevelopment())
34+
app.UseSerilogRequestLogging();
35+
36+
if (app.Environment!.IsDevelopment())
3537
{
3638
app.UseDeveloperExceptionPage();
3739
}
3840
else
3941
{
40-
app.UseHsts();
42+
app.UseStatusCodePagesWithReExecute("~/error");
4143
}
4244

4345
app.UseStaticFiles();
@@ -48,15 +50,14 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
4850
app.UseAuthorization();
4951
app.UseCors();
5052

51-
app.UseEndpoints(endpoints =>
52-
{
53-
endpoints.MapControllers();
54-
});
53+
app.MapControllers();
5554

5655
app.UseSwagger();
5756
app.UseSwaggerUI(c =>
5857
{
5958
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Payload Management API V1");
6059
});
60+
61+
return app;
6162
}
62-
}
63+
}
Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,2 @@
11
{
2-
"Logging": {
3-
"LogLevel": {
4-
"Default": "Debug",
5-
"System": "Information",
6-
"Microsoft": "Information"
7-
}
8-
}
92
}
Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,33 @@
11
{
2-
"Logging": {
3-
"LogLevel": {
4-
"Default": "Warning",
5-
"Microsoft.Hosting.Lifetime": "Information"
6-
}
2+
"Serilog": {
3+
"MinimumLevel": {
4+
"Default": "Debug",
5+
"Override": {
6+
"Microsoft": "Debug",
7+
"Microsoft.Hosting.Lifetime": "Debug",
8+
"Microsoft.AspNetCore.Authentication": "Debug",
9+
"System": "Debug"
10+
}
11+
},
12+
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ],
13+
"WriteTo": [
14+
{
15+
"Name": "ApplicationInsights",
16+
"Args": {
17+
"telemetryConverter": "Serilog.Sinks.ApplicationInsights.TelemetryConverters.TraceTelemetryConverter, Serilog.Sinks.ApplicationInsights"
18+
}
19+
},
20+
{
21+
"Name": "File",
22+
"Args": {
23+
"path": "../_logs-AspNetCoreServiceBusApi1.txt",
24+
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] [{SourceContext}] [{EventId}] {Message}{NewLine}{Exception}",
25+
"rollOnFileSizeLimit": true,
26+
"fileSizeLimitBytes": 4194304,
27+
"retainedFileCountLimit": 5
28+
}
29+
}
30+
]
731
},
832
"AllowedHosts": "*"
933
}

0 commit comments

Comments
 (0)