Sets Serilog file configuration for all setups and environments

This commit is contained in:
BinaryAlley committed 2024-11-06 17:04:21 +02:00
1 parent 6022800776
commit 181c681813
9 files changed
+30 -12

No files matched your search

+1 -3
View File
@@ -1,9 +1,9 @@
# This stage is used when running from VS in fast mode (Default for Debug configuration) # 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 FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
RUN mkdir /logs && chown -R app:app /logs
USER app USER app
WORKDIR /app WORKDIR /app
EXPOSE 5214 EXPOSE 5214
EXPOSE 7057
# ==== Stage 1: Build stage # ==== Stage 1: Build stage
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
@@ -50,9 +50,7 @@ WORKDIR /app
COPY --from=publish /app/publish . COPY --from=publish /app/publish .
ENV ASPNETCORE_HTTP_PORTS=5214 ENV ASPNETCORE_HTTP_PORTS=5214
ENV ASPNETCORE_HTTPS_PORTS=7057
EXPOSE 5214 EXPOSE 5214
EXPOSE 7057
ENTRYPOINT ["dotnet", "Lumina.Presentation.Api.dll"] 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): # to build this Docker image, run the following command from the root of the project (location of the .sln file):
@@ -33,6 +33,7 @@
<PackageReference Include="Serilog" Version="4.1.0" /> <PackageReference Include="Serilog" Version="4.1.0" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.3" /> <PackageReference Include="Serilog.AspNetCore" Version="8.0.3" />
<PackageReference Include="Serilog.Enrichers.AspnetcoreHttpcontext" Version="1.1.0" /> <PackageReference Include="Serilog.Enrichers.AspnetcoreHttpcontext" Version="1.1.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.4" />
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" /> <PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" /> <PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
</ItemGroup> </ItemGroup>
+10 -1
View File
@@ -8,6 +8,7 @@ using Lumina.Domain.Common.DependencyInjection;
using Lumina.Infrastructure.Common.DependencyInjection; using Lumina.Infrastructure.Common.DependencyInjection;
using Lumina.Presentation.Api.Common.DependencyInjection; using Lumina.Presentation.Api.Common.DependencyInjection;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
@@ -45,10 +46,18 @@ public class Program
// determine log path based on environment // determine log path based on environment
string logPath; string logPath;
if (Environment.GetEnvironmentVariable("DOTNET_RUNNING_IN_CONTAINER") == "true") if (Environment.GetEnvironmentVariable("DOTNET_RUNNING_IN_CONTAINER") == "true")
{
logPath = Environment.GetEnvironmentVariable("LOG_PATH") ?? "/logs"; // use docker volume path logPath = Environment.GetEnvironmentVariable("LOG_PATH") ?? "/logs"; // use docker volume path
builder.WebHost.ConfigureKestrel(options =>
{
options.ListenAnyIP(5214); // HTTP only
});
}
else else
logPath = Path.Combine(AppContext.BaseDirectory, "logs"); // use local binary path logPath = Path.Combine(AppContext.BaseDirectory, "logs"); // use local binary path
Directory.CreateDirectory(logPath); Directory.CreateDirectory(logPath);
if (!logPath.EndsWith(Path.DirectorySeparatorChar.ToString()))
logPath = logPath += Path.DirectorySeparatorChar;
// set environment variable for Serilog configuration // set environment variable for Serilog configuration
Environment.SetEnvironmentVariable("LOG_PATH", logPath); Environment.SetEnvironmentVariable("LOG_PATH", logPath);
@@ -58,7 +67,7 @@ public class Program
.ReadFrom.Services(services) .ReadFrom.Services(services)
.Enrich.FromLogContext(); .Enrich.FromLogContext();
}); });
string? aa = builder.Configuration.GetValue<string>("LOG_PATH");
WebApplication app = builder.Build(); WebApplication app = builder.Build();
app.UseSerilogRequestLogging(); app.UseSerilogRequestLogging();
+2 -1
View File
@@ -1,5 +1,6 @@
{ {
"Serilog": { "Serilog": {
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ],
"MinimumLevel": { "MinimumLevel": {
"Default": "Information", "Default": "Information",
"Override": { "Override": {
@@ -20,7 +21,7 @@
{ {
"Name": "File", "Name": "File",
"Args": { "Args": {
"path": "${LOG_PATH}/lumina_api_.log", "path": "%LOG_PATH%lumina_api_.log",
"rollingInterval": "Day", "rollingInterval": "Day",
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}", "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}",
"retainedFileCountLimit": 7, "retainedFileCountLimit": 7,
+1 -3
View File
@@ -1,9 +1,9 @@
# This stage is used when running from VS in fast mode (Default for Debug configuration) # 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 FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
RUN mkdir /logs && chown -R app:app /logs
USER app USER app
WORKDIR /app WORKDIR /app
EXPOSE 5012 EXPOSE 5012
EXPOSE 7112
# ==== Stage 1: Build stage # ==== Stage 1: Build stage
@@ -41,9 +41,7 @@ WORKDIR /app
COPY --from=publish /app/publish . COPY --from=publish /app/publish .
ENV ASPNETCORE_HTTP_PORTS=5012 ENV ASPNETCORE_HTTP_PORTS=5012
ENV ASPNETCORE_HTTPS_PORTS=7112
EXPOSE 5012 EXPOSE 5012
EXPOSE 7112
ENTRYPOINT ["dotnet", "Lumina.Presentation.Web.dll"] 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): # to build this Docker image, run the following command from the root of the project (location of the .sln file):
@@ -27,6 +27,7 @@
<PackageReference Include="Polly" Version="8.4.1" /> <PackageReference Include="Polly" Version="8.4.1" />
<PackageReference Include="Serilog" Version="4.1.0" /> <PackageReference Include="Serilog" Version="4.1.0" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.3" /> <PackageReference Include="Serilog.AspNetCore" Version="8.0.3" />
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.4" />
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" /> <PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" /> <PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
</ItemGroup> </ItemGroup>
+10 -1
View File
@@ -1,6 +1,7 @@
#region ========================================================================= USING ===================================================================================== #region ========================================================================= USING =====================================================================================
using Lumina.Presentation.Web.Common.DependencyInjection; using Lumina.Presentation.Web.Common.DependencyInjection;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Primitives; using Microsoft.Extensions.Primitives;
@@ -31,10 +32,18 @@ public class Program
// determine log path based on environment // determine log path based on environment
string logPath; string logPath;
if (Environment.GetEnvironmentVariable("DOTNET_RUNNING_IN_CONTAINER") == "true") if (Environment.GetEnvironmentVariable("DOTNET_RUNNING_IN_CONTAINER") == "true")
{
logPath = Environment.GetEnvironmentVariable("LOG_PATH") ?? "/logs"; // use docker volume path logPath = Environment.GetEnvironmentVariable("LOG_PATH") ?? "/logs"; // use docker volume path
builder.WebHost.ConfigureKestrel(options =>
{
options.ListenAnyIP(5012); // HTTP only
});
}
else else
logPath = Path.Combine(AppContext.BaseDirectory, "logs"); // use local binary path logPath = Path.Combine(AppContext.BaseDirectory, "logs"); // use local binary path
Directory.CreateDirectory(logPath); Directory.CreateDirectory(logPath);
if (!logPath.EndsWith(Path.DirectorySeparatorChar.ToString()))
logPath = logPath += Path.DirectorySeparatorChar;
// set environment variable for Serilog configuration // set environment variable for Serilog configuration
Environment.SetEnvironmentVariable("LOG_PATH", logPath); Environment.SetEnvironmentVariable("LOG_PATH", logPath);
@@ -50,7 +59,7 @@ public class Program
app.UseSerilogRequestLogging(); app.UseSerilogRequestLogging();
// configure the HTTP request pipeline. // configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment()) if (!app.Environment.IsDevelopment() && Environment.GetEnvironmentVariable("DOTNET_RUNNING_IN_CONTAINER") != "true")
{ {
app.UseExceptionHandler("/Home/Error"); 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. // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
@@ -20,8 +20,8 @@
"commandName": "Docker", "commandName": "Docker",
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}", "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}",
"environmentVariables": { "environmentVariables": {
"ASPNETCORE_HTTPS_PORTS": "7112", "ASPNETCORE_HTTPS_PORTS": "8081",
"ASPNETCORE_HTTP_PORTS": "5012" "ASPNETCORE_HTTP_PORTS": "8080"
}, },
"publishAllPorts": true, "publishAllPorts": true,
"useSSL": true "useSSL": true
+2 -1
View File
@@ -1,5 +1,6 @@
{ {
"Serilog": { "Serilog": {
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ],
"MinimumLevel": { "MinimumLevel": {
"Default": "Information", "Default": "Information",
"Override": { "Override": {
@@ -22,7 +23,7 @@
{ {
"Name": "File", "Name": "File",
"Args": { "Args": {
"path": "${LOG_PATH}/lumina_web_.log", "path": "%LOG_PATH%lumina_web_.log",
"rollingInterval": "Day", "rollingInterval": "Day",
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}", "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}",
"retainedFileCountLimit": 7, "retainedFileCountLimit": 7,