Could not load file or assembly log4net from CLI/C++ console aplication


I have a project in C# that uses 'log4net'. I have to use this C# dll from a C++ project, which is the main program. So, I implemented a wrapper project in CLI/C++ but I am getting an error with this log4net.dll.

I am having the problem regarding 'log4net' when I call the C# dll from a CLR console application project. I know that my C# project works fine with log4net because I tested it with a console application in C#, doing the same thing I want to do as a wrapper. In both projects (Console aplication in C# ans CLR) I copied log4net.dll in the .exe process folder.

As a test example, to know what I mean... For C# console app I have this test code:

static void Main(string[] args)
        GeneratorStatus genStatus = new GeneratorStatus();
        string log = genStatus.GetGenStatusMessage();

For CLI/C++ console app I have this code:

int main(array<System::String ^> ^args){
GeneratorStatus^ genStatus = gcnew GeneratorStatus();
String^ log = genStatus->GetGenStatusMessage();}

In this last case I get this error:

System.IO.FileLoadException: 'Could not load file or assembly 'log4net, Version=, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)'

I realized that the C# console app project has an App.config file in which it is specified the following about log4net:

    <assemblyIdentity name="log4net" publicKeyToken="669e0ddf0bb1aa2a" culture="neutral"/>
    <bindingRedirect oldVersion="" newVersion=""/>

I don't have any file like this for the CLI/C++ project. I have seen in another post that I need to define an app.config file for my C++ application and include on it the log4net configuration. However, I don't know how to do this. Could you help me? Or if you have other hints...

asked on Stack Overflow Mar 27, 2020 by Mario Sanz Gómez • edited Mar 30, 2020 by mkrieger1

1 Answer


At the end the problem was that visual studio does not create a .exe.config file for the CLR console application project with the binding redirect info. that did have the console application in C#.

So, I created a ConsoleApplication.exe.config as the one that VS creates for the C# console project:

<?xml version="1.0" encoding="utf-8"?>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <assemblyIdentity name="log4net" publicKeyToken="669e0ddf0bb1aa2a" culture="neutral"/>
    <bindingRedirect oldVersion="" newVersion=""/>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2"/></startup></configuration>
answered on Stack Overflow Mar 28, 2020 by Mario Sanz Gómez

User contributions licensed under CC BY-SA 3.0