示例#1
0
        private void LogResponse(Logger logger, KissLog.Web.HttpResponse response, ResponseSniffer sniffer)
        {
            try
            {
                using (sniffer.MirrorStream)
                {
                    sniffer.MirrorStream.Position = 0;

                    if (InternalHelpers.PreFilterShouldLogResponseBody(logger, sniffer.MirrorStream, response.Properties))
                    {
                        using (TemporaryFile tempFile = new TemporaryFile())
                        {
                            using (var fs = File.OpenWrite(tempFile.FileName))
                            {
                                sniffer.MirrorStream.CopyTo(fs);
                            }

                            string responseFileName = InternalHelpers.ResponseFileName(response.Properties.Headers);
                            logger.LogFile(tempFile.FileName, responseFileName);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("Error logging HTTP Response Content as file");
                sb.AppendLine(ex.ToString());

                KissLog.Internal.InternalHelpers.Log(sb.ToString(), LogLevel.Error);
            }
        }
        private static Requests.Web.ResponseProperties ToResponseProperties(KissLog.Web.HttpResponse item)
        {
            if (item == null)
            {
                return(null);
            }

            return(new Requests.Web.ResponseProperties
            {
                HttpStatusCode = (int)item.HttpStatusCode,
                HttpStatusCodeText = item.HttpStatusCode.ToString(),
                Headers = item.Properties.Headers,
                ContentLength = item.Properties.ContentLength,
            });
        }
示例#3
0
        private void EndRequest(object sender, EventArgs e)
        {
            HttpContext ctx = HttpContext.Current;

            Logger logger = Logger.Factory.Get() as Logger;

            if (logger == null)
            {
                return;
            }

            Exception ex = ctx.Server.GetLastError();

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

            var sniffer = ctx.Response.Filter as ResponseSniffer;

            if (ctx.Response.StatusCode >= 400 && ex == null)
            {
                if (sniffer != null)
                {
                    string responseContent = null;

                    try
                    {
                        responseContent = sniffer.GetContent();
                    }
                    catch (Exception ex1)
                    {
                        InternalHelpers.Log(ex1.ToString(), LogLevel.Error);
                    }

                    if (string.IsNullOrEmpty(responseContent) == false)
                    {
                        logger.Log(LogLevel.Error, responseContent);
                    }
                }
            }

            WebProperties webProperties = (WebProperties)HttpContext.Current.Items[Constants.HttpRequestPropertiesKey];

            if (webProperties == null)
            {
                // IIS redirect bypasses the BeginRequest() event
                webProperties = new WebProperties
                {
                    Request = HttpRequestFactory.Create(ctx.Request)
                };
            }

            KissLog.Web.HttpResponse response = HttpResponseFactory.Create(ctx.Response);
            webProperties.Response = response;

            if (logger.DataContainer.ExplicitHttpStatusCode.HasValue)
            {
                response.HttpStatusCode = logger.DataContainer.ExplicitHttpStatusCode.Value;
            }

            if (sniffer != null)
            {
                response.Properties.ContentLength = ReadResponseLength(sniffer);

                LogResponse(logger, response, sniffer);
            }

            logger.DataContainer.WebProperties = webProperties;

            IEnumerable <ILogger> loggers = Logger.Factory.GetAll();

            Logger.NotifyListeners(loggers.ToArray());
        }