Add Serilog with SQLite

master
Tudor Stanciu 2023-01-18 01:01:25 +02:00
parent 778ff47236
commit 2377370aab
6 changed files with 108 additions and 30 deletions

View File

@ -5,6 +5,8 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Serilog.AspNetCore" Version="6.1.0" />
<PackageReference Include="Serilog.Sinks.SQLite" Version="5.5.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
</ItemGroup>

View File

@ -0,0 +1,28 @@
using Microsoft.Extensions.Configuration;
using System.IO;
using System;
using System.Data.Common;
namespace Correo.Extensions
{
public static class SqliteExtensions
{
internal static string GetDatabasePath(this IConfiguration configuration)
{
var workspace = configuration.GetValue<string>("Workspace");
var connectionString = configuration.GetConnectionString("DatabaseConnection");
if (string.IsNullOrEmpty(workspace))
throw new Exception($"Workspace path is empty! Check 'Workspace' parameter.");
var builder = new DbConnectionStringBuilder
{
ConnectionString = connectionString
};
var dataSource = builder["Data Source"].ToString();
var databasePath = dataSource.Replace("{Workspace}", workspace);
return databasePath;
}
}
}

View File

@ -0,0 +1,36 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace Correo.Extensions
{
public static class StartupExtensions
{
public static void ConfigureServices(this IServiceCollection services, IConfiguration configuration)
{
// Add services to the container.
services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
services.AddEndpointsApiExplorer();
services.AddSwaggerGen();
}
public static void Configure(this WebApplication app)
{
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseAuthorization();
app.MapControllers();
}
}
}

View File

@ -1,6 +1,11 @@
using Correo.Extensions;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Serilog;
using Serilog.Core;
using System;
namespace Correo
{
@ -9,30 +14,38 @@ namespace Correo
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
builder.Services.ConfigureServices(builder.Configuration);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Host.UseSerilog((_, loggerConfiguration) =>
{
var databasePath = builder.Configuration.GetDatabasePath();
loggerConfiguration
.ReadFrom.Configuration(builder.Configuration)
.Enrich.FromLogContext()
.WriteTo.Console()
.WriteTo.SQLite(databasePath, "__Logs");
});
var app = builder.Build();
app.Configure();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
try
{
app.UseSwagger();
app.UseSwaggerUI();
var urls = builder.Configuration.GetValue<string>("urls");
Log.Information("Starting Correo...");
Log.Information($"Correo listening on {urls}");
Console.WriteLine("Application started. Press Ctrl+C to shut down.");
app.Run();
}
catch (Exception ex)
{
Log.Fatal(ex, "Correo host terminated unexpectedly");
}
finally
{
Log.CloseAndFlush();
}
app.UseAuthorization();
app.MapControllers();
app.Run();
}
}
}

View File

@ -1,8 +0,0 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

View File

@ -1,8 +1,15 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
"Urls": "http://*:5005",
"Workspace": "workspace",
"ConnectionStrings": {
"DatabaseConnection": "Data Source={Workspace}\\correo.db"
},
"Serilog": {
"MinimumLevel": {
"Default": "Debug",
"Override": {
"Microsoft": "Information"
}
}
},
"AllowedHosts": "*"