-
Notifications
You must be signed in to change notification settings - Fork 165
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LogManager.Setup() - SetupBuilderExtension with LoadConfigurationFrom…
…AppSettings
- Loading branch information
Showing
5 changed files
with
251 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
src/NLog.Web.AspNetCore/Config/SetupBuilderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
using System; | ||
#if !ASP_NET_CORE1 && !ASP_NET_CORE2 | ||
using System.IO; | ||
using System.Linq; | ||
#endif | ||
using Microsoft.Extensions.Configuration; | ||
using NLog.Config; | ||
using NLog.Extensions.Logging; | ||
|
||
namespace NLog.Web | ||
{ | ||
/// <summary> | ||
/// Extension methods to setup LogFactory options | ||
/// </summary> | ||
public static class SetupBuilderExtensions | ||
{ | ||
#if !ASP_NET_CORE1 && !ASP_NET_CORE2 | ||
/// <summary> | ||
/// Loads NLog LoggingConfiguration from appsettings.json from the NLog-section | ||
/// </summary> | ||
public static ISetupBuilder LoadConfigurationFromAppSettings(this ISetupBuilder setupBuilder, string basePath = null, string environment = null, string nlogConfigSection = "NLog", bool optional = true, bool reloadOnChange = false) | ||
{ | ||
environment = environment ?? GetAspNetCoreEnvironment("DOTNET_ENVIRONMENT") ?? GetAspNetCoreEnvironment("ASPNETCORE_ENVIRONMENT") ?? "Production"; | ||
|
||
var builder = new ConfigurationBuilder() | ||
// Host Configuration | ||
.SetBasePath(basePath ?? Directory.GetCurrentDirectory()) | ||
.AddEnvironmentVariables(prefix: "ASPNETCORE_") | ||
.AddEnvironmentVariables(prefix: "DOTNET_") | ||
// App Configuration | ||
.AddJsonFile("appsettings.json", optional, reloadOnChange) | ||
.AddJsonFile($"appsettings.{environment}.json", optional: true, reloadOnChange: reloadOnChange) | ||
.AddEnvironmentVariables(); | ||
|
||
var config = builder.Build(); | ||
if (!string.IsNullOrEmpty(nlogConfigSection) && config.GetSection(nlogConfigSection)?.GetChildren().Any() == true) | ||
{ | ||
return setupBuilder.SetupExtensions(e => e.AutoLoadAssemblies(false).RegisterNLogWeb()).LoadConfigurationFromSection(config, nlogConfigSection); | ||
} | ||
else | ||
{ | ||
setupBuilder.SetupExtensions(e => e.AutoLoadAssemblies(false).RegisterNLogWeb().RegisterConfigSettings(config)); | ||
|
||
if (!string.IsNullOrEmpty(basePath)) | ||
{ | ||
if (!string.IsNullOrEmpty(environment)) | ||
{ | ||
setupBuilder.LoadConfigurationFromFile(Path.Combine(basePath, $"nlog.{environment}.config"), optional: true); | ||
} | ||
|
||
setupBuilder.LoadConfigurationFromFile(Path.Combine(basePath, "nlog.config"), optional: true); | ||
} | ||
else if (!string.IsNullOrEmpty(environment)) | ||
{ | ||
setupBuilder.LoadConfigurationFromFile($"nlog.{environment}.config", optional: true); | ||
} | ||
|
||
return setupBuilder.LoadConfigurationFromFile(); // No effect, if config already loaded | ||
} | ||
} | ||
|
||
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 | ||
|
||
/// <summary> | ||
/// Convience method to register aspnet-layoutrenders in NLog.Web as one-liner before loading NLog.config | ||
/// </summary> | ||
/// <remarks> | ||
/// If not providing <paramref name="serviceProvider"/>, then output from aspnet-layoutrenderers will remain empty | ||
/// </remarks> | ||
public static ISetupBuilder RegisterNLogWeb(this ISetupBuilder setupBuilder, IConfiguration configuration = null, IServiceProvider serviceProvider = null) | ||
{ | ||
setupBuilder.SetupExtensions(builder => builder.AutoLoadAssemblies(false).RegisterNLogWeb(serviceProvider)); | ||
|
||
if (configuration == null && serviceProvider != null) | ||
{ | ||
configuration = serviceProvider.GetService(typeof(IConfiguration)) as IConfiguration; | ||
} | ||
|
||
if (configuration != null) | ||
{ | ||
setupBuilder.SetupExtensions(builder => builder.RegisterConfigSettings(configuration)); | ||
} | ||
|
||
return setupBuilder; | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/NLog.Web.AspNetCore/Config/SetupExtensionsBuilderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System; | ||
using System.Reflection; | ||
using Microsoft.Extensions.Configuration; | ||
using NLog.Config; | ||
using NLog.Extensions.Logging; | ||
using NLog.Web.DependencyInjection; | ||
|
||
namespace NLog.Web | ||
{ | ||
/// <summary> | ||
/// Extension methods to setup NLog extensions, so they are known when loading NLog LoggingConfiguration | ||
/// </summary> | ||
public static class SetupExtensionsBuilderExtensions | ||
{ | ||
/// <summary> | ||
/// Replace with version from NLog.Extension.Logging when it has been released with NLog 4.7 | ||
/// </summary> | ||
internal static ISetupExtensionsBuilder RegisterConfigSettings(this ISetupExtensionsBuilder setupBuilder, IConfiguration configuration) | ||
{ | ||
ConfigSettingLayoutRenderer.DefaultConfiguration = configuration; | ||
return setupBuilder.RegisterLayoutRenderer<ConfigSettingLayoutRenderer>("configsetting"); | ||
} | ||
|
||
/// <summary> | ||
/// Register the NLog.Web.AspNetCore LayoutRenderers | ||
/// </summary> | ||
/// <remarks> | ||
/// If not providing <paramref name="serviceProvider"/>, then output from aspnet-layoutrenderers will remain empty | ||
/// </remarks> | ||
public static ISetupExtensionsBuilder RegisterNLogWeb(this ISetupExtensionsBuilder setupBuilder, IServiceProvider serviceProvider = null) | ||
{ | ||
if (serviceProvider != null) | ||
{ | ||
ServiceLocator.ServiceProvider = serviceProvider; | ||
} | ||
|
||
return setupBuilder.RegisterAssembly(typeof(NLogAspNetCoreOptions).GetTypeInfo().Assembly); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters