//using System.Diagnostics;
//using IRaCIS.Application.Interfaces;
//using IRaCIS.Application.Contracts;
//using IRaCIS.Core.Infra.EFCore;
//using IRaCIS.Core.Infrastructure.Extention;
//using Microsoft.AspNetCore.Mvc;
//using Microsoft.AspNetCore.Mvc.Filters;
//using Microsoft.Extensions.Logging;
//using Newtonsoft.Json;

//namespace IRaCIS.Core.Application.Filter
//{

//    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true)]
//    public class LogFilter : Attribute
//    {
//    }
//    public class LogActionFilter : IAsyncActionFilter
//    {
//        private readonly ILogService _logService;
//        private readonly IUserInfo _userInfo;
//        private readonly ILogger<LogActionFilter> _logger;

//        public LogActionFilter(ILogService logService, IUserInfo userInfo , ILogger<LogActionFilter> logger)
//        {
//            _logService = logService;
//            _userInfo = userInfo;
//            _logger = logger;
//        }

//        public Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
//        {

//            if (context.ActionDescriptor.EndpointMetadata!=null&& context.ActionDescriptor.EndpointMetadata.Any(m => m.GetType() == typeof(LogFilter)))
//            {
//                return LogAsync(context, next);
//            }
//            return next();
//        }

//        public async Task LogAsync(ActionExecutingContext context, ActionExecutionDelegate next)
//        {
//            var sw = new Stopwatch();
//            sw.Start();

//            dynamic actionResult = (await next()).Result;
//            sw.Stop();
//            var args = JsonConvert.SerializeObject(context.ActionArguments);
//            var result = JsonConvert.SerializeObject(actionResult?.Value);

//            var attr = (ApiExplorerSettingsAttribute)context.ActionDescriptor.EndpointMetadata.FirstOrDefault(m => m.GetType() == typeof(ApiExplorerSettingsAttribute));
//            var groupName = attr?.GroupName;
//            var res = actionResult?.Value as IResponseOutput;
//            var input = new SystemLogDTO
//            {
//                ClientIP = string.Empty,
//                OptUserId = _userInfo.Id,
//                OptUserName = _userInfo.UserName,
//                ApiPath = context.ActionDescriptor.AttributeRouteInfo.Template.ToLower(),
//                Params = args,
//                Result = result,
//                RequestTime = DateTime.Now,
//                ElapsedMilliseconds = sw.ElapsedMilliseconds,
//                Status =res?.IsSuccess?? false,
//                Message = res?.ErrorMessage,
//                LogCategory = groupName
//            };

//            try
//            {
//                _logService.SaveLog2Db(input);
//            }
//            catch (Exception ex)
//            {

//                _logger.LogError(ex.Message);
//            }
            
//        }
//    }
//}
