From 2c2db62e65d77c7f6e1e04c54f18eebd92f0d494 Mon Sep 17 00:00:00 2001 From: "Daniel J. Summers" Date: Sun, 22 May 2022 18:59:48 -0400 Subject: [PATCH] Add debug logging to web log determination --- src/MyWebLog/Caches.fs | 7 +------ src/MyWebLog/Program.fs | 18 +++++++++++++----- src/MyWebLog/appsettings.json | 2 +- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/MyWebLog/Caches.fs b/src/MyWebLog/Caches.fs index 5e81eef..2fd0b1e 100644 --- a/src/MyWebLog/Caches.fs +++ b/src/MyWebLog/Caches.fs @@ -26,16 +26,11 @@ open System.Collections.Concurrent /// settings update page module WebLogCache = - /// Create the full path of the request - let private fullPath (ctx : HttpContext) = - $"{ctx.Request.Scheme}://{ctx.Request.Host.Value}{ctx.Request.Path.Value}" - /// The cache of web log details let mutable private _cache : WebLog list = [] /// Try to get the web log for the current request (longest matching URL base wins) - let tryGet ctx = - let path = fullPath ctx + let tryGet (path : string) = _cache |> List.filter (fun wl -> path.StartsWith wl.urlBase) |> List.sortByDescending (fun wl -> wl.urlBase.Length) diff --git a/src/MyWebLog/Program.fs b/src/MyWebLog/Program.fs index dde6415..e58ff1c 100644 --- a/src/MyWebLog/Program.fs +++ b/src/MyWebLog/Program.fs @@ -1,17 +1,26 @@ open Microsoft.AspNetCore.Http +open Microsoft.Extensions.Logging open MyWebLog /// Middleware to derive the current web log -type WebLogMiddleware (next : RequestDelegate) = - +type WebLogMiddleware (next : RequestDelegate, log : ILogger) = + + /// Is the debug level enabled on the logger? + let isDebug = log.IsEnabled LogLevel.Debug + member this.InvokeAsync (ctx : HttpContext) = task { - match WebLogCache.tryGet ctx with + /// Create the full path of the request + let path = $"{ctx.Request.Scheme}://{ctx.Request.Host.Value}{ctx.Request.Path.Value}" + match WebLogCache.tryGet path with | Some webLog -> + if isDebug then log.LogDebug $"Resolved web log {WebLogId.toString webLog.id} for {path}" ctx.Items["webLog"] <- webLog if PageListCache.exists ctx then () else do! PageListCache.update ctx if CategoryCache.exists ctx then () else do! CategoryCache.update ctx return! next.Invoke ctx - | None -> ctx.Response.StatusCode <- 404 + | None -> + if isDebug then log.LogDebug $"No resolved web log for {path}" + ctx.Response.StatusCode <- 404 } @@ -153,7 +162,6 @@ open Microsoft.AspNetCore.Authentication.Cookies open Microsoft.AspNetCore.Builder open Microsoft.AspNetCore.HttpOverrides open Microsoft.Extensions.Configuration -open Microsoft.Extensions.Logging open MyWebLog.ViewModels open RethinkDB.DistributedCache open RethinkDb.Driver.FSharp diff --git a/src/MyWebLog/appsettings.json b/src/MyWebLog/appsettings.json index f7733ab..381b010 100644 --- a/src/MyWebLog/appsettings.json +++ b/src/MyWebLog/appsettings.json @@ -3,7 +3,7 @@ "hostname": "data02.bitbadger.solutions", "database": "myWebLog_dev" }, - "Generator": "myWebLog 2.0-alpha10", + "Generator": "myWebLog 2.0-alpha11", "Logging": { "LogLevel": { "MyWebLog.Handlers": "Debug"