How do I add arguments to PostSharp attributes?

后端 未结 1 1037
不思量自难忘°
不思量自难忘° 2021-02-19 05:40

I have a simple PostSharp logging attribute:

[Serializable]
public class MethodLoggingAttribute : OnMethodBoundaryAspect
{
    private ILog _logger;
    public o         


        
相关标签:
1条回答
  • 2021-02-19 06:20

    Just declare a property

    [Serializable]
    public class MethodLoggingAttribute : OnMethodBoundaryAspect
    {
        private ILog _logger;
        public override void OnEntry(MethodExecutionEventArgs eventArgs)
        {
            _logger = LogManager.GetLogger(eventArgs.Method.DeclaringType.ToString());
            if(ShowParameters = true)
            {
                _logger.DebugFormat("Entered {0} with args:{1}", eventArgs.Method.Name, args);
            }
            else
            {
                _logger.DebugFormat("Entered {0}", eventArgs.Method.Name);
            }
        }
    
        private bool m_ShowParameters;
    
        public bool ShowParameters
        {
            get { return m_ShowParameters; }
            set { m_ShowParameters = value; }
        }
    }
    

    Then you can specify it in the way you have mentioned.

    0 讨论(0)
提交回复
热议问题