Ejemplo n.º 1
0
        private void LogRequestAndResponse(IRestResponse response, Stopwatch stopwatch)
        {
            if (Configuration.LoggerConfiguration != null)
            {
                Log.Logger = Configuration.LoggerConfiguration.CreateLogger();
            }

            var uri        = BuildUri(response.Request);
            var properties = new Dictionary <string, object>();

            if (AdditionalProperties?.Any() == true)
            {
                foreach (var item in AdditionalProperties)
                {
                    properties.Add(item.Key, item.Value);
                }
            }

            properties.Add("Agent", "RestSharp");
            properties.Add("ElapsedMilliseconds", stopwatch.ElapsedMilliseconds);
            properties.Add("Method", response.Request.Method.ToString());
            properties.Add("Url", uri.AbsoluteUri);
            properties.Add("Host", uri.Host);
            properties.Add("Path", uri.AbsolutePath);
            properties.Add("Port", uri.Port);
            properties.Add("QueryString", uri.Query);
            properties.Add("Query", GetRequestQueryStringAsObject(response.Request));
            properties.Add("RequestBody", GetRequestBody(response.Request));
            properties.Add("RequestHeaders", GetRequestHeaders(response.Request));
            properties.Add("StatusCode", (int)response.StatusCode);
            properties.Add("StatusCodeFamily", ((int)response.StatusCode).ToString()[0] + "XX");
            properties.Add("StatusDescription", response.StatusDescription?.Replace(" ", ""));
            properties.Add("ResponseStatus", response.ResponseStatus.ToString());
            properties.Add("ProtocolVersion", response.ProtocolVersion);
            properties.Add("IsSuccessful", response.IsSuccessful);
            properties.Add("ErrorMessage", response.ErrorMessage);
            properties.Add("ErrorException", GetResponseException(response.ErrorException));
            properties.Add("ResponseContent", GetResponseContent(response));
            properties.Add("ContentLength", response.ContentLength);
            properties.Add("ContentType", response.ContentType);
            properties.Add("ResponseHeaders", GetResponseHeaders(response));
            properties.Add("Environment", Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"));

            using (LogContext.Push(new RestClientAutologEnricher(properties,
                                                                 GetIgnoredProperties(response.Request), Configuration.PropertiesToDestructure)))
            {
                if (response.IsSuccessful)
                {
                    Log.Information(Configuration.MessageTemplateForSuccess);
                }
                else
                {
                    Log.Error(Configuration.MessageTemplateForError);
                }
            }
        }