From dd854a159ad8386ec1a09aee057c06e3df8c34c5 Mon Sep 17 00:00:00 2001 From: z__ Date: Fri, 4 Feb 2022 15:11:49 +0700 Subject: [PATCH] ok high iq solutions is bad for compatibility --- Torch.Server/Program.cs | 12 +++++----- Torch/Utils/TorchLogManager.cs | 44 +++++++++++++++------------------- 2 files changed, 25 insertions(+), 31 deletions(-) diff --git a/Torch.Server/Program.cs b/Torch.Server/Program.cs index 2b41abf..05d744e 100644 --- a/Torch.Server/Program.cs +++ b/Torch.Server/Program.cs @@ -61,9 +61,6 @@ namespace Torch.Server if (File.Exists(oldTorchCfg)) File.Move(oldTorchCfg, torchCfg, true); - - Target.Register(nameof(LogViewerTarget)); - TorchLogManager.SetConfiguration(new XmlLoggingConfiguration(newNlog)); var config = Persistent.Load(torchCfg); config.Data.InstanceName = instanceName; @@ -76,15 +73,18 @@ namespace Torch.Server var handler = new UnhandledExceptionHandler(config.Data, isService); AppDomain.CurrentDomain.UnhandledException += handler.OnUnhandledException; + + Target.Register(nameof(LogViewerTarget)); + TorchLogManager.RegisterTargets(Environment.GetEnvironmentVariable("TORCH_LOG_EXTENSIONS_PATH") ?? + Path.Combine(instancePath, "LoggingExtensions")); + + TorchLogManager.SetConfiguration(new XmlLoggingConfiguration(newNlog)); var initializer = new Initializer(workingDir, config); if (!initializer.Initialize(args)) Environment.Exit(1); TorchLauncher.Launch(workingDir, binDir); - TorchLogManager.SetConfiguration(TorchLogManager.Configuration, - Environment.GetEnvironmentVariable("TORCH_LOG_EXTENSIONS_PATH") ?? - Path.Combine(instancePath, "LoggingExtensions")); CopyNative(binDir); initializer.Run(isService, instanceName, instancePath); diff --git a/Torch/Utils/TorchLogManager.cs b/Torch/Utils/TorchLogManager.cs index 67138eb..5adea49 100644 --- a/Torch/Utils/TorchLogManager.cs +++ b/Torch/Utils/TorchLogManager.cs @@ -1,47 +1,41 @@ using System; using System.IO; +using System.Linq; +using System.Reflection; using System.Runtime.Loader; using NLog; using NLog.Config; +using NLog.Targets; namespace Torch.Utils; public static class TorchLogManager { - private static AssemblyLoadContext LoadContext; + private static AssemblyLoadContext LoadContext = new("TorchLog"); public static LoggingConfiguration Configuration { get; private set; } - public static void SetConfiguration(LoggingConfiguration configuration, string extensionsDir = null) + public static void SetConfiguration(LoggingConfiguration configuration) { Configuration = configuration; - LogManager.Setup() - .SetupExtensions(builder => - { - if (extensionsDir is null || !Directory.Exists(extensionsDir)) - return; - if (LoadContext is null) - { - LoadContext = new("TorchLog"); - foreach (var file in Directory.EnumerateFiles(extensionsDir, "*.dll", SearchOption.AllDirectories)) - { - builder.RegisterAssembly(LoadContext.LoadFromAssemblyPath(file)); - } - return; - } - foreach (var assembly in LoadContext.Assemblies) - { - builder.RegisterAssembly(assembly); - } - }) - .SetupLogFactory(builder => builder.SetThrowConfigExceptions(true)) - .LoadConfiguration(configuration); + LogManager.Configuration = configuration; LogManager.ReconfigExistingLoggers(); } + public static void RegisterTargets(string dir) + { + if (!Directory.Exists(dir)) return; + + foreach (var type in Directory.EnumerateFiles(dir, "*.dll").Select(LoadContext.LoadFromAssemblyPath) + .SelectMany(b => b.ExportedTypes) + .Where(b => b.GetCustomAttribute() is { })) + { + Target.Register(type.GetCustomAttribute()!.Name, type); + } + } + public static void RestoreGlobalConfiguration() { - LogManager.Configuration = Configuration; - LogManager.ReconfigExistingLoggers(); + SetConfiguration(Configuration); } } \ No newline at end of file