-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* what if it's missing entirely * add debug logging and more config checks * cleanup * more
- Loading branch information
Showing
5 changed files
with
91 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
OpenAlprWebhookProcessor/Middleware/RequestResponseLoggingMiddleware.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Logging; | ||
using System; | ||
using System.IO; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace OpenAlprWebhookProcessor.Middleware | ||
{ | ||
public class RequestResponseLoggingMiddleware | ||
{ | ||
private readonly ILogger _logger; | ||
|
||
private readonly RequestDelegate _next; | ||
|
||
public RequestResponseLoggingMiddleware( | ||
ILogger<RequestResponseLoggingMiddleware> logger, | ||
RequestDelegate next) | ||
{ | ||
_logger = logger; | ||
_next = next; | ||
} | ||
|
||
public async Task Invoke(HttpContext context) | ||
{ | ||
var request = await FormatRequest(context.Request); | ||
|
||
_logger.LogInformation("request received: {0}", request); | ||
|
||
var originalBodyStream = context.Response.Body; | ||
|
||
using (var responseBody = new MemoryStream()) | ||
{ | ||
context.Response.Body = responseBody; | ||
|
||
await _next(context); | ||
|
||
var response = await FormatResponse(context.Response); | ||
|
||
await responseBody.CopyToAsync(originalBodyStream); | ||
} | ||
} | ||
|
||
private async Task<string> FormatRequest(HttpRequest request) | ||
{ | ||
var body = request.Body; | ||
|
||
request.EnableBuffering(); | ||
|
||
var buffer = new byte[Convert.ToInt32(request.ContentLength)]; | ||
|
||
await request.Body.ReadAsync(buffer, 0, buffer.Length); | ||
|
||
var bodyAsText = Encoding.UTF8.GetString(buffer); | ||
|
||
request.Body = body; | ||
|
||
return $"{request.Scheme} {request.Host}{request.Path} {request.QueryString} {bodyAsText}"; | ||
} | ||
|
||
private async Task<string> FormatResponse(HttpResponse response) | ||
{ | ||
response.Body.Seek(0, SeekOrigin.Begin); | ||
|
||
string text = await new StreamReader(response.Body).ReadToEndAsync(); | ||
|
||
response.Body.Seek(0, SeekOrigin.Begin); | ||
|
||
return $"{response.StatusCode}: {text}"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,6 @@ | |
"AllowedHosts": "*", | ||
"Cameras": { | ||
"Cameras": [] | ||
} | ||
}, | ||
"WebRequestLoggingEnabled": false | ||
} |