using log4net;
using MethodDecorator.Fody.Interfaces;
using Rougamo;
using Rougamo.Context;
using System.Reflection;
using System.Text;

namespace SparkClient.Model.Attributes
{
    [AttributeUsage(AttributeTargets.Method)]
    public class LogAttribute : MoAttribute
    {
        private static readonly ILog Logger = LogManager.GetLogger(typeof(LogAttribute));
        

        public override void OnEntry(MethodContext context)
        {
            //Logger.Debug($"Entering {context.Method.DeclaringType?.Name}.{context.Method.Name}");
            // 获取方法参数
            var parameters = context.Method.GetParameters();
            var arguments = context.Arguments;

            // 构建参数字符串
            var parameterLog = new StringBuilder();
            for (int i = 0; i < parameters.Length; i++)
            {
                parameterLog.Append($"{parameters[i].Name} = {arguments[i]}, ");
            }

            // 记录日志
            Logger.Debug($"Entering {context.Method.DeclaringType?.Name}.{context.Method.Name} with parameters: {parameterLog}");
        }

        public override void OnExit(MethodContext context)
        {
            //Logger.Debug($"Exiting {context.Method.DeclaringType?.Name}.{context.Method.Name}");
            // 获取返回值
            var returnValue = context.ReturnValue;

            // 记录日志
            Logger.Debug($"Exiting {context.Method.DeclaringType?.Name}.{context.Method.Name} with return value: {returnValue}");
        }
    }
}