C# 无法加载文件或程序集“log4net”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/768903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Could not load file or assembly 'log4net
提问by Sheff
Ok I am having a very odd issue when deploying one of our web applications to our live servers.
好的,在将我们的一个 Web 应用程序部署到我们的实时服务器时,我遇到了一个非常奇怪的问题。
Our application uses log4net to log a lot of actions quite heavily and after a couple of hours after being deployed we get the following exception.
我们的应用程序使用 log4net 来记录大量操作,在部署后几个小时后,我们得到以下异常。
Could not load file or assembly 'log4net, Version=1.2.9.0, Culture=neutral, PublicKeyToken=b32731d11ce58905' or one of its dependencies. Access is denied.
无法加载文件或程序集“log4net,版本=1.2.9.0,文化=中性,PublicKeyToken=b32731d11ce58905”或其依赖项之一。访问被拒绝。
Here are the relevant web.config additions I use.
这是我使用的相关 web.config 添加项。
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net>
<appender name="ErrorLogFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="D:\SomeLocation\Errorlog"/>
<param name="AppendToFile" value="true"/>
<param name="RollingStyle" value="Size"/>
<param name="maxSizeRollBackups" value="-1" />
<param name="maximumFileSize" value="100MB" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n"/>
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="FATAL" />
<param name="LevelMax" value="FATAL" />
</filter>
</appender>
<appender name="BookingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="D:\SomeLocation\BookingInfoLog"/>
<param name="AppendToFile" value="true"/>
<param name="RollingStyle" value="Size"/>
<param name="maxSizeRollBackups" value="-1" />
<param name="maximumFileSize" value="100MB" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n"/>
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="INFO" />
<param name="LevelMax" value="FATAL" />
</filter>
</appender>
<root>
<level value="ALL"/>
<appender-ref ref="BookingLogFileAppender"/>
<appender-ref ref="ErrorLogFileAppender"/>
</root>
</log4net>
It is working fine on our local machines an dev server but just not on the live.
它在我们的本地机器和开发服务器上运行良好,但不能在现场运行。
Incidentally it seems to break after the JIT compiler runs on the server after a web.config update or an app pool recycle, or even just a long period of time!
顺便说一句,在 web.config 更新或应用程序池回收后,甚至只是很长一段时间后,JIT 编译器在服务器上运行后似乎会中断!
UPDATE:Our live servers run all websites from a share location. I managed to replicate this issue on another server running the application from a share. When setting the site to run from a local drive the issue goes away but our hosting requires us to run from a share. Any ideas why log4net would have permissions issues running from a share. Again the app runs first time until another JIT compilation occurs
更新:我们的实时服务器从共享位置运行所有网站。我设法在另一台从共享运行应用程序的服务器上复制了这个问题。将站点设置为从本地驱动器运行时,问题会消失,但我们的托管要求我们从共享运行。任何想法为什么 log4net 会出现从共享运行的权限问题。应用程序再次运行,直到发生另一个 JIT 编译
回答by Patrick Peters
Check the version you bind in compile time (your file reference) and the version that is used runtime (first look in the GAC, then local)
检查您在编译时绑定的版本(您的文件引用)和运行时使用的版本(首先查看 GAC,然后查看本地)
Usually the version in the GAC differs from the version you have as file reference. Please check this.
通常,GAC 中的版本与您作为文件参考的版本不同。请检查这个。
Also cleanup the ASP.NET temp dir and execute IIS restart (cmd prompt -> iisreset)
还要清理 ASP.NET 临时目录并执行 IIS 重新启动(cmd 提示 -> iisreset)
回答by Mark Broadhurst
Probally asking the obvious but I'm assuming the log4net.dll in your bin folder is the correct version ?
可能会问明显的问题,但我假设您的 bin 文件夹中的 log4net.dll 是正确的版本?
if you have more than one project which references differnt versions of the same DLL its not uncommon for the later project to copy the DLL over the previous version.
如果您有多个项目引用同一个 DLL 的不同版本,则后一个项目将 DLL 复制到前一版本的情况并不少见。
Assuming that you have more than one copy of log4net needing to be supported the best solution I can think of is to add probing to you config and put this version in another folder.
假设您有多个需要支持的 log4net 副本,我能想到的最佳解决方案是向您的配置添加探测并将此版本放在另一个文件夹中。
回答by Matt Palmerlee
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.
当我们转移到 VS 2010 和 .NET 4.0 时,我们也遇到了这个问题,我们根本不使用 log4net,但我怀疑我们使用的其他东西(也许是 Crystal Reports?),我还怀疑我们使用了一个 dll也是一个 32 位 dll,因为当我将 IIS 中应用程序池的高级设置下的“启用 32 位应用程序”选项更改为“真”时,一切又恢复正常。
回答by Gyuri
I bet your JIT compiler has no write access to the share. It seems like the JIT compiler recompiles the whole site from time to time, maybe when it gets recycled.
我敢打赌,您的 JIT 编译器没有对该共享的写访问权限。似乎 JIT 编译器不时重新编译整个站点,也许是在它被回收时。
One options is to deploy the site prebuilt. That way there is no recompilation. The other option is that you give write access to the JIT complier to your share. Chances are it's the same user as the one that runs your web service, eg. SYSTEM, NETWORK SERVICE or LOCAL SERVICE groups or maybe one of the IIS_* users...
一种选择是部署预先构建的站点。这样就没有重新编译。另一种选择是您将 JIT 编译器的写访问权限授予您的共享。它可能与运行您的 Web 服务的用户相同,例如。SYSTEM、NETWORK SERVICE 或 LOCAL SERVICE 组或 IIS_* 用户之一...
回答by Luis Gouveia
I had the same problem and if you use GIT this might help you.
我遇到了同样的问题,如果您使用 GIT,这可能会对您有所帮助。
The problem turned out to be that I had my Log4net.dll file inside a folder called log. The .gitignore configuration file excludes (by default) all folders called log.
问题原来是我的 Log4net.dll 文件位于一个名为log的文件夹中。.gitignore 配置文件排除(默认情况下)所有名为 log 的文件夹。

