using EntrustSettle.Common.Helper; using Microsoft.Extensions.Logging; //using NLog; using RestSharp; using System; using System.Linq; namespace EntrustSettle.Common { public static class RestSharpLogHelper { public static void LogRestSharp(this ILogger logger, string title, IRestClient client, RestRequest request, RestResponse response) { var requestToLog = new { resource = request.Resource, // Parameters are custom anonymous objects in order to have the parameter type as a nice string // otherwise it will just show the enum value parameters = request.Parameters.Select(parameter => new { name = parameter.Name, value = parameter.Value, type = parameter.Type.ToString() }), // ToString() here to have the method as a nice string otherwise it will just show the enum value method = request.Method.ToString(), // This will generate the actual Uri used in the request uri = client.BuildUri(request), }; var responseToLog = new { statusCode = response.StatusCode, content = response.Content, headers = response.Headers, // The Uri that actually responded (could be different from the requestUri if a redirection occurred) responseUri = response.ResponseUri, errorMessage = response.ErrorMessage, }; logger.LogInformation($"Http请求目标:{title}{Environment.NewLine}Request:{{Request}}{Environment.NewLine}Response:{{Response}}", JsonHelper.ObjToJson(requestToLog), JsonHelper.ObjToJson(responseToLog)); } } }