NLog: Appsettings layout render not work in .NET Core 2.2 and IIS in process hosting
Hello,
we get exception when we run NLog in .NET Core 2.2 and inprocess hosting in IIS.
Exception: Error Error parsing layout appsettings will be ignored. Exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. —> System.IO.FileNotFoundException: The configuration file ‘appsettings.json’ was not found and is not optional. The physical path is ‘c:\windows\system32\inetsrv\appsettings.json’.
NLog version: 4.5.11 NLog.Web.AspNetCore version: 4.7.1
Platform: .NET Core 2.2
Current NLog config (xml or C#, if relevant)
<nlog>
<targets>
<target xsi:type="Mail" name="Errors"
subject="Unhandled Error"
body="Timestamp: ${longdate:universalTime=true}${newline}Message: ${message}${newline}${appsettings:name=aplicationName}"
to="..."
from="..."
smtpServer=".."
smtpPort="25"/>
</targets>
</nlog>
- What is the current result? Target is completly ignored
- What is the expected result? Target works
- Are there any workarounds? yes/no Yes, host app out of IIS process, or set values directly in nlog.config
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 21 (14 by maintainers)
OK I think I found the problem
for some reasons VS 2017 won’t always (but sometimes it does) update my nlog.config in bin folder (e.g. bin\Debug\netcoreapp2.2), so be warned.
you need this:
1: in nlog.config
2: and for some reason you also need to install the “NLog.Extensions.Logging” package directly:
Not sure why the latter is needed… (it’s the same version as the dependency of NLog.Web.AspNetCore Version 4.8.0
so total:
Issue you might have with custom layout-renders should probably be reported to the correct owner:
https://github.com/linmasaki/NLog.Appsettings.Standard
But I can recommend that you stop using
${appsettings}
, but instead upgrade to NLog.Web.AspNetCore ver. 4.8.0 and use the${configsetting}
:https://github.com/NLog/NLog/wiki/ConfigSetting-Layout-Renderer
NLog.Web.AspNetCore ver 4.9.3 has been released, and now you can do this:
var logger = NLog.LogManager.Setup().LoadConfigurationFromAppSettings().GetCurrentClassLogger();
That replaces the old style:
var logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
@StefH Thank you for pointing out the clunky way of handling appsettings together with NLog.config. Created https://github.com/NLog/NLog.Extensions.Logging/issues/265 about improving this.