Skip to content

Commit

Permalink
Merge pull request #183 from ARKlab/feature/ConfigureNLog_customization
Browse files Browse the repository at this point in the history
feat(NLog): add callback to customize the NLog configuration during setup
  • Loading branch information
AndreaCuneo authored Aug 9, 2024
2 parents 03a2500 + 852c093 commit 55eae19
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ public static IHostBuilder GetHostBuilder(string[] args)
public static IHostBuilder Config(this IHostBuilder builder, string[] args)
{
return builder
.ConfigureNLog("Ark.Reference.Core.WebInterface")
.ConfigureNLog("Ark.Reference.Core.WebInterface"
// , configure: c => c.WithDatabaseRule("*", NLog.LogLevel.Info) // always log INFO to Database Target if present
)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder
Expand Down
13 changes: 8 additions & 5 deletions Ark.Tools.NLog.Configuration/NLogConfigurer.Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public static Configurer WithDefaultTargetsAndRulesFromConfiguration(this Config
return @this;
}

public static IHostBuilder ConfigureNLog(this IHostBuilder builder, string? appName = null, string? mailFrom = null)
public static IHostBuilder ConfigureNLog(this IHostBuilder builder, string? appName = null, string? mailFrom = null, Action<Configurer>? configure = null)
{
appName ??= Assembly.GetEntryAssembly()?.GetName().Name ?? AppDomain.CurrentDomain.FriendlyName ?? "Unknown";

Expand All @@ -82,14 +82,17 @@ public static IHostBuilder ConfigureNLog(this IHostBuilder builder, string? appN

return builder.ConfigureLogging((ctx, logging) =>
{
NLogConfigurer.For(appName)
.WithDefaultTargetsAndRulesFromConfiguration(ctx.Configuration, appName, mailFrom, async: !ctx.HostingEnvironment.IsEnvironment("SpecFlow"))
.Apply();
var c = NLogConfigurer.For(appName)
.WithDefaultTargetsAndRulesFromConfiguration(ctx.Configuration, appName, mailFrom, async: !ctx.HostingEnvironment.IsEnvironment("SpecFlow"))
;

configure ?.Invoke(c);

c.Apply();

logging.ClearProviders();
logging.AddNLog();
});

}
}
}

0 comments on commit 55eae19

Please # to comment.