Reputation: 21
I track request/response log in middleware of Api gateway .Net5, I can able to read Request.Body, However, I do not understand the body of the response, it show like below
`� <�_O�0G��r_����%>�"A�8��[��Im���
�w7n��{rO��/�կZ~y����36������rNIJ��d�w��\�'�����|w-�!�OR#Q�fi�X�� G��Sg�`��X@��{��KE�k�T3Zj%5N�@���+��SY��[�C(w� �·��@"�E���UnM�}��Jc�k�����F����#��$q�O(�.tÃ�c�ӕW���� �� ID�P`
Here I share my read response code
string responseBody = string.Empty;
var originalBodyStream = context?.Response.Body;
//Create a new memory stream...
using (var responseStream = new MemoryStream())
{
context.Response.Body = responseStream;
await _next.Invoke(context);
context.Response.Body.Seek(0, SeekOrigin.Begin);
responseBody = new StreamReader(context.Response.Body).ReadToEnd();
context.Response.Body.Seek(0, SeekOrigin.Begin);
await responseStream.CopyToAsync(originalBodyStream);
}
And this is my request header
{
"Accept":"application/json, text/plain, */*",
"Accept-Encoding":"gzip",
"Accept-Language":"en-GB,en-US;q=0.9,en;q=0.8",
"Connection":"Keep-Alive",
"Content-Length":"2",
"Content-Type":"application/json;charset=UTF-8",
}
Upvotes: 2
Views: 1013
Reputation: 131722
You shouldn't have to write your own response logging code. ASP.NET Core already supports HTTP Logging. It supports response compression using multiple algorithms too, including Brotli, which isn't supported by SharpZipLib.
To enable logging all that's needed is a call to app.UseHttpLogging()
:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseHttpLogging();
...
Requests and responses will be logged as Information
messages from the Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware
source. The destination of those messages can be configured using Logging configuration, through code or configuration settings.
ASP.NET Core doesn't have a built-in file provider. One of the most common options is to use Serilog, which can be configured to log messages from different sources to different files:
public static int Main(string[] args)
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
.Enrich.FromLogContext()
.WriteTo.Console()
.WriteTo.File("log.txt")
.CreateLogger();
...
and in CreateHostBuilder()
:
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseSerilog() // <-- Add this line
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
The ASP.NET Core Serilog integration also includes Request logging.
Logging to different files by eg log level is possible by passing a parameter to File
, eg:
.WriteTo.Console()
.WriteTo.File("log.txt",LogEventLevel.Information)
.WriteTo.File("errors.txt",LogEventLevel.Error)
Logging to different sinks can be configured using sub-loggers:
.WriteTo.Logger(lc => lc
.Filter.ByIncludingOnly(Matching.FromSource<Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware>())
.WriteTo.File("http.txt"))
Upvotes: 1
Reputation: 21
I can read using SharpZipLib package, This code working for me
GZipInputStream unGzipStream = new GZipInputStream(response.Body);
var responseBodytxt = new StreamReader(unGzipStream).ReadToEnd();
response.Body.Seek(0, SeekOrigin.Begin);
Upvotes: 0