Skip to content

Commit

Permalink
Add serilog
Browse files Browse the repository at this point in the history
  • Loading branch information
jicking committed Apr 19, 2024
1 parent 8a743ef commit 6f08374
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 31 deletions.
13 changes: 13 additions & 0 deletions JixMinApi/JixMinApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>

<ItemGroup>
<Compile Remove="Logs\**" />
<Content Remove="Logs\**" />
<EmbeddedResource Remove="Logs\**" />
<None Remove="Logs\**" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="FluentValidation" Version="11.9.0" />
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="11.9.0" />
Expand All @@ -18,6 +25,12 @@
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.3" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.6" />
<PackageReference Include="Serilog" Version="3.1.1" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.1" />
<PackageReference Include="Serilog.Formatting.Compact" Version="2.0.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>

Expand Down
88 changes: 58 additions & 30 deletions JixMinApi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,79 @@
using JixMinApi.Features.Todo;
using JixMinApi.Shared;
using Microsoft.OpenApi.Models;
using Serilog;
using Serilog.Formatting.Compact;
using System.Reflection;

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

// Add services to the container.
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(options =>
Log.Information("App Starting up ===================");

try
{
options.SwaggerDoc("v1", new OpenApiInfo
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddSerilog((services, lc) => lc
.ReadFrom.Configuration(builder.Configuration)
.ReadFrom.Services(services)
.Enrich.FromLogContext());

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(options =>
{
Version = "v1",
Title = "JixMinApi Demo",
Description = "A simple minimal API Demo that uses vertical slice architecture.",
options.SwaggerDoc("v1", new OpenApiInfo
{
Version = "v1",
Title = "JixMinApi Demo",
Description = "A simple minimal API Demo that uses vertical slice architecture.",
});

var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
options.IncludeXmlComments(xmlPath);
});

var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
options.IncludeXmlComments(xmlPath);
});
builder.Services.AddExceptionHandler<GlobalExceptionHandler>();
builder.Services.AddProblemDetails();

builder.Services.AddValidatorsFromAssembly(typeof(Program).Assembly);
builder.Services.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(typeof(Program).Assembly));

builder.Services.AddExceptionHandler<GlobalExceptionHandler>();
builder.Services.AddProblemDetails();
builder.Services.AddTodoEndpointServices();
builder.Services.AddHealthChecks();

builder.Services.AddValidatorsFromAssembly(typeof(Program).Assembly);
builder.Services.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(typeof(Program).Assembly));
var app = builder.Build();

builder.Services.AddTodoEndpointServices();
builder.Services.AddHealthChecks();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

if (!app.Environment.IsDevelopment())
{
app.UseHttpsRedirection();
app.UseExceptionHandler();
}

var app = builder.Build();
app.UseSerilogRequestLogging();
app.UseTodoEndpoints();
app.MapHealthChecks("/healthz");

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
app.Run();
}
catch (Exception ex)
{
app.UseSwagger();
app.UseSwaggerUI();
Log.Fatal(ex, "App terminated unexpectedly ===================");
}

if (!app.Environment.IsDevelopment())
finally
{
app.UseHttpsRedirection();
app.UseExceptionHandler();
Log.CloseAndFlush();
}

app.UseTodoEndpoints();
app.MapHealthChecks("/healthz");

app.Run();
30 changes: 29 additions & 1 deletion JixMinApi/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,33 @@
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"Serilog": {
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ],
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"System": "Warning"
}
},
"WriteTo": [
{
"Name": "Console"
},
{
"Name": "File",
"Args": {
"path": "Logs/applog-.txt",
"rollingInterval": "Day",
"rollOnFileSizeLimit": true,
"formatter": "Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact"
}
}
],
"Enrich": [ "FromLogContext", "WithMachineName", "WithProcessId", "WithThreadId" ],
"Properties": {
"ApplicationName": "JixMinApi"
}
}
}

0 comments on commit 6f08374

Please sign in to comment.