1

我正在使用 OnMethodBoundryAspect 创建一个简单的日志记录和执行时间方面。我想为每个方法创建一个记录器。但是,如果 Logger 未声明为静态成员,则它不起作用。如果将其声明为静态,则不可能为每个方法创建一个记录器。

这是我的方面:

[Serializable]
public class MonitorAttribute : OnMethodBoundaryAspect
{
    [NonSerialized]
    private Stopwatch _stopwatch;

    private string _methodName;
    private ILog _log;

    public MonitorAttribute()
    {
    }

    /// <summary>
    /// overrides the method name in the logs
    /// </summary>
    /// <param name="method"></param>
    public MonitorAttribute(string method)
    {
        _methodName = method;
    }

    #region Overrides

    public override void CompileTimeInitialize(MethodBase method, AspectInfo aspectInfo)
    {
        if (string.IsNullOrEmpty(_methodName))
            _methodName = method.Name;

        _log = LogManager.GetLogger(_methodName);
    }

    public override void OnEntry(MethodExecutionArgs args)
    {
        _stopwatch = Stopwatch.StartNew();
        _log.InfoFormat("Method {0} called", _methodName);
    }

    public override void OnExit(MethodExecutionArgs args)
    {
        _stopwatch.Stop();
        _log.InfoFormat("Method {0} exited successfully. execution time {1} milliseconds", _methodName, _stopwatch.ElapsedMilliseconds);
    }

    public override void OnSuccess(MethodExecutionArgs args)
    {
        _stopwatch.Stop();
        _log.InfoFormat("Method {0} executed successfully. execution time {1} milliseconds", _methodName, _stopwatch.ElapsedMilliseconds);
    }

    #endregion
}
4

1 回答 1

2

初始化记录器实例的正确位置_logRuntimeInitialize方法。在您当前的示例中,您仅在编译期间创建记录器。

我还建议_stopwatch为每个初始化一个新实例OnEntry不是线程安全的,并且可能导致不正确的结果。

您可以使用静态秒表和MethodExecutionArgs.MethodExecutionTag属性将当前时间存储在OnEntry. 之后,此值将传递给您的OnSuccessOnExit方法。

因此修改后的示例可能如下所示:

[Serializable]
public class MonitorAttribute : OnMethodBoundaryAspect
{
    private static Stopwatch _stopwatch = Stopwatch.StartNew();

    [NonSerialized] private ILog _log;

    private string _methodName;

    public MonitorAttribute()
    {
    }

    /// <summary>
    /// overrides the method name in the logs
    /// </summary>
    /// <param name="method"></param>
    public MonitorAttribute(string method)
    {
        _methodName = method;
    }

    #region Overrides

    public override void CompileTimeInitialize(MethodBase method, AspectInfo aspectInfo)
    {
        if (string.IsNullOrEmpty(_methodName))
            _methodName = method.Name;
    }

    public override void RuntimeInitialize(MethodBase method)
    {
        _log = LogManager.GetLogger(_methodName);
    }

    public override void OnEntry(MethodExecutionArgs args)
    {
        _log.InfoFormat("Method {0} called", _methodName);
        args.MethodExecutionTag = _stopwatch.ElapsedMilliseconds;
    }

    public override void OnExit(MethodExecutionArgs args)
    {
        long stopwatchStart = (long) args.MethodExecutionTag;
        _log.InfoFormat("Method {0} exited successfully. execution time {1} milliseconds", _methodName,
            _stopwatch.ElapsedMilliseconds - stopwatchStart);
    }

    public override void OnSuccess(MethodExecutionArgs args)
    {
        long stopwatchStart = (long) args.MethodExecutionTag;
        _log.InfoFormat("Method {0} executed successfully. execution time {1} milliseconds", _methodName,
            _stopwatch.ElapsedMilliseconds - stopwatchStart);
    }

    #endregion
}
于 2013-09-05T15:57:46.577 回答