public void ShouldFilter_ServerVariables_ByRegexAndName()
        {
            var logFilterSettings = new HttpContextDataLogFilterSettings
            {
                ServerVarFilters = new List <HttpContextDataLogFilter>
                {
                    new HttpContextDataLogFilter {
                        Name = "AUTH_U.*", ReplaceWith = "", NameIsRegex = true
                    },
                    new HttpContextDataLogFilter {
                        Name = "AUTH_PASSWORD", ReplaceWith = "***"
                    },
                }
            };

            var ctxData = new HttpContextData(new ApplicationException("Test exception"), _context.Object, logFilterSettings);

            Assert.IsTrue(ctxData.ServerVariables.Count == 2);
            Assert.IsTrue(ctxData.ServerVariables["AUTH_PASSWORD"] == "***");
            Assert.IsTrue(ctxData.ServerVariables["AUTH_TYPE"] == "Forms");
        }
        public void ShouldFilter_Cookies_ByRegexAndName()
        {
            var logFilterSettings = new HttpContextDataLogFilterSettings
            {
                CookieFilters = new List <HttpContextDataLogFilter>
                {
                    new HttpContextDataLogFilter {
                        Name = "COOKIE_1.*", ReplaceWith = "", NameIsRegex = true
                    },
                    new HttpContextDataLogFilter {
                        Name = "COOKIE_2", ReplaceWith = ""
                    },
                    new HttpContextDataLogFilter {
                        Name = "COOKIE_3", ReplaceWith = "***"
                    },
                },
            };

            var ctxData = new HttpContextData(new ApplicationException("Test exception"), _context.Object, logFilterSettings);

            Assert.IsTrue(ctxData.Cookies.Count == 2);
            Assert.IsTrue(ctxData.Cookies["COOKIE_3"] == "***");
            Assert.IsTrue(ctxData.Cookies["COOKIE_4"] == "ck5");
        }
        public void ShouldFilter_Header_ByRegexAndName()
        {
            var logFilterSettings = new HttpContextDataLogFilterSettings
            {
                HeaderFilters = new List <HttpContextDataLogFilter>
                {
                    new HttpContextDataLogFilter {
                        Name = "HEADER_B_.*_B", ReplaceWith = "", NameIsRegex = true
                    },
                    new HttpContextDataLogFilter {
                        Name = "HEADER_C", ReplaceWith = ""
                    },
                    new HttpContextDataLogFilter {
                        Name = "HEADER_D", ReplaceWith = "***"
                    },
                },
            };

            var ctxData = new HttpContextData(new ApplicationException("Test exception"), _context.Object, logFilterSettings);

            Assert.IsTrue(ctxData.RequestHeaders.Count == 2);
            Assert.IsTrue(ctxData.RequestHeaders["HEADER_D"] == "***");
            Assert.IsTrue(ctxData.RequestHeaders["HEADER_E"] == "H5");
        }
        public void ShouldFilter_FormData_ByRegexAndName()
        {
            var logFilterSettings = new HttpContextDataLogFilterSettings
            {
                FormFilters = new List <HttpContextDataLogFilter>
                {
                    new HttpContextDataLogFilter {
                        Name = "FORM_B_.*_B", ReplaceWith = "", NameIsRegex = true
                    },
                    new HttpContextDataLogFilter {
                        Name = "FORM_C", ReplaceWith = ""
                    },
                    new HttpContextDataLogFilter {
                        Name = "FORM_D", ReplaceWith = "***"
                    },
                },
            };

            var ctxData = new HttpContextData(new ApplicationException("Test exception"), _context.Object, logFilterSettings);

            Assert.IsTrue(ctxData.Form.Count == 2);
            Assert.IsTrue(ctxData.Form["FORM_D"] == "***");
            Assert.IsTrue(ctxData.Form["FORM_E"] == "F5");
        }
Esempio n. 5
0
        public async Task <object> Convert(HttpContextData data)
        {
            CommonLogContent content = new CommonLogContent();

            content.ParentID         = ContextContainer.GetValue <Guid>(ContextExtensionTypes.ParentCommonLogID);
            content.ParentActionName = ContextContainer.GetValue <string>(ContextExtensionTypes.ParentCommonLogActionName);
            content.ActionName       = data.RequestPath;
            content.Message          = string.Empty;

            byte[] bufferBytes     = new byte[1024];
            string strRequestBody  = string.Empty;
            string strResponseBody = string.Empty;

            //尝试获取请求内容和响应内容
            if (data.Request != null)
            {
                using (data.Request)
                {
                    List <byte> requestBytes = new List <byte>();

                    while (true)
                    {
                        var length = await data.Request.ReadAsync(bufferBytes, 0, 1024);

                        requestBytes.AddRange(bufferBytes.Take(length));
                        if (length != 1024)
                        {
                            break;
                        }
                    }

                    strRequestBody = UTF8Encoding.UTF8.GetString(requestBytes.ToArray());
                }
            }

            if (data.Response != null)
            {
                using (data.Response)
                {
                    List <byte> responseBytes = new List <byte>();

                    while (true)
                    {
                        var length = await data.Response.ReadAsync(bufferBytes, 0, 1024);

                        responseBytes.AddRange(bufferBytes.Take(length));
                        if (length != 1024)
                        {
                            break;
                        }
                    }

                    strResponseBody = UTF8Encoding.UTF8.GetString(responseBytes.ToArray());
                }
            }

            content.RequestBody  = strRequestBody;
            content.ResponseBody = strResponseBody;
            content.RequestUri   = data.RequestUri;
            content.Message      = string.Empty;

            return(await Task.FromResult(content));
        }
Esempio n. 6
0
        public async Task <object> Convert(HttpContextData data)
        {
            byte[] bufferBytes     = new byte[1024];
            string strRequestBody  = null;
            string strResponseBody = null;

            //尝试获取请求内容和响应内容
            if (data.Request != null)
            {
                using (data.Request)
                {
                    List <byte> requestBytes = new List <byte>();


                    long totalLength = 0;
                    while (true)
                    {
                        int bufSize = 1024;
                        if (totalLength + 1024 > _maxRequestLength)
                        {
                            bufSize = (int)(_maxRequestLength - totalLength);
                        }
                        var length = await data.Request.ReadAsync(bufferBytes, 0, bufSize);

                        totalLength = totalLength + length;
                        requestBytes.AddRange(bufferBytes.Take(length));
                        if (length != 1024)
                        {
                            break;
                        }
                    }

                    strRequestBody = UTF8Encoding.UTF8.GetString(requestBytes.ToArray());
                }
            }

            if (data.Response != null)
            {
                using (data.Response)
                {
                    List <byte> responseBytes = new List <byte>();

                    long totalLength = 0;
                    while (true)
                    {
                        int bufSize = 1024;
                        if (totalLength + 1024 > _maxResponseLength)
                        {
                            bufSize = (int)(_maxResponseLength - totalLength);
                        }

                        var length = await data.Response.ReadAsync(bufferBytes, 0, bufSize);

                        totalLength = totalLength + length;
                        responseBytes.AddRange(bufferBytes.Take(length));
                        if (length != 1024)
                        {
                            break;
                        }
                    }

                    strResponseBody = UTF8Encoding.UTF8.GetString(responseBytes.ToArray());
                }
            }

            string strActionName = string.Empty;


            CommonLogLocalContent content = new CommonLogLocalContent()
            {
                RequestUri = "", ActionName = strActionName, RequestBody = strRequestBody, ResponseBody = strResponseBody, Message = ""
            };

            return(await Task.FromResult(content));
        }