Exemplo n.º 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);
            }
        }
Exemplo n.º 2
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 = sniffer.GetContent();
                    if (string.IsNullOrEmpty(responseContent) == false)
                    {
                        logger.Log(LogLevel.Error, responseContent);
                    }
                }
            }

            WebRequestProperties properties = (WebRequestProperties)HttpContext.Current.Items[Constants.HttpRequestPropertiesKey];

            properties.EndDateTime = DateTime.UtcNow;

            ResponseProperties response = WebResponsePropertiesFactory.Create(ctx.Response);

            properties.Response = response;

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

            if (sniffer != null)
            {
                response.ContentLength = sniffer.MirrorStream.Length;
            }

            if (sniffer != null)
            {
                using (sniffer.MirrorStream)
                {
                    sniffer.MirrorStream.Position = 0;

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

                            string responseFileName = InternalHelpers.ResponseFileName(properties.Response.Headers);
                            logger.LogFile(tempFile.FileName, responseFileName);
                        }
                    }
                }
            }

            logger.DataContainer.WebRequestProperties = properties;

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

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