From fa8ac43d5b50c38d6677683ebb07e0a138b7f481 Mon Sep 17 00:00:00 2001 From: Ionite Date: Tue, 25 Jul 2023 18:23:11 -0400 Subject: [PATCH] Update logging config with http extension filters --- StabilityMatrix.Avalonia/App.axaml.cs | 37 ++++++++++++++------------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/StabilityMatrix.Avalonia/App.axaml.cs b/StabilityMatrix.Avalonia/App.axaml.cs index 23b23fceb..76b9ae671 100644 --- a/StabilityMatrix.Avalonia/App.axaml.cs +++ b/StabilityMatrix.Avalonia/App.axaml.cs @@ -483,11 +483,13 @@ private static void OnExit(object? sender, ControlledApplicationLifetimeExitEven private static LoggingConfiguration ConfigureLogging() { - var logConfig = new LoggingConfiguration(); - - // File target - logConfig.AddRule(NLog.LogLevel.Debug, NLog.LogLevel.Fatal, - new FileTarget("logfile") + LogManager.Setup().LoadConfiguration(builder => { + var debugTarget = builder.ForTarget("console").WriteTo(new DebuggerTarget + { + Layout = "${message}" + }).WithAsync(); + + var fileTarget = builder.ForTarget("logfile").WriteTo(new FileTarget { Layout = "${longdate}|${level:uppercase=true}|${logger}|${message:withexception=true}", ArchiveOldFileOnStartup = true, @@ -495,19 +497,21 @@ private static LoggingConfiguration ConfigureLogging() ArchiveFileName = "${specialfolder:folder=ApplicationData}/StabilityMatrix/app.{#}.log", ArchiveNumbering = ArchiveNumberingMode.Rolling, MaxArchiveFiles = 2 - }); - - // Debugger Target - logConfig.AddRule(NLog.LogLevel.Trace, NLog.LogLevel.Fatal, - new DebuggerTarget("debugger") - { - Layout = "${message}" - }); + }).WithAsync(); + + // Filter some sources to be warn levels or above only + builder.ForLogger("System.*").WriteToNil(NLog.LogLevel.Warn); + builder.ForLogger("Microsoft.*").WriteToNil(NLog.LogLevel.Warn); + builder.ForLogger("Microsoft.Extensions.Http.*").WriteToNil(NLog.LogLevel.Warn); + + builder.ForLogger().FilterMinLevel(NLog.LogLevel.Trace).WriteTo(debugTarget); + builder.ForLogger().FilterMinLevel(NLog.LogLevel.Debug).WriteTo(fileTarget); + }); // Sentry if (SentrySdk.IsEnabled) { - logConfig.AddSentry(o => + LogManager.Configuration.AddSentry(o => { o.InitializeSdk = false; o.Layout = "${message}"; @@ -521,9 +525,6 @@ private static LoggingConfiguration ConfigureLogging() }); } - LogManager.Configuration = logConfig; - - - return logConfig; + return LogManager.Configuration; } }