TypeLoadException with different NLog versions in MSTest

Josep Arnau 21 Reputation points
2022-12-21T16:27:46.807+00:00

Hi,

I have a project A (an application) which includes a package B, which in turn contains a third party package C3, which in turn contains another third party package package D3. Both packages A and D3 use the same third party package NLog but with different versions: package A includes NLog with version 5.0.0, and package D3 contains NLog with lower version 4.5.0.

A (application)
|-B
|...|-C3
|.......|-D3
|...........|-NLog 4.5.0
|
|-NLog 5.0.0

Compilation of project A is ok and execution of a certain program P is ok.

Then I have a test (MSTest) project T that includes project A:

T (test project)
|-A (application)
....|-B
....|...|-C3
....|.......|-D3
....|...........|-NLog 4.5.0
....|
....|-NLog 5.0.0

But, although project T compiles correctly, package D3 throws an exception at runtime when running a test which internally loads program P with message:

System.TypeLoadException: 'Could not load type 'NLog.Targets.OutputDebugStringTarget' from assembly 'NLog, Version=5.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c'.'

Since project A runs ok, and project T is just a test project which includes project A, I would expect both executions to behave the same but it seems that, when executing the test in project T, D3 uses NLog 5.0.0 and fails because it expects 4.5.0.

The Global Assembly Cache does not contain NLog, so I do not expect the GAC to be related to the problem.

Has anyone gone through a similar situation or has any clue?

Visual Studio Testing
Visual Studio Testing
Visual Studio: A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.Testing: The act or process of applying tests as a means of analysis or diagnosis.
328 questions
0 comments No comments
{count} votes

Accepted answer
  1. Michael Taylor 48,046 Reputation points
    2022-12-21T17:49:21.55+00:00

    That isn't going to work. NLog had breaking changes between v4 and v5. You cannot use code that was compiled for v4 with the v5 version of NLog and vice versa. You'll need to pick the version you want to use and either upgrade your code (and any dependencies) to use v5 or downgrade the package to v4 until you can. This is a known issue with NLog.

    In general you should not try to mix major versions of packages as breaking changes can happen between major versions (hence the version change). But NLog is pretty notorious for breaking things even between minor versions. But in this case v5 broke any v4 reliant code.


1 additional answer

Sort by: Most helpful
  1. Josep Arnau 21 Reputation points
    2023-01-23T08:56:25.2366667+00:00

    Hi, after double checking, the issue came from the third party library having a different behavior for tests and normal run, injecting nlog for the tests pipeline, which led to the crash. Have solved it by not injecting nlog for that third library, which was not needed for the specific test. This ticket can be closed thx.

    0 comments No comments