Exemple #1
0
        public void Log(LogMethodParameter logMethodParameter)
        {
            _logService = InstanceFactory.GetInstance <ILogService>();


            if (logMethodParameter == null)
            {
                throw new Exception("Log method parameter cannot be null");
            }

            if (string.IsNullOrWhiteSpace(logMethodParameter.Message))
            {
                logMethodParameter.Message = JsonConvert.SerializeObject(logMethodParameter.LogDetail, Formatting.Indented);
            }

            try
            {
                Log log = new Log
                {
                    CreateDate = DateTime.Now,
                    LogName    = logMethodParameter.LogName,
                    LogDetail  = logMethodParameter.Message
                };

                _logService.Add(log);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }
 public void Log(LogMethodParameter logMethodParameter)
 {
     if (IsInfoEnabled)
     {
         _log.Info(logMethodParameter);
     }
 }
        public void OnException(ExceptionMethodArgs exceptionMethodArgs)
        {
            ParameterInfo[]           parameterInfos = exceptionMethodArgs.MethodInfo.GetParameters();
            List <LogDetailParameter> logParameters  = new List <LogDetailParameter>();

            for (int i = 0; i < exceptionMethodArgs.Arguments.Length; i++)
            {
                if (exceptionMethodArgs.Arguments[i] != null)
                {
                    var parameter = exceptionMethodArgs.Arguments[i].GetType().BaseType.BaseType == typeof(System.Linq.Expressions.LambdaExpression)
                                ? exceptionMethodArgs.Arguments[i].ToString()
                                : exceptionMethodArgs.Arguments[i];

                    logParameters.Add(new LogDetailParameter
                    {
                        Name  = parameterInfos[i].Name,
                        Type  = parameterInfos[i].ParameterType.Name,
                        Value = parameter
                    });
                }
                else
                {
                    logParameters.Add(new LogDetailParameter
                    {
                        Name  = parameterInfos[i].Name,
                        Type  = parameterInfos[i].ParameterType.Name,
                        Value = null
                    });
                }
            }

            LogDetail logDetail = new LogDetail
            {
                MethodCallDate = DateTime.Now,
                ClassName      = exceptionMethodArgs.MethodInfo.DeclaringType.FullName,
                MethodName     = exceptionMethodArgs.MethodInfo.Name,
                Parameters     = logParameters,
                Message        = exceptionMethodArgs.Exception.Message,
                InnerException = exceptionMethodArgs.Exception.InnerException == null ? "" : exceptionMethodArgs.Exception.InnerException.Message,
                StackStrace    = exceptionMethodArgs.Exception.StackTrace
            };

            var jsonResult = JsonConvert.SerializeObject(logDetail, Formatting.Indented);
            LogMethodParameter logMethodParameter = new LogMethodParameter()
            {
                LogName = $"{exceptionMethodArgs.MethodInfo.Name}_err",
                Message = jsonResult
            };

            _logger.Log(logMethodParameter);
        }
        public void OnBefore(BeforeMethodArgs beforeMethodArgs)
        {
            LogMethodParameter logMethodParameter = null;

            ParameterInfo[]           parameterInfos = beforeMethodArgs.MethodInfo.GetParameters();
            List <LogDetailParameter> logParameters  = new List <LogDetailParameter>();

            for (int i = 0; i < beforeMethodArgs.Arguments.Length; i++)
            {
                if (beforeMethodArgs.Arguments[i] != null)
                {
                    var parameter = beforeMethodArgs.Arguments[i].GetType().BaseType.BaseType == typeof(System.Linq.Expressions.LambdaExpression)
                    ? beforeMethodArgs.Arguments[i].ToString()
                    : beforeMethodArgs.Arguments[i];

                    logParameters.Add(new LogDetailParameter
                    {
                        Name  = parameterInfos[i].Name,
                        Type  = parameterInfos[i].ParameterType.Name,
                        Value = parameter
                    });
                }
                else
                {
                    logParameters.Add(new LogDetailParameter
                    {
                        Name  = parameterInfos[i].Name,
                        Type  = parameterInfos[i].ParameterType.Name,
                        Value = null
                    });
                }
            }

            LogDetail logDetail = new LogDetail
            {
                MethodCallDate = DateTime.Now,
                ClassName      = beforeMethodArgs.MethodInfo.DeclaringType.FullName,
                MethodName     = beforeMethodArgs.MethodInfo.Name,
                Parameters     = logParameters
            };


            logMethodParameter = new LogMethodParameter()
            {
                LogName   = beforeMethodArgs.MethodInfo.Name,
                LogDetail = logDetail
            };

            _logger.Log(logMethodParameter);
        }
Exemple #5
0
        public void Log(LogMethodParameter logMethodParameter)
        {
            var    mainPath = AppDomain.CurrentDomain.BaseDirectory + "/Log";
            string year     = DateTime.Now.Year.ToString();
            string month    = DateTime.Now.Month.ToString();
            string day      = DateTime.Now.Day.ToString();
            string hour     = DateTime.Now.Hour.ToString();
            string logName  = $"{year}{month}{day}";

            if (logMethodParameter == null)
            {
                throw new Exception("Log method parameter cannot be null");
            }
            if (!string.IsNullOrWhiteSpace(logMethodParameter.LogName))
            {
                logName = logMethodParameter.LogName;
            }
            if (string.IsNullOrWhiteSpace(logMethodParameter.Message))
            {
                logMethodParameter.Message = JsonConvert.SerializeObject(logMethodParameter.LogDetail, Formatting.Indented);
            }

            try
            {
                string fullPath = $"{mainPath}/{year}/{month}/{day}";
                if (!string.IsNullOrWhiteSpace(logMethodParameter.Path))
                {
                    fullPath = logMethodParameter.Path;
                }
                if (!Directory.Exists(fullPath))
                {
                    Directory.CreateDirectory(fullPath);
                }

                string       filePath = $"{fullPath}/{logName}_{hour}.json";
                FileStream   fs       = new FileStream(filePath, FileMode.Append, FileAccess.Write);
                StreamWriter sw       = new StreamWriter(fs);
                sw.WriteLine("/*------------------ START " + DateTime.Now.ToString() + "------------------*/");
                sw.WriteLine(logMethodParameter.Message);
                sw.WriteLine("/*------------------ END " + DateTime.Now.ToString() + "------------------*/");
                sw.WriteLine("");
                sw.Flush();
                sw.Close();
                fs.Close();
            }
            catch
            {
            }
        }
        public void OnAfter(AfterMethodArgs afterMethodArgs)
        {
            LogMethodParameter logMethodParameter = null;

            ParameterInfo[]           parameterInfos = afterMethodArgs.MethodInfo.GetParameters();
            List <LogDetailParameter> logParameters  = new List <LogDetailParameter>();

            for (int i = 0; i < afterMethodArgs.Arguments.Length; i++)
            {
                if (afterMethodArgs.Arguments[i] != null)
                {
                    var parameter = afterMethodArgs.Arguments[i].GetType().BaseType.BaseType == typeof(System.Linq.Expressions.LambdaExpression)
                                    ? afterMethodArgs.Arguments[i].ToString()
                                    : afterMethodArgs.Arguments[i];

                    logParameters.Add(new LogDetailParameter
                    {
                        Name  = parameterInfos[i].Name,
                        Type  = parameterInfos[i].ParameterType.Name,
                        Value = parameter
                    });
                }
                else
                {
                    logParameters.Add(new LogDetailParameter
                    {
                        Name  = parameterInfos[i].Name,
                        Type  = parameterInfos[i].ParameterType.Name,
                        Value = null
                    });
                }
            }

            LogDetail logDetail = new LogDetail
            {
                ClassName  = afterMethodArgs.MethodInfo.DeclaringType.FullName,
                MethodName = afterMethodArgs.MethodInfo.Name,
                Parameters = logParameters,
                Result     = afterMethodArgs.Value
            };

            logMethodParameter = new LogMethodParameter()
            {
                LogName   = afterMethodArgs.MethodInfo.Name,
                LogDetail = logDetail
            };

            _logger.Log(logMethodParameter);
        }