Nlog: Unit tests failing when referencing project using NLog 4.7

Created on 28 Aug 2020  路  8Comments  路  Source: NLog/NLog

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:

  1. When our unit test project builds, it copies the tested project's NLog.config. We don't do this explicitly -- it just happens by us referencing the tested project.
  2. Because we haven't set ConfigurationItemFactory.Default.CreateInstance, NLog does not know how to construct our custom target, so BufferingWrapper.WrappedTarget is not set.
  3. Some new validation in NLog 4.7 is throwing a hard exception in this case.

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:

  1. Prevent the copying of the NLog.config from the tested project to the unit test project.
  2. Set some sort of stub ConfigurationItemFactory.Default.CreateInstance in each unit test project

Any suggestions on the best way around this? Thanks!

question

Most helpful comment

Thanks! Upgraded to that version and confirmed that everything works. Really appreciate the ping!

All 8 comments

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!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MaximRouiller picture MaximRouiller  路  3Comments

BobSeu picture BobSeu  路  3Comments

Jerefeny picture Jerefeny  路  3Comments

sszost picture sszost  路  3Comments

ericnewton76 picture ericnewton76  路  3Comments