使用Postsharp时在VS2012中编译c#项目时出错

时间:2012-10-30 10:58:45

标签: c# nlog postsharp

我目前正在开展一个我们想要添加PostSharp功能的项目。

我已将Postsharp属性设置为

[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);
    }
}

并尝试将其用作

    [NLogTrace(NLogLevel.Debug)]

然而,在编译项目时,我收到以下错误:

    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.. 

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:5)

[NonSerialized]
public Logger logger;

声明上面标记的记录器已解决了我遇到的问题