且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

log4net 在.NET4.0下使用

更新时间:2022-09-16 19:07:53


在.NET 4.0下使用最新版本的log4Net 1.2.10,会遇到下面这样的错误:

重写成员“log4net.Util.ReadOnlyPropertiesDictionary.GetObjectData(System.Runtime.Serialization.SerializationInfo,

System.Runtime.Serialization.StreamingContext)”时违反了继承安全性规则。重写方法的安全可访问性必须与所重写方法的安全可访问性

匹配。 
说明: 执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.TypeLoadException: 重写成员“log4net.Util.ReadOnlyPropertiesDictionary.GetObjectData

(System.Runtime.Serialization.SerializationInfo, System.Runtime.Serialization.StreamingContext)”时违反了继承安全性规则。重写

方法的安全可访问性必须与所重写方法的安全可访问性匹配。

源错误:


行 10:     public class CustomerController : Controller
行 11:     {
行 12:         private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(CustomerController));

 
经跟踪源码,要解决这个错误,应对log4net源码做如下修改:

1、使用NET_20预编译常量编译log4net项目,目标Runtime改为.NET 4.0 framework;

2、确保log4net.Util.SystemInfo.CurrentThreadId返回的是System.Threading.Thread.CurrentThread.ManagedThreadId;

3、注释掉AssemblyInfo.cs中的代码行: [assembly: System.Security.AllowPartiallyTrustedCallers] 或者改为[assembly:SecurityRules(SecurityRuleSet.Level1)]

4、更改XmlConfigurator.cs 中的settings.ProhibitDtd = false; 为settings.DtdProcessing = DtdProcessing.Parse;


本文转自海天一鸥博客园博客,原文链接:http://www.cnblogs.com/sgsoft/archive/2011/03/22/1991976.html,如需转载请自行联系原作者