Skip to content

Commit

Permalink
Debug for web requests (#12)
Browse files Browse the repository at this point in the history
* what if it's missing entirely

* add debug logging and more config checks

* cleanup

* more
  • Loading branch information
mlapaglia authored Jan 3, 2021
1 parent 716781f commit 34be5c4
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 5 deletions.
7 changes: 4 additions & 3 deletions ConfigurationExamples/appsettings.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"Manufacturer": "Hikvision",
"OpenAlprCameraId": 0,
"Password": "ip_camera_password",
"UpdateOverlayTextUrl": "http://ip_camera_ip/url_to_update_overlays",
"UpdateOverlayTextUrl": "http://ip_camera_ip/ISAPI/System/Video/inputs/channels/1/overlays",
"Username": "ip_camera_username"
},
{
Expand All @@ -20,9 +20,10 @@
"Manufacturer": "Hikvision",
"OpenAlprCameraId": 0,
"Password": "ip_camera_password",
"UpdateOverlayTextUrl": "http://ip_camera_ip/url_to_update_overlays",
"UpdateOverlayTextUrl": "http://ip_camera_ip/ISAPI/System/Video/inputs/channels/1/overlays",
"Username": "ip_camera_username"
}
]
}
},
"WebRequestLoggingEnabled": false
}
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}";
}
}
}
12 changes: 12 additions & 0 deletions OpenAlprWebhookProcessor/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using OpenAlprWebhookProcessor.Cameras.Configuration;
using OpenAlprWebhookProcessor.Middleware;
using OpenAlprWebhookProcessor.WebhookProcessor;
using Serilog;
using System;

namespace OpenAlprWebhookProcessor
{
Expand All @@ -26,6 +28,11 @@ public void ConfigureServices(IServiceCollection services)
var cameraConfiguration = new CameraConfiguration();
Configuration.GetSection("Cameras").Bind(cameraConfiguration);

if (cameraConfiguration.Cameras == null || cameraConfiguration.Cameras.Count == 0)
{
throw new ArgumentException("no cameras found in appsettings, check your configuration");
}

services.AddLogging(config =>
{
config.AddDebug();
Expand All @@ -49,6 +56,11 @@ public void Configure(
app.UseDeveloperExceptionPage();
}

if (Configuration.GetValue("WebRequestLoggingEnabled", false))
{
app.UseMiddleware<RequestResponseLoggingMiddleware>();
}

app.UseHttpsRedirection();

app.UseSerilogRequestLogging();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void Handle(OpenAlprWebhook webhook)
ProcessedPlateConfidence = Math.Round(webhook.BestPlate.Confidence, 2),
};

if (webhook.Vehicle.MakeModel.Count > 0)
if (webhook.Vehicle.MakeModel != null && webhook.Vehicle.MakeModel.Count > 0)
{
updateRequest.VehicleDescription = $"{webhook.Vehicle.Year[0].Name} {FormatVehicleDescription(webhook.Vehicle.MakeModel[0].Name)}";
}
Expand Down
3 changes: 2 additions & 1 deletion OpenAlprWebhookProcessor/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"AllowedHosts": "*",
"Cameras": {
"Cameras": []
}
},
"WebRequestLoggingEnabled": false
}

0 comments on commit 34be5c4

Please sign in to comment.