Hi all, some of our application's unit tests are failing after upgrading to NLog 4.7. We've done some troubleshooting and understand the reason, but we're unclear if this is unintended behavior in NLog 4.7 or if we should update our tests to work around it. The tests pass under NLog 4.6, but fail when using NLog 4.7.
We set the ConfigurationItemFactory.Default.CreateInstance delegate so we can inject services into a custom NLog target. The details of the custom NLog target aren't so important. We use it wrapped in a BufferingWrapper like so:
<target name="wrapperTarget" xsi:type="BufferingWrapper" slidingTimeout="true" bufferSize="${bufferSize}" flushTimeout="${flushTimeOut}">
<target xsi:type="OurCustomTarget"
BatchSize ="${bufferSize}"
Path ="${customLogPath}"
Protocol="${configsetting:item=Protocol}" />
</target>
In our unit tests, we don't bother setting ConfigurationItemFactory.Default.CreateInstance as historically we haven't needed (or wanted!) our custom targets to be used when running unit tests.
After upgrading to NLog 4.7 we get the following exceptions in our tests:
NLog.NLogConfigurationException: Required parameter 'WrappedTarget' on 'BufferingWrapper Target[wrapperTarget]()' was not specified.
at NLog.Internal.PropertyHelper.CheckRequiredParameters(Object o)
at NLog.Config.LoggingConfiguration.ValidateConfig()
at NLog.Config.LoggingConfiguration.InitializeAll()
at NLog.LogFactory.ReconfigExistingLoggers()
at NLog.LogFactory.get_Configuration()
at NLog.LogFactory.GetLoggerThreadSafe(String name, Type loggerType)
at NLog.LogFactory.GetLogger(String name)
at NLog.LogManager.GetLogger(String name)
at OurCompany.Logging.LogService.GetLogger(String name) in E:\BuildAgent\...
at OurCompany.Service.cctor() in E:\BuildAgent\...
I think what's going on here is:
ConfigurationItemFactory.Default.CreateInstance, NLog does not know how to construct our custom target, so BufferingWrapper.WrappedTarget is not set.I suspect the above steps 1 and 2 were also happening in NLog 4.6, but some new validation was added in NLog 4.7.
Potential fixes we could apply on our end are:
ConfigurationItemFactory.Default.CreateInstance in each unit test projectAny suggestions on the best way around this? Thanks!
Hi! Thanks for opening your first issue here! Please make sure to follow the issue template - so we could help you better!
Think the cause was introduced with this PR #3562 It would allow NLog to load LoggingConfiguration even if one target could not be loaded (Before it would abort right away).
But NLog should of course not throw exceptions if having failed to assign required values to a target (or layout). Instead it should just make an alert and move along. Could be nice if it skipped the initialization of the Target if already having detected that required parameters was missing (keeping the target in disabled state).
Created #4102
@waf There is a pre-release-build available here that should resolve this issue:
https://ci.appveyor.com/project/nlog/nlog/builds/34919297/artifacts
Just download NLog.4.7.4.12059-PR4102.nupkg to a local nuget-package-folder
Thank you @snakefoot! I'll download this and report back early this week.
Confirmed that NLog.4.7.4.12059-PR4102.nupkg resolves the issue. Thanks!
@waf NLog 4.7.5 has now been release: https://www.nuget.org/packages/NLog
Thanks! Upgraded to that version and confirmed that everything works. Really appreciate the ping!
Most helpful comment
Thanks! Upgraded to that version and confirmed that everything works. Really appreciate the ping!