コード例 #1
0
        /// <summary>
        /// Log response data
        /// </summary>
        /// <param name="context">Http context object</param>
        /// <param name="body"></param>
        /// <param name="ex">Exception data object</param>
        private void LogResponse(HttpContext context, string body, Exception ex)
        {
            if (_options.LogResponse)
            {
                AuditResponseInfo respInfo = new AuditResponseInfo()
                {
                    Id         = context.TraceIdentifier,
                    Headers    = context.Response.Headers.ToDictionary(k => k.Key, v => v.Value.ToString()),
                    StatusCode = context.Response.StatusCode,
                    Body       = body,
                    Exception  = ex
                };

                _logger.Log(ex == null ? LogLevel.Information : LogLevel.Error, _eventId, respInfo, ex, (i, e) => { return($"RESPONSE: {respInfo.StatusCode}-{(HttpStatusCode)respInfo.StatusCode}"); });
            }
        }
コード例 #2
0
ファイル: Logger.cs プロジェクト: rodriguesrm/rsoft-logs
        ///<inheritdoc/>
        public void Log <TState>
        (
            LogLevel logLevel,
            EventId eventId,
            TState state,
            Exception exception,
            Func <TState, Exception, string> formatter = null)
        {
            if ((this as ILogger).IsEnabled(logLevel))
            {
                LogEntry info = new LogEntry()
                {
                    Category        = _category,
                    Level           = logLevel,
                    Text            = exception?.Message ?? state.ToString(),
                    Exception       = exception != null ? new LogExceptionInfo(exception) : null,
                    EventId         = eventId,
                    ApplicationUser = GetSignedUserInformation()
                };

                if (state is string)
                {
                    info.Scopes.Add("Text", state.ToString());
                }
                else if (state is AuditRequestInfo || state is AuditGrpcRequestInfo)
                {
                    AuditRequestInfo auditRequest = state as AuditRequestInfo;

                    info.Text = formatter?.Invoke(state, exception) ?? info.Text;
                    if (auditRequest.Body != null && info.Text.Contains(auditRequest.Body))
                    {
                        auditRequest.Body = null;
                    }

                    info.Scopes.Add("Date", auditRequest.Date.ToString("u"));
                    info.Scopes.Add("Scheme", auditRequest.Scheme);

                    if (auditRequest.Headers?.Count > 0)
                    {
                        foreach (var header in auditRequest.Headers)
                        {
                            info.Scopes.Add($"Headers.{header.Key}", header.Value.EscapeForJson()); //.ToEscapedString());
                        }
                    }

                    info.Scopes.Add("Method", auditRequest.Method);
                    info.Scopes.Add("Host", auditRequest.Host);
                    if (!string.IsNullOrWhiteSpace(auditRequest.QueryString))
                    {
                        info.Scopes.Add("QueryString", auditRequest.QueryString);
                    }
                    info.Scopes.Add("ClientCertificate", auditRequest.ClientCertificate);
                    info.Scopes.Add("LocalIpAddress", auditRequest.LocalIpAddress);
                    info.Scopes.Add("LocalPort", auditRequest.LocalPort.ToString());
                    info.Scopes.Add("RemoteIpAddress", auditRequest.RemoteIpAddress);
                    info.Scopes.Add("RemotePort", auditRequest.RemotePort.ToString());
                    info.Scopes.Add("RawUrl", auditRequest.RawUrl);

                    if (!string.IsNullOrWhiteSpace(auditRequest.Body))
                    {
                        if (state is AuditGrpcRequestInfo)
                        {
                            info.Scopes.Add("Body", auditRequest.Body);
                        }
                        else
                        {
                            info.Scopes.Add("Body", auditRequest.Body.AsJson());
                        }
                    }
                }
                else if (state is AuditResponseInfo || state is AuditGrpcResponseInfo)
                {
                    AuditResponseInfo auditResponse = state as AuditResponseInfo;

                    info.Text = formatter?.Invoke(state, exception) ?? info.Text;
                    if (auditResponse.Body != null && info.Text.Contains(auditResponse.Body))
                    {
                        auditResponse.Body = null;
                    }

                    info.Scopes.Add("StatusCode", auditResponse.StatusCode.ToString());
                    info.Scopes.Add("Date", auditResponse.Date.ToString("u"));

                    if (auditResponse.Headers?.Count > 0)
                    {
                        foreach (var header in auditResponse.Headers)
                        {
                            info.Scopes.Add($"Headers.{header.Key}", header.Value.EscapeForJson());
                        }
                    }

                    if (!string.IsNullOrWhiteSpace(auditResponse.Body))
                    {
                        if (state is AuditGrpcResponseInfo)
                        {
                            info.Scopes.Add("Body", auditResponse.Body.ToString());
                        }
                        else
                        {
                            info.Scopes.Add("Body", auditResponse.Body.AsJson());
                        }
                    }
                }
                else if (state is IEnumerable <KeyValuePair <string, object> > properties)
                {
                    info.Text = formatter?.Invoke(state, exception) ?? info.Text;

                    foreach (KeyValuePair <string, object> item in properties)
                    {
                        if (item.Key != "{OriginalFormat}")
                        {
                            if (item.Value is string)
                            {
                                info.Scopes[item.Key] = item.Value.ToString();
                            }
                            else if (item.Value?.GetType()?.Name == "RuntimeMethodInfo")
                            {
                                info.Scopes["MethodInfo.Module"]        = (item.Value as MethodInfo).Module.Name;
                                info.Scopes["MethodInfo.DeclaringType"] = (item.Value as MethodInfo).DeclaringType.Name;
                                info.Scopes["MethodInfo.Name"]          = (item.Value as MethodInfo).Name;
                            }
                            else
                            {
                                info.Scopes[item.Key] = item.Value.AsJson();
                            }
                        }
                    }
                }

                if (_provider.ScopeProvider != null)
                {
                    _provider.ScopeProvider.ForEachScope((value, loggingProps) =>
                    {
                        if (value is string)
                        {
                            info.Scopes["Text"] = value.ToString();
                        }
                        else if (value is IEnumerable <KeyValuePair <string, object> > props)
                        {
                            foreach (KeyValuePair <string, object> pair in props)
                            {
                                string pairValue = null;
                                if (pair.Value is string)
                                {
                                    pairValue = pair.Value.ToString();
                                }
                                else
                                {
                                    pairValue = pair.Value.AsJson();
                                }

                                if (!string.IsNullOrWhiteSpace(pairValue))
                                {
                                    info.Scopes[pair.Key] = pairValue;
                                }
                            }
                        }
                    }, state);

                    info.Scopes["ApplicationName"]    = Assembly.GetEntryAssembly().GetName().Name;
                    info.Scopes["ApplicationVersion"] = Assembly.GetEntryAssembly().GetName().Version.ToString();
                    string environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT");
                    info.Scopes["Environment"] = environment;
                }

                _provider.WriteLog(info);
            }
        }