diff --git a/ConfigurationExamples/appsettings.example.json b/ConfigurationExamples/appsettings.example.json index aaad45e2..d799a011 100644 --- a/ConfigurationExamples/appsettings.example.json +++ b/ConfigurationExamples/appsettings.example.json @@ -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" }, { @@ -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 } diff --git a/OpenAlprWebhookProcessor/Middleware/RequestResponseLoggingMiddleware.cs b/OpenAlprWebhookProcessor/Middleware/RequestResponseLoggingMiddleware.cs new file mode 100644 index 00000000..7ea4ea5f --- /dev/null +++ b/OpenAlprWebhookProcessor/Middleware/RequestResponseLoggingMiddleware.cs @@ -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 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 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 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}"; + } + } +} diff --git a/OpenAlprWebhookProcessor/Startup.cs b/OpenAlprWebhookProcessor/Startup.cs index d941811f..4953a50f 100644 --- a/OpenAlprWebhookProcessor/Startup.cs +++ b/OpenAlprWebhookProcessor/Startup.cs @@ -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 { @@ -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(); @@ -49,6 +56,11 @@ public void Configure( app.UseDeveloperExceptionPage(); } + if (Configuration.GetValue("WebRequestLoggingEnabled", false)) + { + app.UseMiddleware(); + } + app.UseHttpsRedirection(); app.UseSerilogRequestLogging(); diff --git a/OpenAlprWebhookProcessor/WebhookProcessor/WebhookHandler.cs b/OpenAlprWebhookProcessor/WebhookProcessor/WebhookHandler.cs index c8f34a2a..f7d2b5c7 100644 --- a/OpenAlprWebhookProcessor/WebhookProcessor/WebhookHandler.cs +++ b/OpenAlprWebhookProcessor/WebhookProcessor/WebhookHandler.cs @@ -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)}"; } diff --git a/OpenAlprWebhookProcessor/appsettings.json b/OpenAlprWebhookProcessor/appsettings.json index ab63b1a7..7a6c4868 100644 --- a/OpenAlprWebhookProcessor/appsettings.json +++ b/OpenAlprWebhookProcessor/appsettings.json @@ -2,5 +2,6 @@ "AllowedHosts": "*", "Cameras": { "Cameras": [] - } + }, + "WebRequestLoggingEnabled": false }