Search code examples
c#nlogpostsharp

Error when compiling c# project in VS2012 when using Postsharp


I am currently working on a project where we were wanting to add PostSharp functionality.

I have set up my Postsharp attribute as so

[Serializable]
public class NLogTraceAttribute : OnMethodBoundaryAspect
{
    private readonly string _logLevel;
    ILogger logger;

    public NLogTraceAttribute(string logLevel)
    {
        _logLevel = logLevel;

        logger = new Logger("TraceAttribute");
    }

    public override void OnEntry(MethodExecutionArgs args)
    {
        LogAction("Enter", args);
    }

    public override void OnExit(MethodExecutionArgs args)
    {
        LogAction("Leave", args);
    }

    private void LogAction(string action, MethodExecutionArgs args)
    {
        var argumentsInfo = args.GetArgumentsInfo();

        logger.Log(_logLevel, "{0}: {1}.{2}{3}", action, args.Method.DeclaringType.Name, args.Method.Name, argumentsInfo);
    }
}

and trying to use it as

    [NLogTrace(NLogLevel.Debug)]

However when compiling the project I am getting the following error:

    Error   26  Cannot serialize the aspects: Type 'NLog.Logger' in Assembly 
    'NLog, Version=2.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c' 
    is not marked as serializable.. 

Any help would be appreciated


Solution

  • [NonSerialized]
    public Logger logger;
    

    Declaring the logger as marked above has solved the problem I was having