3

我目前正在开发一个我们想要添加 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.. 

任何帮助,将不胜感激

4

1 回答 1

5
[NonSerialized]
public Logger logger;

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

于 2012-11-01T12:10:04.317 回答