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
            {
                ClassName  = beforeMethodArgs.MethodInfo.DeclaringType.FullName,
                MethodName = beforeMethodArgs.MethodInfo.Name,
                Parameters = logParameters
            };


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

            _logger.Log(logMethodParameter);
        }
Exemplo n.º 2
0
        protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            if (_commonLogger != null && request != null)
            {
                string content = await request.Content.ReadAsStringAsync();

                string message =
                    $"HTTP^ request (v{request.Version}): [{request.Method.Method}]{request.RequestUri} content: {content}";
                var msgBuilder = new StringBuilder(message);

                if (request.Headers != null)
                {
                    foreach (var header in request.Headers)
                    {
                        if (header.Value != null)
                        {
                            msgBuilder.AppendFormat("\n{0} = {1}", header.Key, string.Join(", ", header.Value));
                        }
                    }
                }

                message = msgBuilder.ToString();
                _commonLogger.Log(LogLevel.Debug, message: message);
            }

            return(await base.SendAsync(request, cancellationToken)
                   .ContinueWith(task =>
            {
                HttpResponseMessage response = task.Result;

                if (_commonLogger != null && response != null)
                {
                    string content = "NotImplementedException";     //response.Content.ReadAsStringAsync().Result;
                    string message =
                        $"HTTP response (v{response.Version}): Status code: {response.StatusCode}, Reason phrase: {response.ReasonPhrase}, content: {content}";
                    _commonLogger.Log(LogLevel.Debug, message: message);
                }

                return response;
            }));
        }
        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 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
            {
                MethodCallDate = DateTime.Now,
                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);
        }
Exemplo n.º 5
0
 public override void Log(ExceptionLoggerContext context)
 {
     _logger.Log(logLevel: LogLevel.Fatal, exception: context.Exception, message: RequestToString(context.Request));
 }