Пример #1
0
        public static void SaveRequestData(HttpContextBase context)
        {
            var url = context.Request.Url.ToString();

            var data = new BsonDocumentWrapper(new
            {
                //_id = ObjectId.GenerateNewId(),
                at = DateTime.UtcNow,
                url,
                method = context.Request.HttpMethod,
                headers = From(context.Request.Headers),
                clientIp = context.Request.UserHostAddress
            }).ToBsonDocument();
            if (context.Request.Form.Count > 0)
            {
                data.Add("form", From(context.Request.Form));
            }
            var cookies = From(context.Response.Cookies);
            if (cookies.Length > 0)
                data.Add("cookies", BsonArray.Create((IEnumerable)cookies));
            var referrer = context.Request.UrlReferrer;
            if (referrer != null)
                data.Add("referrer", referrer.ToString());

            try
            {
                DB.Database.GetCollection("requestsLog").Insert(data);
            }
            catch
            {
            }
        }
 public void TestAddNameValue()
 {
     var wrapper = new BsonDocumentWrapper(new BsonDocument());
     Assert.AreEqual(false, wrapper.IsMaterialized);
     wrapper.Add("x", 1);
     Assert.AreEqual(true, wrapper.IsMaterialized);
     Assert.AreEqual(1, wrapper.ElementCount);
     Assert.AreEqual("x", wrapper.GetElement(0).Name);
     Assert.AreEqual(new BsonInt32(1), wrapper[0]);
 }
        public void TestAddNameValueFactoryWithCondition()
        {
            var wrapper = new BsonDocumentWrapper(new BsonDocument());
            Assert.AreEqual(false, wrapper.IsMaterialized);
            wrapper.Add("x", () => 1, false);
            Assert.AreEqual(true, wrapper.IsMaterialized);
            Assert.AreEqual(0, wrapper.ElementCount);

            wrapper = new BsonDocumentWrapper(new BsonDocument());
            Assert.AreEqual(false, wrapper.IsMaterialized);
            wrapper.Add("x", () => 1, true);
            Assert.AreEqual(true, wrapper.IsMaterialized);
            Assert.AreEqual(1, wrapper.ElementCount);
            Assert.AreEqual("x", wrapper.GetElement(0).Name);
            Assert.AreEqual(new BsonInt32(1), wrapper[0]);
        }