Aneef
Aneef

Reputation: 3729

log4net and .net Framework 4.0

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

Upvotes: 3

Views: 10876

Answers (5)

Aneef
Aneef

Reputation: 3729

I Fixed it by adding [assembly: System.Security.SecurityRules(System.Security.SecurityRuleSet.Level1)] to log4net AssemblyInfo but now its not logging

Upvotes: 0

Khachatur
Khachatur

Reputation: 989

I have fixed the similar issue by simply updating the Log4Net package from Visual Studio package manager.

Thanks, Khachatur

Upvotes: 0

Björn
Björn

Reputation: 51

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.

Upvotes: 4

TechGeorgii
TechGeorgii

Reputation: 417

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

Upvotes: 3

Chris Ellingsworth
Chris Ellingsworth

Reputation: 85

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

Upvotes: 2

Related Questions