13

I've been asked to take a look at an error in an ASP/C# application with its Paypal integration. The error, shown in full, is:

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

From what I understand, this means that the actual file located (that is, log4net.dll in my bin directory) does not match the version expected based on some assembly configuration. The problem I'm having is that I cannot locate where this file is being referenced.

I have access to all the files in the web root directory of the site, and cannot locate any config files that reference this DLL. Where else might I need to look to determine what's causing the mis-match?

As a note, I've made sure that the version of the DLL in the bin directory is up to date, but this does not seem to have resolved anything.

Elie
  • 13,693
  • 23
  • 74
  • 128

9 Answers9

16

We had this problem also when we moved to VS 2010 and .NET 4.0, we don't use log4net at all, but I suspect something else we use does (maybe Crystal Reports?) and I also suspect there is a dll we use that is a 32-bit dll as well because when I change the "Enable 32-Bit Applications" option under the advanced settings for the Application Pool in IIS to "True" everything worked again.

Matt Palmerlee
  • 2,668
  • 2
  • 27
  • 28
  • Yup, for me it's definitely due to Crystal Reports. – basher Apr 28 '15 at 14:32
  • It's funny. It happened to me too - asking for reference which my solution does not use at all. My solution which "worked" just restart VS2017. And I know exactly that previously loaded project uses log4net. But current not at all. Seems they share some temp files or smth and there is a clash. – Saulius Nov 29 '17 at 10:34
  • It is Crystal Reports that needs log4net and you can download CR here, you need to do a quick registration though. https://www.sap.com/cmp/td/sap-crystal-reports-visual-studio-trial.html – Dung Apr 15 '20 at 14:47
11

You need to control the references in projects as well - the references are compiled into the assembly itself and will attempt a loading. My guess is, you have an outdated reference to the log4net assembly but are using the latest version.

Femaref
  • 60,705
  • 7
  • 138
  • 176
  • That makes sense. Is there a way for me to see which reference is being made in the project - what file would this typically be located in when looking at the compiled project? – Elie Jan 10 '11 at 01:12
  • You shouldn't look at the compiled project, but the uncompiled one - the csproj file. otherwise, I think depends.exe (in Mark's post) would help. – Femaref Jan 10 '11 at 01:32
  • This can be verified in nuget packages as well. If one of your projects reference log4net version 1.2.3.4 and the other projects looks for log4net version 5.6.7.8, then you will get this error. (or any other nuget packages). Just match those versions, and you're good to go. – fluidguid Jul 13 '17 at 22:51
4

We had a similar issue with our web application. We went from ancient .NET 1.1 32-bit to .NET 4.0 64-bit.

The error I was receiving within our one of our user controls was the following:

ASP.NET runtime error: Could not load file or assembly 'log4net' or one of its dependencies. An attempt was made to load a program with an incorrect format.

My guess here is that we're compiling a DLL in 64-bit but its referencing a 32-bit dll? I would agree with what Matt Palmerlee said above - switching the application pool to 32-bit mode DOES FIX the problem but your still stuck in a 32-bit application pool. We wanted to take advantage of the extra memory 64-bit will offer our IIS application pools.

Ultimately, I was not able to figure out which of the third party DLLs was actually referencing log4net. I did notice after building though that a log4net.dll was copied into my "bin" directory and I right clicked on it and figured out it was related to Apache Foundation - http://logging.apache.org/log4net/

I ended up just downloading the latest log4net.dll for .NET 4, added it as a reference to my web application project, recompiled and then re-opened the user control and the error disappeared.

log4net download

Hope this helps

Chris Smith
  • 688
  • 8
  • 20
  • I had same problem..it was referencing log4net from GAC so, your comments helped me to fix it...thanks – Munawar Sep 14 '12 at 12:52
2

Same error here, here is how we fixed: Downloading latest .Net 4.0 log4net.dll from Apache and replacing the version in the bin folder worked for me. You should add the reference to your project to make it permanent. Here is the link: Apache

Go to Downloads, Binaries and choose the new key version. Once downloaded, navigate to the .Net 4.0 folder to find the .dll file.

Theodore
  • 21
  • 1
2

You probably have the latest version of log4net but have a project that's referencing an old one. You can force all assemblies referencing the old version to reference the new version by using a <bindingRedirect>

You can find more info about them here: http://msdn.microsoft.com/en-us/library/eftw1fys.aspx

If you don't know the specific version to redirect, you can also use a range of versions and point them all to your specific version.

lomaxx
  • 113,627
  • 57
  • 144
  • 179
1

I don't have any useful information about the specific error. However, in case you have not used them a couple of useful utilities to help with this type of problem are Dependency Walker and .NET Reflector.

The dependency checker can be used to see if there are unexpected modules used by the log4net assembly. And the Reflector utility shows all kinds of useful information about assemblies (including versions, referenced assemblies, not to mention disassembled code).

Mark Wilkins
  • 40,729
  • 5
  • 57
  • 110
1

I also ran into this problem and the reason it was happening was that the project, for some reason, was pulling the log4net from the GAC, so it is quite possible that the version in the GAC does not match the version referenced in your project

0

I was going to follow Matt's advice to enable 32-bit applications in the Application Pool settings of IIS.

Turns out I didn't even need to go that far, the error resolved as soon as I switched to IIS from Cassini.

To switch to IIS:

  1. Enable IIS in Windows Programs and Features (Make sure to also enable "ASP.NET x.x" node under IIS > World Wide Web Services > Application Development Features)
  2. In project Properties > Web > Use Local IIS Web Server > Create Virtual Directory (Must run VS as Admin to do this).
  3. Build project > Run

No log4net error for me after this.

I don't know why I was getting it because I don't even use log4net anywhere, but I'm glad it's gone.

parliament
  • 21,544
  • 38
  • 148
  • 238
0

After reading these answers I ended up checking the .csproj and finding the actual reference to 'Lib\log4net.dll' in the section. I deleted this and the project compiled.

pat capozzi
  • 1,412
  • 1
  • 20
  • 16