Closed SirBisgaard closed 4 years ago
Jesus, I hope my issue will be found with this Id #404...
Sounds like a duplicate of #265 . NLog cannot read ${configsetting}
-options before having loaded appsettings.json.
Chicken and the egg problem. You want the logging-framework up and running early, but suddenly it depends on having loaded appsettings.json.
I'm hoping that LogFactory.Setup
in NLog 4.7 will make it easier to hook NLog together with Extension Logging (and friends). Thus removing NLogBuilder.ConfigureNLog("nlog.config")
It is like that issue #265. But should the documentation not be changed to match this problem? For example on the layout render it should state that this problem will occur when using web APIs.
@SirBisgaard You are completely right. Think the problem is that I have never used NLog.Extensions.Logging
for anything. So I'm not the best person for writing documentation for this. I have tried to explain how it works here:
https://github.com/NLog/NLog/wiki/ConfigSetting-Layout-Renderer
But I have also tried to implement logic so it will automatically register itself using AddNLog
and UseNLog
(To make it more userfriendly, as you have witnessed when not using NLogBuilder
). Ofcourse this is not possible using NLogBuilder.ConfigureNLog
(Happens before HostBuilder
). Which is the reason I will try to get rid of it and instead replace it with extension-methods on LogFactory.Setup
in NLog 4.7.
Again NLog is a community project that is developed in peoples spare time. The more contribution the project gets the faster the development will become.
With NLog.Web.AspNetCore ver. 4.9.3 then you can replace this:
var logger = NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
With:
var logger = LogManager.Setup().LoadConfigurationFromAppSettings().GetCurrentClassLogger();
Allowing logging to work before having started hosting-environment.
Bug
NLog version: 4.6.8 NLog.Extensions.Logging version: 1.6.1 NLog.Web.AspNetCore version: 4.9.0
Platform: .NET Core 3.2 Current NLog config
What is the current result? We have followed the guide provided at Getting started with ASP.NET Core 3 As seen in our config we use the ConfigSettingRenderLayout, to get the connection string from our app settings. The problem is that the render does not pick up the connection string from our app settings. And the internal log is prompting the error message from the layout render class.
The problem is only present when I debug the code but not when deploying the project to our IIS servers. I have tried to find the source of the problem. And when I comment out the NLog code in the Program.cs file as shown below. It had no problem finding the connection string in the app settings.
So I think the issue is that the configure NLog statement in Program.cs does not have a reference to app settings at that moment, but when loading nlog later it does not reload/reapply the layout render with the new configuration. I hope it makes some sense... :-)
What is the expected result? That the layout render gets the IConfiguration so it can get the connection string.
Please post full exception details (message, stacktrace, inner exceptions)
Are there any work arrounds? yes/no Yes but it breaks the best practice guide provided by the framework.
Is there a version in which it did worked? No.
Can you help us by writing an unit test? No.