feat(core): add optional SQL query logging
This commit is contained in:
parent
b95fb76cd4
commit
6aed05af06
8 changed files with 56 additions and 31 deletions
|
@ -7,30 +7,32 @@ using NodaTime;
|
|||
using Serilog;
|
||||
using Serilog.Events;
|
||||
|
||||
namespace Foxchat.Core;
|
||||
namespace Foxchat.Core.Extensions;
|
||||
|
||||
public static class ServiceCollectionExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Adds Serilog to this service collection. This method also initializes Serilog so it should be called as early as possible, before any log calls.
|
||||
/// </summary>
|
||||
public static void AddSerilog(this WebApplicationBuilder builder, LogEventLevel level)
|
||||
public static void AddSerilog(this WebApplicationBuilder builder)
|
||||
{
|
||||
var config = builder.Configuration.Get<CoreConfig>() ?? new();
|
||||
|
||||
var logCfg = new LoggerConfiguration()
|
||||
.Enrich.FromLogContext()
|
||||
.MinimumLevel.Is(level)
|
||||
.MinimumLevel.Is(config.Logging.LogEventLevel)
|
||||
// ASP.NET's built in request logs are extremely verbose, so we use Serilog's instead.
|
||||
// Serilog doesn't disable the built in logs so we do it here.
|
||||
// Serilog doesn't disable the built-in logs, so we do it here.
|
||||
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
|
||||
.MinimumLevel.Override("Microsoft.EntityFrameworkCore.Database.Command",
|
||||
config.Logging.LogQueries ? LogEventLevel.Information : LogEventLevel.Warning)
|
||||
.MinimumLevel.Override("Microsoft.AspNetCore.Hosting", LogEventLevel.Warning)
|
||||
.MinimumLevel.Override("Microsoft.AspNetCore.Mvc", LogEventLevel.Warning)
|
||||
.MinimumLevel.Override("Microsoft.AspNetCore.Routing", LogEventLevel.Warning)
|
||||
.WriteTo.Console();
|
||||
|
||||
if (config.SeqLogUrl != null)
|
||||
logCfg.WriteTo.Seq(config.SeqLogUrl, restrictedToMinimumLevel: LogEventLevel.Verbose);
|
||||
if (config.Logging.SeqLogUrl != null)
|
||||
logCfg.WriteTo.Seq(config.Logging.SeqLogUrl, restrictedToMinimumLevel: LogEventLevel.Verbose);
|
||||
|
||||
Log.Logger = logCfg.CreateLogger();
|
||||
|
||||
|
@ -54,9 +56,9 @@ public static class ServiceCollectionExtensions
|
|||
return services;
|
||||
}
|
||||
|
||||
public static T AddConfiguration<T>(this WebApplicationBuilder builder, string? configFile = null) where T : class, new()
|
||||
public static T AddConfiguration<T>(this WebApplicationBuilder builder, string? configFile = null)
|
||||
where T : class, new()
|
||||
{
|
||||
|
||||
builder.Configuration.Sources.Clear();
|
||||
builder.Configuration.AddConfiguration(configFile);
|
||||
|
||||
|
@ -76,4 +78,4 @@ public static class ServiceCollectionExtensions
|
|||
.AddIniFile(file, optional: false, reloadOnChange: true)
|
||||
.AddEnvironmentVariables();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue