mirror of
https://github.com/Kareadita/Kavita.git
synced 2025-06-01 04:34:49 -04:00
* Recreated Kavita Logging with Serilog instead of Default. This needs to be move out of the appsettings now, to allow auto updater to patch. * Refactored the code to be completely configured via Code rather than appsettings.json. This is a required step for Auto Updating. * Added in the ability to send logs directly to the UI only for users on the log route. Stopping implementation as Alerts page will handle the rest of the implementation. * Fixed up the backup service to not rely on Config from appsettings.json * Tweaked the Logging levels available * Moved everything over to File-scoped namespaces * Moved everything over to File-scoped namespaces * Code cleanup, removed an old migration and changed so debug logging doesn't print sensitive db data * Removed dead code
90 lines
3.9 KiB
C#
90 lines
3.9 KiB
C#
using System.IO;
|
|
using API.Services;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Serilog;
|
|
using Serilog.Core;
|
|
using Serilog.Events;
|
|
|
|
namespace API.Logging;
|
|
|
|
/// <summary>
|
|
/// This class represents information for configuring Logging in the Application. Only a high log level is exposed and Kavita
|
|
/// controls the underlying log levels for different loggers in ASP.NET
|
|
/// </summary>
|
|
public static class LogLevelOptions
|
|
{
|
|
public const string LogFile = "config/logs/kavita.log";
|
|
public const bool LogRollingEnabled = true;
|
|
/// <summary>
|
|
/// Controls the Logging Level of the Application
|
|
/// </summary>
|
|
private static readonly LoggingLevelSwitch LogLevelSwitch = new ();
|
|
/// <summary>
|
|
/// Controls Microsoft's Logging Level
|
|
/// </summary>
|
|
private static readonly LoggingLevelSwitch MicrosoftLogLevelSwitch = new (LogEventLevel.Error);
|
|
/// <summary>
|
|
/// Controls Microsoft.Hosting.Lifetime's Logging Level
|
|
/// </summary>
|
|
private static readonly LoggingLevelSwitch MicrosoftHostingLifetimeLogLevelSwitch = new (LogEventLevel.Error);
|
|
/// <summary>
|
|
/// Controls Hangfire's Logging Level
|
|
/// </summary>
|
|
private static readonly LoggingLevelSwitch HangfireLogLevelSwitch = new (LogEventLevel.Error);
|
|
/// <summary>
|
|
/// Controls Microsoft.AspNetCore.Hosting.Internal.WebHost's Logging Level
|
|
/// </summary>
|
|
private static readonly LoggingLevelSwitch AspNetCoreLogLevelSwitch = new (LogEventLevel.Error);
|
|
|
|
public static LoggerConfiguration CreateConfig(LoggerConfiguration configuration)
|
|
{
|
|
return configuration
|
|
.MinimumLevel
|
|
.ControlledBy(LogLevelSwitch)
|
|
.MinimumLevel.Override("Microsoft", MicrosoftLogLevelSwitch)
|
|
.MinimumLevel.Override("Microsoft.Hosting.Lifetime", MicrosoftHostingLifetimeLogLevelSwitch)
|
|
.MinimumLevel.Override("Hangfire", HangfireLogLevelSwitch)
|
|
.MinimumLevel.Override("Microsoft.AspNetCore.Hosting.Internal.WebHost", AspNetCoreLogLevelSwitch)
|
|
.Enrich.FromLogContext()
|
|
.WriteTo.Console()
|
|
.WriteTo.File(LogFile,
|
|
shared: true,
|
|
rollingInterval: RollingInterval.Day,
|
|
outputTemplate: "[{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} {CorrelationId} {Level}] {Message:lj}{NewLine}{Exception}");
|
|
}
|
|
|
|
public static void SwitchLogLevel(string level)
|
|
{
|
|
switch (level)
|
|
{
|
|
case "Debug":
|
|
LogLevelSwitch.MinimumLevel = LogEventLevel.Debug;
|
|
MicrosoftHostingLifetimeLogLevelSwitch.MinimumLevel = LogEventLevel.Debug;
|
|
AspNetCoreLogLevelSwitch.MinimumLevel = LogEventLevel.Debug;
|
|
break;
|
|
case "Information":
|
|
LogLevelSwitch.MinimumLevel = LogEventLevel.Information;
|
|
MicrosoftHostingLifetimeLogLevelSwitch.MinimumLevel = LogEventLevel.Error;
|
|
AspNetCoreLogLevelSwitch.MinimumLevel = LogEventLevel.Error;
|
|
break;
|
|
case "Trace":
|
|
LogLevelSwitch.MinimumLevel = LogEventLevel.Verbose;
|
|
MicrosoftHostingLifetimeLogLevelSwitch.MinimumLevel = LogEventLevel.Debug;
|
|
AspNetCoreLogLevelSwitch.MinimumLevel = LogEventLevel.Debug;
|
|
break;
|
|
case "Warning":
|
|
LogLevelSwitch.MinimumLevel = LogEventLevel.Warning;
|
|
MicrosoftHostingLifetimeLogLevelSwitch.MinimumLevel = LogEventLevel.Error;
|
|
AspNetCoreLogLevelSwitch.MinimumLevel = LogEventLevel.Error;
|
|
break;
|
|
case "Critical":
|
|
LogLevelSwitch.MinimumLevel = LogEventLevel.Error;
|
|
MicrosoftHostingLifetimeLogLevelSwitch.MinimumLevel = LogEventLevel.Error;
|
|
AspNetCoreLogLevelSwitch.MinimumLevel = LogEventLevel.Error;
|
|
break;
|
|
}
|
|
}
|
|
|
|
}
|