3

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
adrianbanks
  • 81,306
  • 22
  • 176
  • 206
Aneef
  • 3,641
  • 10
  • 43
  • 67

5 Answers5

4

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.

Björn
  • 51
  • 2
  • 1
    The project that I downloaded from Apache.org had NET_4_0 defined in Debug configuration only. Had to add it to Release manually. – myforums May 21 '12 at 18:56
  • 1
    I have just added NET_4_0 for release configuration in 1.2.11 and it works! Thanks – sergtk Nov 07 '12 at 22:44
3

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

TechGeorgii
  • 414
  • 4
  • 14
  • Thanks! saved me reading all those blogs explaining how to download source and tweak like they did, instead of simply adding a `download here` on the top of page :) – Hertzel Guinness Apr 18 '11 at 05:21
2

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

Community
  • 1
  • 1
0

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

Thanks, Khachatur

Khachatur
  • 921
  • 1
  • 12
  • 30
0

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

Aneef
  • 3,641
  • 10
  • 43
  • 67