예제 #1
0
 public ResponseLogEnricher(ResponseLogData response, NancySerilogOptions options)
 {
     this.response = response;
     this.options  = options;
     if (this.options.IgnoredResponseLogFields == null)
     {
         this.options.IgnoredResponseLogFields = new FieldChooser <ResponseLogData>();
     }
 }
        static void AfterPipelineHook(NancyContext context)
        {
            if (!context.Items.ContainsKey("Stopwatch"))
            {
                return;
            }

            var nancyResponseData = new ResponseLogData();
            var stopwatch         = (Stopwatch)context.Items["Stopwatch"];

            stopwatch.Stop();

            nancyResponseData.RequestId           = (string)context.Items["RequestId"];
            nancyResponseData.Duration            = stopwatch.ElapsedMilliseconds;
            nancyResponseData.StatusCode          = (int)context.Response.StatusCode;
            nancyResponseData.ResponseHeaders     = new Dictionary <string, string>(context.Response.Headers);
            nancyResponseData.ResponseContentType = context.Response.ContentType ?? "";
            nancyResponseData.ReasonPhrase        = context.Response.ReasonPhrase ?? "";
            nancyResponseData.ResolvedPath        = context.ResolvedRoute.Description.Path;
            nancyResponseData.RequestedPath       = context.Request.Path;
            nancyResponseData.RawResponseCookies  = context.Response.Cookies.Select(cookie => new ResponseCookie
            {
                HttpOnly = cookie.HttpOnly,
                Secure   = cookie.Secure,
                Expires  = cookie.Expires,
                Name     = cookie.Name,
                Value    = cookie.Value
            }).ToArray();

            var cookieDict = new Dictionary <string, string>();

            foreach (var cookie in context.Response.Cookies)
            {
                cookieDict.Add(cookie.Name, cookie.Value);
            }

            nancyResponseData.ResponseCookies = cookieDict;

            using (var memoryStream = new MemoryStream())
            {
                context.Response.Contents(memoryStream);
                memoryStream.Flush();
                memoryStream.Position                   = 0;
                nancyResponseData.ResponseContent       = Encoding.UTF8.GetString(memoryStream.ToArray());
                nancyResponseData.ResponseContentLength = nancyResponseData.ResponseContent.Length;
            }

            var method = context.Request.Method;
            var logger = Log.ForContext(new ResponseLogEnricher(nancyResponseData));

            logger.Information($"Response {method} {nancyResponseData.RequestedPath.TrimAt(40)}");
        }
        public static ResponseLogData ReadResponseProperties(this NancyContext context, NancySerilogOptions options)
        {
            var ignoredFields = options.IgnoredResponseLogFields.ToArray();

            var responseLogData = new ResponseLogData();
            var stopwatch       = (Stopwatch)context.Items["Stopwatch"];

            stopwatch.Stop();

            responseLogData.RequestId           = (string)context.Items["RequestId"];
            responseLogData.Duration            = stopwatch.ElapsedMilliseconds;
            responseLogData.StatusCode          = (int)context.Response.StatusCode;
            responseLogData.ResponseHeaders     = new Dictionary <string, string>(context.Response.Headers);
            responseLogData.ResponseContentType = context.Response.ContentType ?? "";
            responseLogData.ReasonPhrase        = context.Response.ReasonPhrase ?? "";
            responseLogData.ResolvedPath        = context.ResolvedRoute.Description.Path;
            responseLogData.RequestedPath       = context.Request.Path;
            responseLogData.Method = context.Request.Method;

            if (!ignoredFields.Contains(nameof(responseLogData.RawResponseCookies)))
            {
                var rawCookies = context.Response.Cookies.Select(cookie => new ResponseCookie
                {
                    HttpOnly = cookie.HttpOnly,
                    Secure   = cookie.Secure,
                    Expires  = cookie.Expires,
                    Name     = cookie.Name,
                    Value    = cookie.Value
                });

                responseLogData.RawResponseCookies = rawCookies.ToArray();
            }

            if (!ignoredFields.Contains(nameof(responseLogData.ResponseCookies)))
            {
                // Add cookies as key-valued dict, easier to index on documents
                // for example if you are using Elasticseach
                var cookieDict = new Dictionary <string, string>();

                foreach (var cookie in context.Response.Cookies)
                {
                    cookieDict.Add(cookie.Name, cookie.Value);
                }

                responseLogData.ResponseCookies = cookieDict;
            }


            if (!ignoredFields.Contains(nameof(responseLogData.ResponseContent)))
            {
                // Read the contents of the response stream and add it to log
                using (var memoryStream = new MemoryStream())
                {
                    context.Response.Contents(memoryStream);
                    responseLogData.ResponseContent       = Encoding.UTF8.GetString(memoryStream.ToArray());
                    responseLogData.ResponseContentLength = responseLogData.ResponseContent.Length;
                }
            }
            else
            {
                responseLogData.ResponseContent       = "";
                responseLogData.ResponseContentLength = 0;
            }

            if (!ignoredFields.Contains(nameof(responseLogData.ResolvedRouteParameters)))
            {
                responseLogData.ResolvedRouteParameters = ReadDynamicDictionary(context.Parameters);
            }

            return(responseLogData);
        }
예제 #4
0
 public ResponseLogEnricher(ResponseLogData response)
 {
     this.response = response;
 }