From 181c68181395de3ec596e87bcc709692c2ef6afd Mon Sep 17 00:00:00 2001 From: Binary Alley Date: Wed, 6 Nov 2024 17:04:21 +0200 Subject: [PATCH] Sets Serilog file configuration for all setups and environments --- src/Lumina.Presentation.Api/Dockerfile | 4 +--- .../Lumina.Presentation.Api.csproj | 1 + src/Lumina.Presentation.Api/Program.cs | 11 ++++++++++- src/Lumina.Presentation.Api/appsettings.json | 3 ++- src/Lumina.Presentation.Web/Dockerfile | 4 +--- .../Lumina.Presentation.Web.csproj | 1 + src/Lumina.Presentation.Web/Program.cs | 11 ++++++++++- .../Properties/launchSettings.json | 6 +++--- src/Lumina.Presentation.Web/appsettings.json | 3 ++- 9 files changed, 31 insertions(+), 13 deletions(-) diff --git a/src/Lumina.Presentation.Api/Dockerfile b/src/Lumina.Presentation.Api/Dockerfile index b22810de..c84bbb52 100644 --- a/src/Lumina.Presentation.Api/Dockerfile +++ b/src/Lumina.Presentation.Api/Dockerfile @@ -1,9 +1,9 @@ # This stage is used when running from VS in fast mode (Default for Debug configuration) FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base +RUN mkdir /logs && chown -R app:app /logs USER app WORKDIR /app EXPOSE 5214 -EXPOSE 7057 # ==== Stage 1: Build stage FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build @@ -50,9 +50,7 @@ WORKDIR /app COPY --from=publish /app/publish . ENV ASPNETCORE_HTTP_PORTS=5214 -ENV ASPNETCORE_HTTPS_PORTS=7057 EXPOSE 5214 -EXPOSE 7057 ENTRYPOINT ["dotnet", "Lumina.Presentation.Api.dll"] # to build this Docker image, run the following command from the root of the project (location of the .sln file): diff --git a/src/Lumina.Presentation.Api/Lumina.Presentation.Api.csproj b/src/Lumina.Presentation.Api/Lumina.Presentation.Api.csproj index 0ec6b0aa..d278535c 100644 --- a/src/Lumina.Presentation.Api/Lumina.Presentation.Api.csproj +++ b/src/Lumina.Presentation.Api/Lumina.Presentation.Api.csproj @@ -33,6 +33,7 @@ + diff --git a/src/Lumina.Presentation.Api/Program.cs b/src/Lumina.Presentation.Api/Program.cs index 5a51d830..3b6256ce 100644 --- a/src/Lumina.Presentation.Api/Program.cs +++ b/src/Lumina.Presentation.Api/Program.cs @@ -8,6 +8,7 @@ using Lumina.Domain.Common.DependencyInjection; using Lumina.Infrastructure.Common.DependencyInjection; using Lumina.Presentation.Api.Common.DependencyInjection; using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -45,10 +46,18 @@ public class Program // determine log path based on environment string logPath; if (Environment.GetEnvironmentVariable("DOTNET_RUNNING_IN_CONTAINER") == "true") + { logPath = Environment.GetEnvironmentVariable("LOG_PATH") ?? "/logs"; // use docker volume path + builder.WebHost.ConfigureKestrel(options => + { + options.ListenAnyIP(5214); // HTTP only + }); + } else logPath = Path.Combine(AppContext.BaseDirectory, "logs"); // use local binary path Directory.CreateDirectory(logPath); + if (!logPath.EndsWith(Path.DirectorySeparatorChar.ToString())) + logPath = logPath += Path.DirectorySeparatorChar; // set environment variable for Serilog configuration Environment.SetEnvironmentVariable("LOG_PATH", logPath); @@ -58,7 +67,7 @@ public class Program .ReadFrom.Services(services) .Enrich.FromLogContext(); }); - + string? aa = builder.Configuration.GetValue("LOG_PATH"); WebApplication app = builder.Build(); app.UseSerilogRequestLogging(); diff --git a/src/Lumina.Presentation.Api/appsettings.json b/src/Lumina.Presentation.Api/appsettings.json index 6e9958fc..f5281223 100644 --- a/src/Lumina.Presentation.Api/appsettings.json +++ b/src/Lumina.Presentation.Api/appsettings.json @@ -1,5 +1,6 @@ { "Serilog": { + "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ], "MinimumLevel": { "Default": "Information", "Override": { @@ -20,7 +21,7 @@ { "Name": "File", "Args": { - "path": "${LOG_PATH}/lumina_api_.log", + "path": "%LOG_PATH%lumina_api_.log", "rollingInterval": "Day", "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}", "retainedFileCountLimit": 7, diff --git a/src/Lumina.Presentation.Web/Dockerfile b/src/Lumina.Presentation.Web/Dockerfile index 51d02c14..a634add1 100644 --- a/src/Lumina.Presentation.Web/Dockerfile +++ b/src/Lumina.Presentation.Web/Dockerfile @@ -1,9 +1,9 @@ # This stage is used when running from VS in fast mode (Default for Debug configuration) FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base +RUN mkdir /logs && chown -R app:app /logs USER app WORKDIR /app EXPOSE 5012 -EXPOSE 7112 # ==== Stage 1: Build stage @@ -41,9 +41,7 @@ WORKDIR /app COPY --from=publish /app/publish . ENV ASPNETCORE_HTTP_PORTS=5012 -ENV ASPNETCORE_HTTPS_PORTS=7112 EXPOSE 5012 -EXPOSE 7112 ENTRYPOINT ["dotnet", "Lumina.Presentation.Web.dll"] # to build this Docker image, run the following command from the root of the project (location of the .sln file): diff --git a/src/Lumina.Presentation.Web/Lumina.Presentation.Web.csproj b/src/Lumina.Presentation.Web/Lumina.Presentation.Web.csproj index 288e9517..8e9f641f 100644 --- a/src/Lumina.Presentation.Web/Lumina.Presentation.Web.csproj +++ b/src/Lumina.Presentation.Web/Lumina.Presentation.Web.csproj @@ -27,6 +27,7 @@ + diff --git a/src/Lumina.Presentation.Web/Program.cs b/src/Lumina.Presentation.Web/Program.cs index cd604da9..7d4bf1a8 100644 --- a/src/Lumina.Presentation.Web/Program.cs +++ b/src/Lumina.Presentation.Web/Program.cs @@ -1,6 +1,7 @@ #region ========================================================================= USING ===================================================================================== using Lumina.Presentation.Web.Common.DependencyInjection; using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Primitives; @@ -31,10 +32,18 @@ public class Program // determine log path based on environment string logPath; if (Environment.GetEnvironmentVariable("DOTNET_RUNNING_IN_CONTAINER") == "true") + { logPath = Environment.GetEnvironmentVariable("LOG_PATH") ?? "/logs"; // use docker volume path + builder.WebHost.ConfigureKestrel(options => + { + options.ListenAnyIP(5012); // HTTP only + }); + } else logPath = Path.Combine(AppContext.BaseDirectory, "logs"); // use local binary path Directory.CreateDirectory(logPath); + if (!logPath.EndsWith(Path.DirectorySeparatorChar.ToString())) + logPath = logPath += Path.DirectorySeparatorChar; // set environment variable for Serilog configuration Environment.SetEnvironmentVariable("LOG_PATH", logPath); @@ -50,7 +59,7 @@ public class Program app.UseSerilogRequestLogging(); // configure the HTTP request pipeline. - if (!app.Environment.IsDevelopment()) + if (!app.Environment.IsDevelopment() && Environment.GetEnvironmentVariable("DOTNET_RUNNING_IN_CONTAINER") != "true") { app.UseExceptionHandler("/Home/Error"); // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. diff --git a/src/Lumina.Presentation.Web/Properties/launchSettings.json b/src/Lumina.Presentation.Web/Properties/launchSettings.json index cc18997c..780c8f09 100644 --- a/src/Lumina.Presentation.Web/Properties/launchSettings.json +++ b/src/Lumina.Presentation.Web/Properties/launchSettings.json @@ -20,12 +20,12 @@ "commandName": "Docker", "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}", "environmentVariables": { - "ASPNETCORE_HTTPS_PORTS": "7112", - "ASPNETCORE_HTTP_PORTS": "5012" + "ASPNETCORE_HTTPS_PORTS": "8081", + "ASPNETCORE_HTTP_PORTS": "8080" }, "publishAllPorts": true, "useSSL": true } }, "$schema": "http://json.schemastore.org/launchsettings.json" -} +} \ No newline at end of file diff --git a/src/Lumina.Presentation.Web/appsettings.json b/src/Lumina.Presentation.Web/appsettings.json index 695d09b0..0254b2b9 100644 --- a/src/Lumina.Presentation.Web/appsettings.json +++ b/src/Lumina.Presentation.Web/appsettings.json @@ -1,5 +1,6 @@ { "Serilog": { + "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ], "MinimumLevel": { "Default": "Information", "Override": { @@ -22,7 +23,7 @@ { "Name": "File", "Args": { - "path": "${LOG_PATH}/lumina_web_.log", + "path": "%LOG_PATH%lumina_web_.log", "rollingInterval": "Day", "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}", "retainedFileCountLimit": 7,