Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I am getting "Cannot serialized the aspects: Type 'log4net.Core.LogImpl' in assembly 'log4net...'" -how can I make it serializable?

Environment

  • Windows 7 x64
  • PostSharp 2.1.7.30 installed
  • PostSharp 2.1.7.29 (reference retrieved from the PostSharp install directory)
  • log4net 1.2.11.0 (reference retrieved from the net\2.0\release directory of the log4net binaries download)
  • ASP.NET 2.0 application
  • Visual Studio 2010 SP1

Aspect

[Serializable]
public class MethodBoundaryAspect : PostSharp.Aspects.OnMethodBoundaryAspect
{
    ILog _logger = LogManager.GetLogger("MethodBoundaryAspect");

    public override void OnEntry(PostSharp.Aspects.MethodExecutionArgs args)
    {
        _logger.DebugFormat("The user {0} entered the method {1} at {2}.",
            HttpContext.Current.Profile.UserName,
            args.Method.Name,
            DateTime.Now);

        base.OnEntry(args);
    }

    public override void OnExit(PostSharp.Aspects.MethodExecutionArgs args)
    {
        _logger.DebugFormat("The user {0} exited the method {1} at {2}.",
            HttpContext.Current.Profile.UserName,
            args.Method.Name,
            DateTime.Now);

        base.OnExit(args);
    }

    public override void OnException(PostSharp.Aspects.MethodExecutionArgs args)
    {
        _logger.DebugFormat("The user {0} was executing the method {1} when an unhandled exception occurred at {2}.{3}{4}",
            HttpContext.Current.Profile.UserName,
            args.Method.Name,
            DateTime.Now,
            Environment.NewLine,
            args.Exception.ToString());

        base.OnException(args);
    }
}

Compile Error

Cannot serialize the aspects: Type 'log4net.Core.LogImpl' in Assembly 'log4net, Version=1.2.11.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a' is not marked as serializable.

How can I make the log4net.Core.LogImpl serializable so that I can compile this solution?

like image 641
Mike Perrenoud Avatar asked Apr 26 '13 14:04

Mike Perrenoud


1 Answers

Based on the comments from Grant and mtsiakiris I solved this problem by adding the [NonSerialized] attribute to the field like this:

[NonSerialized]
ILog _logger = LogManager.GetLogger("MethodBoundaryAspect");

which then just ignored it during serialization. The reason this occurs is because PostSharp needs to serialize the attribute during its IL generation.

like image 148
Mike Perrenoud Avatar answered Sep 23 '22 21:09

Mike Perrenoud