From 2377370aab800453ae412d782ecd4a089baffe99 Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Wed, 18 Jan 2023 01:01:25 +0200 Subject: [PATCH] Add Serilog with SQLite --- src/Correo/Correo.csproj | 2 + src/Correo/Extensions/SqliteExtensions.cs | 28 +++++++++++++ src/Correo/Extensions/StartupExtensions.cs | 36 ++++++++++++++++ src/Correo/Program.cs | 49 ++++++++++++++-------- src/Correo/appsettings.Development.json | 8 ---- src/Correo/appsettings.json | 15 +++++-- 6 files changed, 108 insertions(+), 30 deletions(-) create mode 100644 src/Correo/Extensions/SqliteExtensions.cs create mode 100644 src/Correo/Extensions/StartupExtensions.cs delete mode 100644 src/Correo/appsettings.Development.json diff --git a/src/Correo/Correo.csproj b/src/Correo/Correo.csproj index 9da1712..e20cee5 100644 --- a/src/Correo/Correo.csproj +++ b/src/Correo/Correo.csproj @@ -5,6 +5,8 @@ + + diff --git a/src/Correo/Extensions/SqliteExtensions.cs b/src/Correo/Extensions/SqliteExtensions.cs new file mode 100644 index 0000000..af4b8a7 --- /dev/null +++ b/src/Correo/Extensions/SqliteExtensions.cs @@ -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("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; + } + } +} diff --git a/src/Correo/Extensions/StartupExtensions.cs b/src/Correo/Extensions/StartupExtensions.cs new file mode 100644 index 0000000..a2f1637 --- /dev/null +++ b/src/Correo/Extensions/StartupExtensions.cs @@ -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(); + } + } +} diff --git a/src/Correo/Program.cs b/src/Correo/Program.cs index 07888f6..7be9777 100644 --- a/src/Correo/Program.cs +++ b/src/Correo/Program.cs @@ -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("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(); } } } \ No newline at end of file diff --git a/src/Correo/appsettings.Development.json b/src/Correo/appsettings.Development.json deleted file mode 100644 index 0c208ae..0000000 --- a/src/Correo/appsettings.Development.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft.AspNetCore": "Warning" - } - } -} diff --git a/src/Correo/appsettings.json b/src/Correo/appsettings.json index 10f68b8..aa5c599 100644 --- a/src/Correo/appsettings.json +++ b/src/Correo/appsettings.json @@ -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": "*"