SHA256
Added Serilog for improved logs
- Logs will be shown by console and stored in files.
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
namespace Somnia.GuestLog;
|
||||
|
||||
public static class AppSettings
|
||||
{
|
||||
public const string AppName = "Somnia-GuestLog-dev";
|
||||
|
||||
public const string AppVersion = "v0.0.0-dev"; //2026-09-08
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"Serilog": {
|
||||
"Using": [
|
||||
"Serilog.Sinks.Console",
|
||||
"Serilog.Sinks.File"
|
||||
],
|
||||
"MinimumLevel": {
|
||||
"Default": "Information",
|
||||
"Override": {
|
||||
"Microsoft": "Warning",
|
||||
"Microsoft.EntityFrameworkCore.Database.Command": "Warning"
|
||||
}
|
||||
},
|
||||
"Enrich": [
|
||||
"FromLogContext",
|
||||
"WithThreadId",
|
||||
"WithProcessId"
|
||||
],
|
||||
"Properties": {
|
||||
"ApplicationName": "Somnia-GuestLog"
|
||||
},
|
||||
"WriteTo": [
|
||||
{
|
||||
"Name": "Console",
|
||||
"Args": {
|
||||
"restrictedToMinimumLevel": "Information"
|
||||
}
|
||||
},
|
||||
{
|
||||
"Name": "File",
|
||||
"Args": {
|
||||
// Logs data path
|
||||
"path": "/SomniaGuestLog/logs/log_.log",
|
||||
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{ProcessId}-{ThreadId}] [{Level}] {JobKey}{Message}{NewLine}{Exception}",
|
||||
"rollingInterval": "Day",
|
||||
"rollOnFileSizeLimit": true,
|
||||
"fileSizeLimitBytes": 1073741824,
|
||||
"retainedFileCountLimit": 30,
|
||||
"restrictedToMinimumLevel": "Information"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,9 @@ using Microsoft.AspNetCore.Localization;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Diagnostics;
|
||||
using MudBlazor.Services;
|
||||
using Serilog;
|
||||
using Serilog.Extensions.Logging;
|
||||
using Somnia.GuestLog;
|
||||
using Somnia.GuestLog.Components;
|
||||
using Somnia.GuestLog.Infrastructure;
|
||||
using Somnia.GuestLog.Models.Configuration;
|
||||
@@ -18,6 +21,20 @@ using Somnia.GuestLog.Validators;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
//Load config files.
|
||||
builder.Configuration.AddJsonFile("Config/log_Config.json", optional: false, reloadOnChange: false);
|
||||
|
||||
//Create and configure Logger (Serilog)
|
||||
var serilog = new LoggerConfiguration()
|
||||
.ReadFrom.Configuration(builder.Configuration)
|
||||
.CreateLogger();
|
||||
builder.Services.AddSerilog(serilog);
|
||||
|
||||
var logger = new SerilogLoggerFactory(serilog).CreateLogger<Program>();
|
||||
|
||||
logger.LogInformation("\n\n\n -------------- Starting {appName} {appVersion} --------------", AppSettings.AppName, AppSettings.AppVersion);
|
||||
|
||||
|
||||
var connectionStringGuestLog = builder.Configuration.GetConnectionString("DbSomniaGuestLog")
|
||||
?? throw new InvalidOperationException("Connection string 'DbSomniaGuestLog' not found.");
|
||||
builder.Services.AddDbContextFactory<GuestLogContext>(options =>
|
||||
|
||||
@@ -12,6 +12,13 @@
|
||||
<PackageReference Include="FluentStorage.AWS" />
|
||||
<PackageReference Include="FluentValidation" />
|
||||
<PackageReference Include="MudBlazor" />
|
||||
<PackageReference Include="Serilog" />
|
||||
<PackageReference Include="Serilog.Enrichers.Process" />
|
||||
<PackageReference Include="Serilog.Enrichers.Thread" />
|
||||
<PackageReference Include="Serilog.Extensions.Hosting" />
|
||||
<PackageReference Include="Serilog.Settings.Configuration" />
|
||||
<PackageReference Include="Serilog.Sinks.Console" />
|
||||
<PackageReference Include="Serilog.Sinks.File" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -2,12 +2,6 @@
|
||||
"ConnectionStrings": {
|
||||
"DbSomniaGuestLog": "Host=localhost;Port=5432;Username=postgres;Password=P@ssw0rd;Database=SomniaGuestLog"
|
||||
},
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*",
|
||||
"AddressQuery": {
|
||||
"EnableAddressQuery": true,
|
||||
|
||||
Reference in New Issue
Block a user