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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions src/OrchardCore/OrchardCore.Logging.NLog/HostBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System.IO;
using Microsoft.Extensions.Hosting;
using NLog;
using NLog.LayoutRenderers;
using NLog.Web;

namespace OrchardCore.Logging;
Expand All @@ -10,15 +8,15 @@ public static class HostBuilderExtensions
{
public static IHostBuilder UseNLogHost(this IHostBuilder builder)
{
LayoutRenderer.Register<TenantLayoutRenderer>(TenantLayoutRenderer.LayoutRendererName);
builder.UseNLog();
builder.ConfigureAppConfiguration((context, _) =>
{
var environment = context.HostingEnvironment;
environment.ConfigureNLog($"{environment.ContentRootPath}{Path.DirectorySeparatorChar}NLog.config");
LogManager.Configuration.Variables["configDir"] = environment.ContentRootPath;
});
LogManager.Setup().SetupExtensions(ext =>
ext.RegisterLayoutRenderer<TenantLayoutRenderer>(TenantLayoutRenderer.LayoutRendererName));

return builder;
return builder
.UseNLog()
.ConfigureAppConfiguration((context, _) =>
{
var environment = context.HostingEnvironment;
LogManager.Configuration.Variables["configDir"] = environment.ContentRootPath;
});
}
}
Original file line number Diff line number Diff line change
@@ -1,40 +1,44 @@
using System.IO;
using System.Reflection;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using NLog;
using NLog.Config;
using NLog.LayoutRenderers;
using NLog.Web;

namespace OrchardCore.Logging
namespace OrchardCore.Logging;

public static class WebHostBuilderExtensions
{
public static class WebHostBuilderExtensions
public static IWebHostBuilder UseNLogWeb(this IWebHostBuilder builder)
{
public static IWebHostBuilder UseNLogWeb(this IWebHostBuilder builder)
{
LayoutRenderer.Register<TenantLayoutRenderer>(TenantLayoutRenderer.LayoutRendererName);
builder.UseNLog();
builder.ConfigureAppConfiguration((context, _) =>
LogManager.Setup().SetupExtensions(ext =>
ext.RegisterLayoutRenderer<TenantLayoutRenderer>(TenantLayoutRenderer.LayoutRendererName));

return builder
.UseNLog()
.ConfigureAppConfiguration((context, _) =>
{
var environment = context.HostingEnvironment;
environment.ConfigureNLog($"{environment.ContentRootPath}{Path.DirectorySeparatorChar}NLog.config");
LogManager.Configuration.Variables["configDir"] = environment.ContentRootPath;
});

return builder;
}
}
}

internal static class AspNetExtensions
internal static class AspNetExtensions
{
public static LoggingConfiguration ConfigureNLog(this IHostEnvironment env, string configFileRelativePath)
{
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;

LogManager.AddHiddenAssembly(typeof(AspNetExtensions).GetTypeInfo().Assembly);
var fileName = Path.Combine(env.ContentRootPath, configFileRelativePath);
LogManager.LoadConfiguration(fileName);
return LogManager.Configuration;
}
var fileName = Path.Combine(env.ContentRootPath, configFileRelativePath);

LogManager.Setup()
.SetupLogFactory(factory => factory.AddCallSiteHiddenAssembly(typeof(AspNetExtensions).GetType().Assembly))
.SetupExtensions(ext =>
{
ext.RegisterLayoutRenderer<TenantLayoutRenderer>(TenantLayoutRenderer.LayoutRendererName);
ext.RegisterNLogWeb();
})
.LoadConfigurationFromFile(fileName);

return LogManager.Configuration;
}
}