From f12eb7ecc788a6f07e7b61e67d6ff7fd5d5d7baf Mon Sep 17 00:00:00 2001 From: jtkech Date: Thu, 28 Apr 2022 01:16:47 +0200 Subject: [PATCH] UseNLogHost() to be used on IHostBuilder --- .../HostBuilderExtensions.cs | 24 +++++++++++++++++++ .../WebHostBuilderExtensions.cs | 3 +-- 2 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 src/OrchardCore/OrchardCore.Logging.NLog/HostBuilderExtensions.cs diff --git a/src/OrchardCore/OrchardCore.Logging.NLog/HostBuilderExtensions.cs b/src/OrchardCore/OrchardCore.Logging.NLog/HostBuilderExtensions.cs new file mode 100644 index 00000000000..0d0ea4dafc0 --- /dev/null +++ b/src/OrchardCore/OrchardCore.Logging.NLog/HostBuilderExtensions.cs @@ -0,0 +1,24 @@ +using System.IO; +using Microsoft.Extensions.Hosting; +using NLog; +using NLog.LayoutRenderers; +using NLog.Web; + +namespace OrchardCore.Logging; + +public static class HostBuilderExtensions +{ + public static IHostBuilder UseNLogHost(this IHostBuilder builder) + { + LayoutRenderer.Register(TenantLayoutRenderer.LayoutRendererName); + builder.UseNLog(); + builder.ConfigureAppConfiguration((context, _) => + { + var environment = context.HostingEnvironment; + environment.ConfigureNLog($"{environment.ContentRootPath}{Path.DirectorySeparatorChar}NLog.config"); + LogManager.Configuration.Variables["configDir"] = environment.ContentRootPath; + }); + + return builder; + } +} diff --git a/src/OrchardCore/OrchardCore.Logging.NLog/WebHostBuilderExtensions.cs b/src/OrchardCore/OrchardCore.Logging.NLog/WebHostBuilderExtensions.cs index 2239c0bc388..5b05a017fa3 100644 --- a/src/OrchardCore/OrchardCore.Logging.NLog/WebHostBuilderExtensions.cs +++ b/src/OrchardCore/OrchardCore.Logging.NLog/WebHostBuilderExtensions.cs @@ -15,7 +15,7 @@ public static IWebHostBuilder UseNLogWeb(this IWebHostBuilder builder) { LayoutRenderer.Register(TenantLayoutRenderer.LayoutRendererName); builder.UseNLog(); - builder.ConfigureAppConfiguration((context, configuration) => + builder.ConfigureAppConfiguration((context, _) => { var environment = context.HostingEnvironment; environment.ConfigureNLog($"{environment.ContentRootPath}{Path.DirectorySeparatorChar}NLog.config"); @@ -26,7 +26,6 @@ public static IWebHostBuilder UseNLogWeb(this IWebHostBuilder builder) } } - // Waiting for NLog to use `IHostEnvironment`. internal static class AspNetExtensions { public static LoggingConfiguration ConfigureNLog(this IHostEnvironment env, string configFileRelativePath)