问题
I was having issues in log4net when I updated my solutions to .net 4.0 , and then I downloaded the source of it and built log4net and targeted it to .net 4.0 and used it in my projects.
Initially when I referred log4net that is targeted to run time 2.0 it complied and run the application but log did not work.
Now when I run my project with log4net targeted to .net 4.0 I get the error "The type initializer for 'Log4NetTest.TestLog' threw an exception."
Any Idea how to solve this?
Edit: this is the inner exception:
InnerException: System.TypeLoadException Message=Inheritance security rules violated while overriding member: 'log4net.Util.ReadOnlyPropertiesDictionary.GetObjectData(System.Runtime.Serialization.SerializationInfo, System.Runtime.Serialization.StreamingContext)'. Security accessibility of the overriding method must match the security accessibility of the method being overriden. Source=log4net TypeName=log4net.Util.ReadOnlyPropertiesDictionary.GetObjectData(System.Runtime.Serialization.SerializationInfo, System.Runtime.Serialization.StreamingContext) StackTrace: at log4net.Repository.Hierarchy.Hierarchy..ctor(ILoggerFactory loggerFactory) at log4net.Repository.Hierarchy.Hierarchy..ctor() in C:\src\Repository\Hierarchy\Hierarchy.cs:line 150
回答1:
In the log4net source code, add a [SecurityCritical] attribute to the Util.ReadOnlyPropertiesDictionary.GetObjectData method and build.
Just compiled the source for target framework 4.0 (not client). Source code of version 1.2.11 already contains this attribute, but be sure to use 'NET_4_0' as a compiler argument. Otherwise, this attribute will not be added.
回答2:
I've found a compiled version of log4net for .NET client profile here:
http://gosheg.blogspot.com/2011/04/log4net-in-net-40-c-applications-net.html
回答3:
This worked for me:
In the log4net source code, add a [SecurityCritical] attribute to the Util.ReadOnlyPropertiesDictionary.GetObjectData method and build.
There might be some other things you'll need to do to get it to build for the 4.0 framework.
See this post:
Weird override problem with Fluent NHibernate and .NET 4
回答4:
I have fixed the similar issue by simply updating the Log4Net package from Visual Studio package manager.
Thanks, Khachatur
回答5:
I Fixed it by adding [assembly: System.Security.SecurityRules(System.Security.SecurityRuleSet.Level1)]
to log4net AssemblyInfo but now its not logging
来源:https://stackoverflow.com/questions/2903669/log4net-and-net-framework-4-0