99namespace WireMock . Owin ;
1010
1111internal class WireMockMiddlewareLogger (
12- IWireMockMiddlewareOptions _options ,
13- LogEntryMapper _logEntryMapper ,
14- IGuidUtils _guidUtils
12+ IWireMockMiddlewareOptions options ,
13+ LogEntryMapper logEntryMapper ,
14+ IGuidUtils guidUtils ,
15+ IAdminPaths adminPaths
1516) : IWireMockMiddlewareLogger
1617{
1718 public void LogRequestAndResponse ( bool logRequest , RequestMessage request , IResponseMessage ? response , MappingMatcherResult ? match , MappingMatcherResult ? partialMatch , Activity ? activity )
1819 {
20+ var mapping = match ? . Mapping ;
21+ var partialMapping = partialMatch ? . Mapping ;
22+
1923 var logEntry = new LogEntry
2024 {
21- Guid = _guidUtils . NewGuid ( ) ,
25+ Guid = guidUtils . NewGuid ( ) ,
2226 RequestMessage = request ,
2327 ResponseMessage = response ,
2428
25- MappingGuid = match ? . Mapping ? . Guid ,
26- MappingTitle = match ? . Mapping ? . Title ,
29+ MappingGuid = mapping ? . Guid ,
30+ MappingTitle = mapping ? . Title ,
2731 RequestMatchResult = match ? . RequestMatchResult ,
2832
29- PartialMappingGuid = partialMatch ? . Mapping ? . Guid ,
30- PartialMappingTitle = partialMatch ? . Mapping ? . Title ,
33+ PartialMappingGuid = partialMapping ? . Guid ,
34+ PartialMappingTitle = partialMapping ? . Title ,
3135 PartialMatchResult = partialMatch ? . RequestMatchResult
3236 } ;
3337
34- WireMockActivitySource . EnrichWithLogEntry ( activity , logEntry , _options . ActivityTracingOptions ) ;
38+ WireMockActivitySource . EnrichWithLogEntry ( activity , logEntry , options . ActivityTracingOptions ) ;
3539 activity ? . Dispose ( ) ;
3640
3741 LogLogEntry ( logEntry , logRequest ) ;
3842
3943 try
4044 {
41- if ( _options . SaveUnmatchedRequests == true && match ? . RequestMatchResult is not { IsPerfectMatch : true } )
45+ if ( options . SaveUnmatchedRequests == true && match ? . RequestMatchResult is not { IsPerfectMatch : true } )
4246 {
4347 var filename = $ "{ logEntry . Guid } .LogEntry.json";
44- _options . FileSystemHandler ? . WriteUnmatchedRequest ( filename , _options . DefaultJsonSerializer . Serialize ( logEntry ) ) ;
48+ options . FileSystemHandler ? . WriteUnmatchedRequest ( filename , options . DefaultJsonSerializer . Serialize ( logEntry ) ) ;
4549 }
4650 }
4751 catch
@@ -52,34 +56,35 @@ public void LogRequestAndResponse(bool logRequest, RequestMessage request, IResp
5256
5357 public void LogLogEntry ( LogEntry entry , bool addRequest )
5458 {
55- _options . Logger . DebugRequestResponse ( _logEntryMapper . Map ( entry ) , entry . RequestMessage ? . Path . StartsWith ( "/__admin/" ) == true ) ;
59+ var isAdminRequest = adminPaths . Includes ( entry . RequestMessage ? . Path ) ;
60+ options . Logger . DebugRequestResponse ( logEntryMapper . Map ( entry ) , isAdminRequest ) ;
5661
5762 // If addRequest is set to true and MaxRequestLogCount is null or does have a value greater than 0, try to add a new request log.
58- if ( addRequest && _options . MaxRequestLogCount is null or > 0 )
63+ if ( addRequest && options . MaxRequestLogCount is null or > 0 )
5964 {
6065 TryAddLogEntry ( entry ) ;
6166 }
6267
6368
6469 // In case MaxRequestLogCount has a value greater than 0, try to delete existing request logs based on the count.
65- if ( _options . MaxRequestLogCount is > 0 )
70+ if ( options . MaxRequestLogCount is > 0 )
6671 {
67- var logEntries = _options . LogEntries . ToList ( ) ;
72+ var logEntries = options . LogEntries . ToList ( ) ;
6873
6974 foreach ( var logEntry in logEntries
7075 . OrderBy ( le => le . RequestMessage ? . DateTime ?? le . ResponseMessage ? . DateTime )
71- . Take ( logEntries . Count - _options . MaxRequestLogCount . Value ) )
76+ . Take ( logEntries . Count - options . MaxRequestLogCount . Value ) )
7277 {
7378 TryRemoveLogEntry ( logEntry ) ;
7479 }
7580 }
7681
7782 // In case RequestLogExpirationDuration has a value greater than 0, try to delete existing request logs based on the date.
78- if ( _options . RequestLogExpirationDuration is > 0 )
83+ if ( options . RequestLogExpirationDuration is > 0 )
7984 {
80- var logEntries = _options . LogEntries . ToList ( ) ;
85+ var logEntries = options . LogEntries . ToList ( ) ;
8186
82- var checkTime = DateTime . UtcNow . AddHours ( - _options . RequestLogExpirationDuration . Value ) ;
87+ var checkTime = DateTime . UtcNow . AddHours ( - options . RequestLogExpirationDuration . Value ) ;
8388 foreach ( var logEntry in logEntries . Where ( le => le . RequestMessage ? . DateTime < checkTime || le . ResponseMessage ? . DateTime < checkTime ) )
8489 {
8590 TryRemoveLogEntry ( logEntry ) ;
@@ -91,7 +96,7 @@ private void TryAddLogEntry(LogEntry logEntry)
9196 {
9297 try
9398 {
94- _options . LogEntries . Add ( logEntry ) ;
99+ options . LogEntries . Add ( logEntry ) ;
95100 }
96101 catch
97102 {
@@ -103,7 +108,7 @@ private void TryRemoveLogEntry(LogEntry logEntry)
103108 {
104109 try
105110 {
106- _options . LogEntries . Remove ( logEntry ) ;
111+ options . LogEntries . Remove ( logEntry ) ;
107112 }
108113 catch
109114 {
0 commit comments