예제 #1
0
        /// <summary>
        /// Sets Error properties pulled from HttpContext, if present
        /// </summary>
        /// <param name="context">The HttpContext related to the request</param>
        private void SetContextProperties(HttpContext context)
        {
            if (context == null)
            {
                return;
            }

            var request = context.Request;

            Func <Func <HttpRequest, NameValueCollection>, NameValueCollection> tryGetCollection = getter =>
            {
                try
                {
                    return(new NameValueCollection(getter(request)));
                }
                catch (HttpRequestValidationException e)
                {
                    Trace.WriteLine("Error parsing collection: " + e.Message);
                    return(new NameValueCollection {
                        { CollectionErrorKey, e.Message }
                    });
                }
            };

            ServerVariables = tryGetCollection(r => r.ServerVariables);
            QueryString     = tryGetCollection(r => r.QueryString);
            Form            = tryGetCollection(r => r.Form);

            // Filter form variables for sensitive information
            if (FormLogFilters.Count > 0)
            {
                foreach (var k in FormLogFilters.Keys)
                {
                    if (Form[k] != null)
                    {
                        Form[k] = FormLogFilters[k];
                    }
                }
            }

            try
            {
                Cookies = new NameValueCollection(request.Cookies.Count);
                for (var i = 0; i < request.Cookies.Count; i++)
                {
                    var    name = request.Cookies[i].Name;
                    string val;
                    CookieLogFilters.TryGetValue(name, out val);
                    Cookies.Add(name, val ?? request.Cookies[i].Value);
                }
            }
            catch (HttpRequestValidationException e)
            {
                Trace.WriteLine("Error parsing cookie collection: " + e.Message);
            }
        }
예제 #2
0
        /// <summary>
        /// Sets Error properties pulled from HttpContext, if present
        /// </summary>
        /// <param name="context">The HttpContext related to the request</param>
        private void SetContextProperties(HttpContext context)
        {
            if (context == null)
            {
                return;
            }

            var request = context.Request;

            Func <Func <HttpRequest, NameValueCollection>, NameValueCollection> tryGetCollection = getter =>
            {
                try
                {
                    return(new NameValueCollection(getter(request)));
                }
                catch (HttpRequestValidationException e)
                {
                    Trace.WriteLine("Error parsing collection: " + e.Message);
                    return(new NameValueCollection {
                        { CollectionErrorKey, e.Message }
                    });
                }
            };

            ServerVariables = tryGetCollection(r => r.ServerVariables);
            QueryString     = tryGetCollection(r => r.QueryString);
            Form            = tryGetCollection(r => r.Form);

            // Filter form variables for sensitive information
            if (FormLogFilters.Count > 0)
            {
                foreach (var k in FormLogFilters.Keys)
                {
                    if (Form[k] != null)
                    {
                        Form[k] = FormLogFilters[k];
                    }
                }
            }

            try
            {
                Cookies = new NameValueCollection(request.Cookies.Count);
                for (var i = 0; i < request.Cookies.Count; i++)
                {
                    var    name = request.Cookies[i].Name;
                    string val;
                    CookieLogFilters.TryGetValue(name, out val);
                    Cookies.Add(name, val ?? request.Cookies[i].Value);
                }
            }
            catch (HttpRequestValidationException e)
            {
                Trace.WriteLine("Error parsing cookie collection: " + e.Message);
            }

            RequestHeaders = new NameValueCollection(request.Headers.Count);
            foreach (var header in request.Headers.AllKeys)
            {
                // Cookies are handled above, no need to repeat
                if (string.Compare(header, "Cookie", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    continue;
                }

                if (request.Headers[header] != null)
                {
                    RequestHeaders[header] = request.Headers[header];
                }
            }
        }