Example #1
0
        internal void PostAcquireRequestState(HttpContextBase httpContext)
        {
            if (httpContext == null)
            {
                throw new ArgumentNullException(nameof(httpContext));
            }

            if (httpContext.Session == null)
            {
                return;
            }

            httpContext.Session.Add(SessionKey, true);

            var    factory = new LoggerFactory();
            Logger logger  = factory.GetInstance(httpContext);

            if (logger.DataContainer.HttpProperties == null && httpContext.Request != null)
            {
                KissLog.Http.HttpRequest httpRequest = HttpRequestFactory.Create(httpContext.Request);
                logger.DataContainer.SetHttpProperties(new Http.HttpProperties(httpRequest));
            }

            if (logger.DataContainer.HttpProperties == null)
            {
                return;
            }

            logger.DataContainer.HttpProperties.Request.SetSession(httpContext.Session.SessionID, httpContext.Session.IsNewSession);
        }
Example #2
0
        internal void OnBeginRequest(HttpContextBase httpContext)
        {
            if (httpContext == null)
            {
                throw new ArgumentNullException(nameof(httpContext));
            }

            if (httpContext.Request == null)
            {
                InternalLogger.LogException(new NullHttpRequestException(nameof(OnBeginRequest)));
                return;
            }

            KissLog.Http.HttpRequest httpRequest = HttpRequestFactory.Create(httpContext.Request);

            var    factory = new LoggerFactory();
            Logger logger  = factory.GetInstance(httpContext);

            logger.DataContainer.SetHttpProperties(new Http.HttpProperties(httpRequest));

            KissLog.InternalHelpers.WrapInTryCatch(() =>
            {
                NotifyListeners.NotifyBeginRequest.Notify(httpRequest);
            });
        }
        internal static User CreateUser(KissLog.Http.HttpRequest httpRequest)
        {
            if (httpRequest == null)
            {
                throw new ArgumentNullException(nameof(httpRequest));
            }

            User user = RequestLogsApiListener.Options.Handlers.CreateUserPayload.Invoke(httpRequest);

            if (IsNull(user))
            {
                return(null);
            }

            return(user);
        }
Example #4
0
        internal void OnPostAuthenticateRquest(HttpContextBase httpContext)
        {
            if (httpContext == null)
            {
                throw new ArgumentNullException(nameof(httpContext));
            }

            var    factory = new LoggerFactory();
            Logger logger  = factory.GetInstance(httpContext);

            if (logger.DataContainer.HttpProperties == null && httpContext.Request != null)
            {
                KissLog.Http.HttpRequest httpRequest = HttpRequestFactory.Create(httpContext.Request);
                logger.DataContainer.SetHttpProperties(new Http.HttpProperties(httpRequest));
            }

            if (logger.DataContainer.HttpProperties == null)
            {
                return;
            }

            bool isAuthenticated = httpContext.User?.Identity?.IsAuthenticated ?? false;

            logger.DataContainer.HttpProperties.Request.SetIsAuthenticated(isAuthenticated);

            if (httpContext.User == null || httpContext.User is ClaimsPrincipal == false)
            {
                return;
            }

            ClaimsPrincipal claimsPrincipal = httpContext.User as ClaimsPrincipal;

            if (claimsPrincipal.Identity == null || claimsPrincipal.Identity is ClaimsIdentity == false)
            {
                return;
            }

            ClaimsIdentity claimsIdentity = claimsPrincipal.Identity as ClaimsIdentity;

            List <KeyValuePair <string, string> > claims = InternalHelpers.ToKeyValuePair(claimsIdentity);

            logger.DataContainer.HttpProperties.Request.Properties.SetClaims(claims);
        }
Example #5
0
        internal void OnEndRequest(HttpContextBase httpContext)
        {
            if (httpContext == null)
            {
                throw new ArgumentNullException(nameof(httpContext));
            }

            if (httpContext.Response == null)
            {
                InternalLogger.LogException(new NullHttpResponseException(nameof(OnEndRequest)));
                return;
            }

            if (httpContext.Request == null)
            {
                InternalLogger.LogException(new NullHttpRequestException(nameof(OnEndRequest)));
                return;
            }

            var    factory = new LoggerFactory();
            Logger logger  = factory.GetInstance(httpContext);

            // IIS redirect bypasses the IHttpModule.BeginRequest event
            if (logger.DataContainer.HttpProperties == null)
            {
                KissLog.Http.HttpRequest httpRequest = HttpRequestFactory.Create(httpContext.Request);
                logger.DataContainer.SetHttpProperties(new Http.HttpProperties(httpRequest));
            }

            MirrorStreamDecorator responseStream = GetResponseStream(httpContext.Response);
            long contentLength = responseStream == null ? 0 : responseStream.MirrorStream.Length;

            KissLog.Http.HttpResponse httpResponse = HttpResponseFactory.Create(httpContext.Response, contentLength);
            logger.DataContainer.HttpProperties.SetResponse(httpResponse);

            Exception ex = httpContext.Server?.GetLastError();

            if (ex != null)
            {
                logger.Error(ex);
            }

            if (responseStream != null)
            {
                if (KissLog.InternalHelpers.CanReadResponseBody(httpResponse.Properties.Headers))
                {
                    if (ShouldLogResponseBody(logger, factory, httpContext))
                    {
                        ILogResponseBodyStrategy logResponseBody = LogResponseBodyStrategyFactory.Create(responseStream.MirrorStream, responseStream.Encoding, logger);
                        logResponseBody.Execute();
                    }
                }

                responseStream.MirrorStream.Dispose();
            }

            IEnumerable <Logger> loggers = factory.GetAll(httpContext);

            KissLog.InternalHelpers.WrapInTryCatch(() =>
            {
                NotifyListeners.NotifyFlush.Notify(loggers.ToArray());
            });
        }
Example #6
0
        public async Task Invoke(HttpContext context)
        {
            KissLog.Http.HttpRequest httpRequest = HttpRequestFactory.Create(context.Request);

            var    factory = new LoggerFactory();
            Logger logger  = factory.GetInstance(context);

            logger.DataContainer.SetHttpProperties(new Http.HttpProperties(httpRequest));

            KissLog.InternalHelpers.WrapInTryCatch(() =>
            {
                NotifyListeners.NotifyBeginRequest.Notify(httpRequest);
            });

            ExceptionDispatchInfo ex = null;

            if (context.Response.Body != null && context.Response.Body is MirrorStreamDecorator == false)
            {
                context.Response.Body = new MirrorStreamDecorator(context.Response.Body);
            }

            try
            {
                await _next(context);
            }
            catch (Exception e)
            {
                ex = ExceptionDispatchInfo.Capture(e);
                throw;
            }
            finally
            {
                MirrorStreamDecorator responseStream = GetResponseStream(context.Response);
                long contentLength = responseStream == null ? 0 : responseStream.MirrorStream.Length;
                int  statusCode    = context.Response.StatusCode;

                if (ex != null)
                {
                    statusCode = (int)HttpStatusCode.InternalServerError;
                    logger.Error(ex.SourceException);
                }

                KissLog.Http.HttpResponse httpResponse = HttpResponseFactory.Create(context.Response, contentLength);
                httpResponse.SetStatusCode(statusCode);

                logger.DataContainer.HttpProperties.SetResponse(httpResponse);

                if (responseStream != null)
                {
                    if (KissLog.InternalHelpers.CanReadResponseBody(httpResponse.Properties.Headers))
                    {
                        if (ShouldLogResponseBody(logger, factory, context))
                        {
                            ILogResponseBodyStrategy logResponseBody = LogResponseBodyStrategyFactory.Create(responseStream.MirrorStream, responseStream.Encoding, logger);
                            logResponseBody.Execute();
                        }
                    }

                    responseStream.MirrorStream.Dispose();
                }

                IEnumerable <Logger> loggers = factory.GetAll(context);

                KissLog.InternalHelpers.WrapInTryCatch(() =>
                {
                    NotifyListeners.NotifyFlush.Notify(loggers.ToArray());
                });
            }
        }