Skip to content

Commit

Permalink
bug in request body reader resolved
Browse files Browse the repository at this point in the history
  • Loading branch information
vahid committed Nov 2, 2024
1 parent 11bb369 commit d8832b5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 22 deletions.
28 changes: 9 additions & 19 deletions 03-Infrastructure/Jinget.Logger/Loggers/RequestLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,28 @@

namespace Jinget.Logger.Loggers;

public class RequestLogger<TCategoryName> : Log<TCategoryName>, ILog
public class RequestLogger<TCategoryName>(
RequestDelegate next,
ILogger<TCategoryName> logger,
IOptions<BlackListHeader> blackListHeaders,
IOptions<WhiteListHeader> whiteListHeaders) : Log<TCategoryName>(next, logger, blackListHeaders, whiteListHeaders), ILog
{
public RequestLogger(RequestDelegate next, ILogger<TCategoryName> logger,
IOptions<BlackListHeader> blackListHeaders,
IOptions<WhiteListHeader> whiteListHeaders)
: base(next, logger, blackListHeaders, whiteListHeaders)
{
}

public async Task LogAsync(HttpContext context)
{
context.Request.EnableBuffering();
string requestBodyText;
bool ismultiPart = false;
var originalRequestBody = context.Request?.Body;
if (context.IsMultipartContentType())
{
requestBodyText = "--REQUEST BODY TRIMMED BY LOGGER-- multipart/form-data";
ismultiPart = true;
}
else
{
var requestBodyStream = new MemoryStream();
await context.Request?.Body.CopyToAsync(requestBodyStream);
requestBodyStream.Seek(0, SeekOrigin.Begin);
requestBodyText = await new StreamReader(requestBodyStream, Encoding.UTF8).ReadToEndAsync();
context.Request.EnableBuffering();
using var reader = new StreamReader(context.Request.Body, Encoding.UTF8, leaveOpen: true);
requestBodyText = await reader.ReadToEndAsync();
context.Request.Body.Seek(0, SeekOrigin.Begin);
}

SetLog(context, requestBodyText);
if (!ismultiPart)
context.Request.Body = originalRequestBody;

await Next(context);
}

Expand Down
10 changes: 7 additions & 3 deletions Tests/Jinget.WebAPI.Tests/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
Url = "localhost:9200",
UseSsl = false,
UseGlobalExceptionHandler = true,
Handle4xxResponses = false
Handle4xxResponses = true
};
builder.Services.ConfigureElasticSearchLogger(elasticSearchSetting);
builder.Services.AddControllers();
Expand Down Expand Up @@ -78,10 +78,14 @@
});
app.MapGet("successlog", () => "Hello vahid");
app.MapGet("detailedlog", () => "Sample Success");
app.MapPost("save", (BaseSettingModel setting) => setting);
app.MapPost("save", (viewmodel vm) => vm);

app.MapGet("/logs/{search}/{page}/{pagesize}", async (
IElasticSearchLoggingDomainService domainService, string search, int page, int pagesize) =>
await domainService.SearchAsync("20241026", search, page, pagesize, origin: "/logs/"));

app.Run();
app.Run();
public class viewmodel
{
public string name { get; set; }
}

0 comments on commit d8832b5

Please sign in to comment.