-
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
229 additions
and
4 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
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,95 @@ | ||
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 | ||
{ | ||
/// <summary> | ||
/// Extension methods to setup LogFactory options | ||
/// </summary> | ||
public static class SetupBuilderExtensions | ||
{ | ||
#if ASP_NET_CORE3 | ||
/// <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).RegisterConfigSettings(config).RegisterNLogWeb()); | ||
|
||
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, IServiceProvider serviceProvider = null) | ||
{ | ||
setupBuilder.SetupExtensions(s => s.RegisterNLogWeb()); | ||
if (serviceProvider != null) | ||
ServiceLocator.ServiceProvider = serviceProvider; | ||
return setupBuilder; | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
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,30 @@ | ||
using System.Reflection; | ||
using Microsoft.Extensions.Configuration; | ||
using NLog.Config; | ||
using NLog.Extensions.Logging; | ||
|
||
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> | ||
public static ISetupExtensionsBuilder RegisterNLogWeb(this ISetupExtensionsBuilder setupBuilder) | ||
{ | ||
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