Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace NLog methods that are now obsoletes #13824

Merged
merged 3 commits into from
Jun 8, 2023
Merged

Conversation

jtkech
Copy link
Member

@jtkech jtkech commented Jun 6, 2023

No description provided.

@@ -30,10 +31,12 @@ internal static class AspNetExtensions
{
public static LoggingConfiguration ConfigureNLog(this IHostEnvironment env, string configFileRelativePath)
{
ConfigurationItemFactory.Default.RegisterItemsFromAssembly(typeof(AspNetExtensions).GetTypeInfo().Assembly);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of depending on assembly-scanning, then it could nice if just doing registration of the single custom LayoutRenderer:

var fileName = Path.Combine(env.ContentRootPath, configFileRelativePath);
LogManager.Setup().SetupLogFactory(factory => factory.AddCallSiteHiddenAssembly(typeof(AspNetExtensions).GetTypeInfo().Assembly))
          .SetupExtensions(ext => {
                ext.RegisterLayoutRenderer<TenantLayoutRenderer>(TenantLayoutRenderer.LayoutRendererName);
                ext.RegisterNLogWeb();
          })
          .LoadConfigurationFromFile(fileName);
return LogManager.Configuration;

LayoutRenderer.Register<TenantLayoutRenderer>(TenantLayoutRenderer.LayoutRendererName);
LogManager.Setup().SetupExtensions(builder =>
builder.RegisterLayoutRenderer<TenantLayoutRenderer>(TenantLayoutRenderer.LayoutRendererName));

builder.UseNLog();
builder.ConfigureAppConfiguration((context, _) =>
Copy link

@snakefoot snakefoot Jun 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Notice that builder.UseNLog() with NLog.Web.AspNetCore v5, will automatically load nlog.config from the env.ContentRootPath.

So ConfigureAppConfiguration could be changed to just:

builder.ConfigureAppConfiguration((context, _) =>
{
   var environment = context.HostingEnvironment;
   LogManager.Configuration.Variables["configDir"] = environment.ContentRootPath;
}

LayoutRenderer.Register<TenantLayoutRenderer>(TenantLayoutRenderer.LayoutRendererName);
LogManager.Setup().SetupExtensions(builder =>
builder.RegisterLayoutRenderer<TenantLayoutRenderer>(TenantLayoutRenderer.LayoutRendererName));

builder.UseNLog();
builder.ConfigureAppConfiguration((context, _) =>
Copy link

@snakefoot snakefoot Jun 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Notice that builder.UseNLog() with NLog.Web.AspNetCore v5, will automatically load nlog.config from the env.ContentRootPath.

So ConfigureAppConfiguration could be changed to just:

builder.ConfigureAppConfiguration((context, _) =>
{
   var environment = context.HostingEnvironment;
   LogManager.Configuration.Variables["configDir"] = environment.ContentRootPath;
}

@jtkech
Copy link
Member Author

jtkech commented Jun 7, 2023

@snakefoot I did the changes, thanks for the feedback!

@snakefoot
Copy link

Happy that you approved my suggestions :)

@sebastienros sebastienros merged commit d0f9707 into main Jun 8, 2023
@sebastienros sebastienros deleted the jtkech/nlog-obsolete branch June 8, 2023 18:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants