Skip to content

Commit 6f08374

Browse files
committed
Add serilog
1 parent 8a743ef commit 6f08374

File tree

3 files changed

+100
-31
lines changed

3 files changed

+100
-31
lines changed

JixMinApi/JixMinApi.csproj

+13
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@
1010
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
1111
</PropertyGroup>
1212

13+
<ItemGroup>
14+
<Compile Remove="Logs\**" />
15+
<Content Remove="Logs\**" />
16+
<EmbeddedResource Remove="Logs\**" />
17+
<None Remove="Logs\**" />
18+
</ItemGroup>
19+
1320
<ItemGroup>
1421
<PackageReference Include="FluentValidation" Version="11.9.0" />
1522
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="11.9.0" />
@@ -18,6 +25,12 @@
1825
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.3" />
1926
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.3" />
2027
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.6" />
28+
<PackageReference Include="Serilog" Version="3.1.1" />
29+
<PackageReference Include="Serilog.AspNetCore" Version="8.0.1" />
30+
<PackageReference Include="Serilog.Formatting.Compact" Version="2.0.0" />
31+
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.0" />
32+
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
33+
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
2134
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
2235
</ItemGroup>
2336

JixMinApi/Program.cs

+58-30
Original file line numberDiff line numberDiff line change
@@ -2,51 +2,79 @@
22
using JixMinApi.Features.Todo;
33
using JixMinApi.Shared;
44
using Microsoft.OpenApi.Models;
5+
using Serilog;
6+
using Serilog.Formatting.Compact;
57
using System.Reflection;
68

7-
var builder = WebApplication.CreateBuilder(args);
9+
Log.Logger = new LoggerConfiguration()
10+
.WriteTo.Console()
11+
.WriteTo.File(new CompactJsonFormatter(), "Logs/applog-.txt", rollingInterval: RollingInterval.Day)
12+
.CreateBootstrapLogger();
813

9-
// Add services to the container.
10-
builder.Services.AddEndpointsApiExplorer();
11-
builder.Services.AddSwaggerGen(options =>
14+
Log.Information("App Starting up ===================");
15+
16+
try
1217
{
13-
options.SwaggerDoc("v1", new OpenApiInfo
18+
var builder = WebApplication.CreateBuilder(args);
19+
20+
// Add services to the container.
21+
builder.Services.AddSerilog((services, lc) => lc
22+
.ReadFrom.Configuration(builder.Configuration)
23+
.ReadFrom.Services(services)
24+
.Enrich.FromLogContext());
25+
26+
builder.Services.AddEndpointsApiExplorer();
27+
builder.Services.AddSwaggerGen(options =>
1428
{
15-
Version = "v1",
16-
Title = "JixMinApi Demo",
17-
Description = "A simple minimal API Demo that uses vertical slice architecture.",
29+
options.SwaggerDoc("v1", new OpenApiInfo
30+
{
31+
Version = "v1",
32+
Title = "JixMinApi Demo",
33+
Description = "A simple minimal API Demo that uses vertical slice architecture.",
34+
});
35+
36+
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
37+
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
38+
options.IncludeXmlComments(xmlPath);
1839
});
1940

20-
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
21-
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
22-
options.IncludeXmlComments(xmlPath);
23-
});
41+
builder.Services.AddExceptionHandler<GlobalExceptionHandler>();
42+
builder.Services.AddProblemDetails();
43+
44+
builder.Services.AddValidatorsFromAssembly(typeof(Program).Assembly);
45+
builder.Services.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(typeof(Program).Assembly));
2446

25-
builder.Services.AddExceptionHandler<GlobalExceptionHandler>();
26-
builder.Services.AddProblemDetails();
47+
builder.Services.AddTodoEndpointServices();
48+
builder.Services.AddHealthChecks();
2749

28-
builder.Services.AddValidatorsFromAssembly(typeof(Program).Assembly);
29-
builder.Services.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(typeof(Program).Assembly));
50+
var app = builder.Build();
3051

31-
builder.Services.AddTodoEndpointServices();
32-
builder.Services.AddHealthChecks();
52+
// Configure the HTTP request pipeline.
53+
if (app.Environment.IsDevelopment())
54+
{
55+
app.UseSwagger();
56+
app.UseSwaggerUI();
57+
}
58+
59+
if (!app.Environment.IsDevelopment())
60+
{
61+
app.UseHttpsRedirection();
62+
app.UseExceptionHandler();
63+
}
3364

34-
var app = builder.Build();
65+
app.UseSerilogRequestLogging();
66+
app.UseTodoEndpoints();
67+
app.MapHealthChecks("/healthz");
3568

36-
// Configure the HTTP request pipeline.
37-
if (app.Environment.IsDevelopment())
69+
app.Run();
70+
}
71+
catch (Exception ex)
3872
{
39-
app.UseSwagger();
40-
app.UseSwaggerUI();
73+
Log.Fatal(ex, "App terminated unexpectedly ===================");
4174
}
42-
43-
if (!app.Environment.IsDevelopment())
75+
finally
4476
{
45-
app.UseHttpsRedirection();
46-
app.UseExceptionHandler();
77+
Log.CloseAndFlush();
4778
}
4879

49-
app.UseTodoEndpoints();
50-
app.MapHealthChecks("/healthz");
5180

52-
app.Run();

JixMinApi/appsettings.json

+29-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,33 @@
55
"Microsoft.AspNetCore": "Warning"
66
}
77
},
8-
"AllowedHosts": "*"
8+
"AllowedHosts": "*",
9+
"Serilog": {
10+
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ],
11+
"MinimumLevel": {
12+
"Default": "Information",
13+
"Override": {
14+
"Microsoft": "Warning",
15+
"System": "Warning"
16+
}
17+
},
18+
"WriteTo": [
19+
{
20+
"Name": "Console"
21+
},
22+
{
23+
"Name": "File",
24+
"Args": {
25+
"path": "Logs/applog-.txt",
26+
"rollingInterval": "Day",
27+
"rollOnFileSizeLimit": true,
28+
"formatter": "Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact"
29+
}
30+
}
31+
],
32+
"Enrich": [ "FromLogContext", "WithMachineName", "WithProcessId", "WithThreadId" ],
33+
"Properties": {
34+
"ApplicationName": "JixMinApi"
35+
}
36+
}
937
}

0 commit comments

Comments
 (0)