Skip to content

Commit

Permalink
fix(logging): reduce logging for requests (#206)
Browse files Browse the repository at this point in the history
* build: Added Serilog.Settings.Configuration package for configuring the Serilog logger via the appsettings file

* feat: Moved Serilog configuration to appsettings and added example

* chore(logging): amend sample config with template

* fix(dealer): access issue in controller

---------

Co-authored-by: Fenrikur <3359222+Fenrikur@users.noreply.github.com>
  • Loading branch information
Metawolve and Fenrikur authored Sep 2, 2024
1 parent 4837c9b commit 09d0597
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
namespace Eurofurence.App.Server.Web.Controllers
{
[Route("Api/[controller]")]
[AllowAnonymous]
public class DealersController : BaseController
{
private readonly IDealerService _dealerService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<PackageReference Include="Serilog" Version="4.0.1" />
<PackageReference Include="Serilog.Expressions" Version="5.0.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.2" />
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.7.3" />
Expand Down
55 changes: 1 addition & 54 deletions src/Eurofurence.App.Server.Web/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Autofac;
using Eurofurence.App.Server.Services.Abstractions;
using Eurofurence.App.Server.Services.Abstractions.Fursuits;
using Eurofurence.App.Server.Web.Extensions;
using Eurofurence.App.Server.Web.Jobs;
using Eurofurence.App.Server.Web.Swagger;
Expand All @@ -15,7 +14,6 @@
using Microsoft.Extensions.Logging;
using Serilog;
using Serilog.Context;
using Serilog.Events;
using Swashbuckle.AspNetCore.SwaggerUI;
using System;
using System.Text.Json;
Expand Down Expand Up @@ -359,58 +357,7 @@ public void Configure(
IHostApplicationLifetime appLifetime
)
{
var loggerConfiguration = new LoggerConfiguration().Enrich.FromLogContext();

var consoleLogLevel = env.IsDevelopment() ? LogEventLevel.Debug : LogEventLevel.Information;

loggerConfiguration
.WriteTo.Console(
restrictedToMinimumLevel: consoleLogLevel,
outputTemplate:
"{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{IPAddress}] [{Level}] {Message}{NewLine}{Exception}");

if (!string.IsNullOrEmpty(Configuration["auditLog"]))
{
loggerConfiguration
.WriteTo
.Logger(lc =>
lc.Filter
.ByIncludingOnly($"EventId.Id = {LogEvents.Audit.Id}")
.WriteTo.File(Configuration["auditLog"],
restrictedToMinimumLevel: LogEventLevel.Verbose,
outputTemplate:
"{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{IPAddress}] [{Level}] {Message}{NewLine}{Exception}"
)
);
}

var cgc = new CollectionGameConfiguration();
Configuration.GetSection(CollectionGameConfiguration.CollectionGame).Bind(cgc);

if (!string.IsNullOrEmpty(cgc.LogFile))
{
loggerConfiguration
.WriteTo
.Logger(lc =>
lc.Filter
.ByIncludingOnly($"EventId.Id = {LogEvents.CollectionGame.Id}")
.WriteTo.File(cgc.LogFile, (LogEventLevel)cgc.LogLevel)
);
}

if (!string.IsNullOrEmpty(Configuration["importLog"]))
{
loggerConfiguration
.WriteTo
.Logger(lc =>
lc.Filter
.ByIncludingOnly($"EventId.Id = {LogEvents.Import.Id}")
.WriteTo.File(Configuration["importLog"],
restrictedToMinimumLevel: LogEventLevel.Verbose
)
);
}

var loggerConfiguration = new LoggerConfiguration().ReadFrom.Configuration(Configuration);
Log.Logger = loggerConfiguration.CreateLogger();

loggerFactory
Expand Down
43 changes: 30 additions & 13 deletions src/Eurofurence.App.Server.Web/appsettings.sample.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,36 @@
"ConnectionStrings": {
"Eurofurence": "Server=db; Port=3306; Database=ef_backend; user=root; SslMode=Preferred;"
},
"oAuth": {
"issuer": "EurofurenceAppWebApi",
"audience": "EurofurenceAppWebApi",
"secretKey": "LynxesAreAwesomeAndHaveFuzzyEartuftsAndThisIsTotallyNotTheTokenUsedInTheLiveEnvironment"
},
"wns": {
"clientId": "",
"clientSecret": "",
"targetTopic": "Debug"
"Serilog": {
"Using": [
"Serilog.Sinks.Console",
"Serilog.Sinks.File"
],
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information",
"System.Net.Http.HttpClient": "Warning"
}
},
"WriteTo": [
{
"Name": "Console",
"Args": {
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] [{IPAddress}] {Message}{NewLine}{Exception}"
}
},
{
"Name": "File",
"Args": {
"path": "/tmp/log.txt"
}
}
],
"Enrich": [
"FromLogContext"
]
},
"firebase": {
"googleServiceCredentialKeyFile": "firebase.json",
Expand All @@ -54,10 +75,6 @@
"accessToken": "",
"proxy": ""
},
"collectionGame": {
"logLevel": 1,
"logFile": "/tmp/collect.log"
},
"artistAlley": {
"telegram": {
"adminGroupChatId": "",
Expand Down

0 comments on commit 09d0597

Please sign in to comment.