Added Serilog

- Configuration is defined in appsettings.json
- Uses console and file sinks
This commit is contained in:
Eero Holmala 2024-03-12 14:18:18 +02:00
parent ed94081826
commit 7df4ab72b8
6 changed files with 29 additions and 5 deletions

View File

@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.AspNetCore.Mvc.RazorPages;
using Serilog;
namespace WretchedMachines.Pages; namespace WretchedMachines.Pages;
@ -14,6 +15,6 @@ public class IndexModel : PageModel
public void OnGet() public void OnGet()
{ {
Log.Information("Test");
} }
} }

View File

@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.AspNetCore.Mvc.RazorPages;
using Serilog;
namespace WretchedMachines.Pages; namespace WretchedMachines.Pages;
@ -14,6 +15,7 @@ public class PrivacyModel : PageModel
public void OnGet() public void OnGet()
{ {
Log.Information("Test");
} }
} }

View File

@ -1,10 +1,18 @@
using Serilog;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(builder.Configuration)
.CreateLogger();
Log.Information("Starting");
builder.Host.UseSerilog();
// Add services to the container. // Add services to the container.
builder.Services.AddRazorPages(); builder.Services.AddRazorPages();
// builder.Host.UseSerilog();
var app = builder.Build(); var app = builder.Build();
// Configure the HTTP request pipeline. // Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment()) if (!app.Environment.IsDevelopment())
{ {
@ -13,6 +21,7 @@ if (!app.Environment.IsDevelopment())
app.UseHsts(); app.UseHsts();
} }
app.UseSerilogRequestLogging();
app.UseHttpsRedirection(); app.UseHttpsRedirection();
app.UseStaticFiles(); app.UseStaticFiles();

View File

@ -9,6 +9,8 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Markdig" Version="0.35.0" /> <PackageReference Include="Markdig" Version="0.35.0" />
<PackageReference Include="Serilog" Version="3.1.1" /> <PackageReference Include="Serilog" Version="3.1.1" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.1" />
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" /> <PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
</ItemGroup> </ItemGroup>

View File

@ -2,7 +2,7 @@
"DetailedErrors": true, "DetailedErrors": true,
"Logging": { "Logging": {
"LogLevel": { "LogLevel": {
"Default": "Information", "Default": "Debug",
"Microsoft.AspNetCore": "Warning" "Microsoft.AspNetCore": "Warning"
} }
} }

View File

@ -5,5 +5,15 @@
"Microsoft.AspNetCore": "Warning" "Microsoft.AspNetCore": "Warning"
} }
}, },
"AllowedHosts": "*" "AllowedHosts": "*",
"ConnectionStrings": {
"Default": ""
},
"Serilog": {
"Using": ["Serilog.Sinks.Console", "Serilog.Sinks.File"],
"WriteTo": [
{"Name": "Console"},
{ "Name": "File", "Args": { "path": "logs/log-.txt", "rollingInterval": "Day" } }
]
}
} }