/// <summary> /// The automagically-called method that processes the request. /// </summary> /// <param name="context">The <see cref="HttpContext"/> instance for the current request.</param> /// <returns>A <see cref="Task"/> that completes when the middleware has processed the request.</returns> public async Task Invoke(HttpContext context) { IDisposable?localScope = null; try { if (context.Request.IsInteresting()) { var sessionId = CookieHandler.GetSessionId(context, _cookieOptions); var agentSessionId = HeaderHandler.GetAgentSessionId(context); //only grab session ids if we could possibly log them... if (_loggerFactory != null) { IList <KeyValuePair <string, object> >?requestProperties = null; if (!string.IsNullOrEmpty(sessionId)) { requestProperties ??= new List <KeyValuePair <string, object> >(); requestProperties.Add(new KeyValuePair <string, object>(Constants.SessionIdKey, sessionId)); } if (!string.IsNullOrEmpty(agentSessionId)) { requestProperties ??= new List <KeyValuePair <string, object> >(); requestProperties.Add( new KeyValuePair <string, object>(Constants.AgentSessionIdKey, agentSessionId)); } if (requestProperties != null) { var logger = _loggerFactory !.CreateLogger(Constants.Category); localScope = logger.BeginScope(requestProperties); } } } await _next(context); } finally { localScope?.Dispose(); } }