diff --git a/examples/ASP.NET Core 3/ASP.NET Core 3 - VS2019/Program.cs b/examples/ASP.NET Core 3/ASP.NET Core 3 - VS2019/Program.cs index 01e13fb3f..8d00b15ed 100644 --- a/examples/ASP.NET Core 3/ASP.NET Core 3 - VS2019/Program.cs +++ b/examples/ASP.NET Core 3/ASP.NET Core 3 - VS2019/Program.cs @@ -14,7 +14,8 @@ public class Program { public static void Main(string[] args) { - var logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger(); + var logger = NLog.LogManager.Setup().LoadConfigurationFromAppSettings().LogFactory.GetCurrentClassLogger(); + try { logger.Debug("init main"); diff --git a/src/NLog.Web.AspNetCore/Config/SetupBuilderExtensions.cs b/src/NLog.Web.AspNetCore/Config/SetupBuilderExtensions.cs new file mode 100644 index 000000000..1822bef52 --- /dev/null +++ b/src/NLog.Web.AspNetCore/Config/SetupBuilderExtensions.cs @@ -0,0 +1,81 @@ +using System; +#if ASP_NET_CORE3 +using System.IO; +#endif +using System.Linq; +using Microsoft.Extensions.Configuration; +using NLog.Config; +using NLog.Extensions.Logging; +using NLog.Web.DependencyInjection; + +namespace NLog.Web +{ + /// + /// Extension methods to setup LogFactory options + /// + public static class SetupBuilderExtensions + { +#if ASP_NET_CORE3 + /// + /// Loads NLog LoggingConfiguration from appsettings.json from the NLog-section + /// + public static ISetupBuilder LoadConfigurationFromAppSettings(this ISetupBuilder setupBuilder, string basePath = null, string environment = null, bool optional = true, bool reloadOnChange = false) + { + var builder = new ConfigurationBuilder() + .SetBasePath(basePath ?? Directory.GetCurrentDirectory()) + .AddJsonFile("appsettings.json", optional, reloadOnChange) + .AddJsonFile($"appsettings.{(environment ?? GetAspNetCoreEnvironment("DOTNET_ENVIRONMENT") ?? GetAspNetCoreEnvironment("ASPNETCORE_ENVIRONMENT") ?? "Production")}.json", optional: true, reloadOnChange: reloadOnChange) + .AddJsonFile("appsettings.local.json", optional: true, reloadOnChange: reloadOnChange) + .AddEnvironmentVariables(prefix: "ASPNETCORE_") + .AddEnvironmentVariables(prefix: "DOTNET_"); + var config = builder.Build(); + return setupBuilder.RegisterNLogWeb().LoadConfigurationFromSection(config); + } + + private static string GetAspNetCoreEnvironment(string variableName) + { + try + { + var environment = Environment.GetEnvironmentVariable(variableName); + if (string.IsNullOrWhiteSpace(environment)) + return null; + + return environment.Trim(); + } + catch (Exception ex) + { + NLog.Common.InternalLogger.Error(ex, "Failed to lookup environment variable {0}", variableName); + return null; + } + } +#endif + + /// + /// Convience method to register aspnet-layoutrenders in NLog.Web as one-liner before loading NLog.config + /// + /// + /// If not providing , then output from aspnet-layoutrenderers will remain empty + /// + public static ISetupBuilder RegisterNLogWeb(this ISetupBuilder setupBuilder, IServiceProvider serviceProvider = null) + { + setupBuilder.SetupExtensions(s => s.RegisterNLogWeb()); + if (serviceProvider != null) + ServiceLocator.ServiceProvider = serviceProvider; + return setupBuilder; + } + + /// + /// Replace with version from NLog.Extension.Logging when it has been released with NLog 4.7 + /// + internal static ISetupBuilder LoadConfigurationFromSection(this ISetupBuilder setupBuilder, IConfiguration configuration) + { + setupBuilder.SetupExtensions(e => e.RegisterConfigSettings(configuration)); + var nlogConfig = configuration.GetSection("NLog"); + if (nlogConfig != null && nlogConfig.GetChildren().Any()) + { + setupBuilder.LogFactory.Configuration = new NLogLoggingConfiguration(nlogConfig, setupBuilder.LogFactory); + } + return setupBuilder; + } + } +} diff --git a/src/NLog.Web.AspNetCore/Config/SetupExtensionsBuilderExtensions.cs b/src/NLog.Web.AspNetCore/Config/SetupExtensionsBuilderExtensions.cs new file mode 100644 index 000000000..494105b2b --- /dev/null +++ b/src/NLog.Web.AspNetCore/Config/SetupExtensionsBuilderExtensions.cs @@ -0,0 +1,30 @@ +using System.Reflection; +using Microsoft.Extensions.Configuration; +using NLog.Config; +using NLog.Extensions.Logging; + +namespace NLog.Web +{ + /// + /// Extension methods to setup NLog extensions, so they are known when loading NLog LoggingConfiguration + /// + public static class SetupExtensionsBuilderExtensions + { + /// + /// Replace with version from NLog.Extension.Logging when it has been released with NLog 4.7 + /// + internal static ISetupExtensionsBuilder RegisterConfigSettings(this ISetupExtensionsBuilder setupBuilder, IConfiguration configuration) + { + ConfigSettingLayoutRenderer.DefaultConfiguration = configuration; + return setupBuilder.RegisterLayoutRenderer("configsetting"); + } + + /// + /// Register the NLog.Web.AspNetCore LayoutRenderers + /// + public static ISetupExtensionsBuilder RegisterNLogWeb(this ISetupExtensionsBuilder setupBuilder) + { + return setupBuilder.RegisterAssembly(typeof(NLogAspNetCoreOptions).GetTypeInfo().Assembly); + } + } +} diff --git a/src/NLog.Web.AspNetCore/NLog.Web.AspNetCore.csproj b/src/NLog.Web.AspNetCore/NLog.Web.AspNetCore.csproj index 40a1b4242..c169424d4 100644 --- a/src/NLog.Web.AspNetCore/NLog.Web.AspNetCore.csproj +++ b/src/NLog.Web.AspNetCore/NLog.Web.AspNetCore.csproj @@ -63,6 +63,7 @@ Supported platforms: +