예제 #1
0
        public void MongoFieldTest1()
        {
            const string name = "Name";

            var mf = new MongoField(name, _layout.Object);

            Assert.AreEqual(name, mf.Name);
            Assert.AreEqual(_layout.Object, mf.Layout);
            Assert.AreEqual("String", mf.BsonType);
        }
예제 #2
0
        public void MongoFieldTest()
        {
            var mf = new MongoField();

            Assert.IsNull(mf.Name);
            // ReSharper disable HeuristicUnreachableCode
            Assert.IsNull(mf.Layout);
            Assert.AreEqual("String", mf.BsonType);
            // ReSharper restore HeuristicUnreachableCode
        }
예제 #3
0
        public void MongoFieldTest2()
        {
            const string name     = "Name";
            const string bsonType = "Wtf";

            var mf = new MongoField(name, _layout.Object, bsonType);

            Assert.AreEqual(name, mf.Name);
            Assert.AreEqual(_layout.Object, mf.Layout);
            Assert.AreEqual(bsonType, mf.BsonType);
        }
예제 #4
0
        private BsonValue GetValue(MongoField field, LogEventInfo logEvent)
        {
            var value = field.Layout.Render(logEvent);

            if (string.IsNullOrWhiteSpace(value))
            {
                return(null);
            }

            value = value.Trim();

            if (string.IsNullOrEmpty(field.BsonType) ||
                string.Equals(field.BsonType, "String", StringComparison.OrdinalIgnoreCase))
            {
                return(new BsonString(value));
            }


            BsonValue bsonValue;

            if (string.Equals(field.BsonType, "Boolean", StringComparison.OrdinalIgnoreCase) &&
                MongoConvert.TryBoolean(value, out bsonValue))
            {
                return(bsonValue);
            }

            if (string.Equals(field.BsonType, "DateTime", StringComparison.OrdinalIgnoreCase) &&
                MongoConvert.TryDateTime(value, out bsonValue))
            {
                return(bsonValue);
            }

            if (string.Equals(field.BsonType, "Double", StringComparison.OrdinalIgnoreCase) &&
                MongoConvert.TryDouble(value, out bsonValue))
            {
                return(bsonValue);
            }

            if (string.Equals(field.BsonType, "Int32", StringComparison.OrdinalIgnoreCase) &&
                MongoConvert.TryInt32(value, out bsonValue))
            {
                return(bsonValue);
            }

            if (string.Equals(field.BsonType, "Int64", StringComparison.OrdinalIgnoreCase) &&
                MongoConvert.TryInt64(value, out bsonValue))
            {
                return(bsonValue);
            }

            return(new BsonString(value));
        }
예제 #5
0
        private BsonValue GetValue(MongoField field, LogEventInfo logEvent)
        {
            var value = field.Layout.Render(logEvent);

            if (string.IsNullOrWhiteSpace(value))
            {
                return(null);
            }

            value = value.Trim();

            return(ConvertBsonValue(field, value));
        }
예제 #6
0
        private BsonValue ConvertBsonValue(MongoField field, string valueStr)
        {
            if (string.IsNullOrEmpty(field.BsonType) || string.Equals(field.BsonType, "String", StringComparison.OrdinalIgnoreCase))
            {
                return(new BsonString(valueStr));
            }


            BsonValue bsonValue;

            if (string.Equals(field.BsonType, "Boolean", StringComparison.OrdinalIgnoreCase) &&
                MongoConvert.TryBoolean(valueStr, out bsonValue))
            {
                return(bsonValue);
            }

            if (string.Equals(field.BsonType, "DateTime", StringComparison.OrdinalIgnoreCase) &&
                MongoConvert.TryDateTime(valueStr, out bsonValue))
            {
                return(bsonValue);
            }

            if (string.Equals(field.BsonType, "Double", StringComparison.OrdinalIgnoreCase) &&
                MongoConvert.TryDouble(valueStr, out bsonValue))
            {
                return(bsonValue);
            }

            if (string.Equals(field.BsonType, "Int32", StringComparison.OrdinalIgnoreCase) &&
                MongoConvert.TryInt32(valueStr, out bsonValue))
            {
                return(bsonValue);
            }

            if (string.Equals(field.BsonType, "Int64", StringComparison.OrdinalIgnoreCase) &&
                MongoConvert.TryInt64(valueStr, out bsonValue))
            {
                return(bsonValue);
            }

            return(new BsonString(valueStr));
        }
예제 #7
0
        private BsonValue GetValue(MongoField field, LogEventInfo logEvent)
        {
            var value = field.Layout.Render(logEvent);
            if (string.IsNullOrWhiteSpace(value))
                return null;

            value = value.Trim();
            
            if (string.IsNullOrEmpty(field.BsonType) 
                || string.Equals(field.BsonType, "String", StringComparison.OrdinalIgnoreCase))
                return new BsonString(value);

            
            BsonValue bsonValue;
            if (string.Equals(field.BsonType, "Boolean", StringComparison.OrdinalIgnoreCase)
                && MongoConvert.TryBoolean(value, out bsonValue))
                return bsonValue;

            if (string.Equals(field.BsonType, "DateTime", StringComparison.OrdinalIgnoreCase)
                && MongoConvert.TryDateTime(value, out bsonValue))
                return bsonValue;

            if (string.Equals(field.BsonType, "Double", StringComparison.OrdinalIgnoreCase)
                && MongoConvert.TryDouble(value, out bsonValue))
                return bsonValue;
            
            if (string.Equals(field.BsonType, "Int32", StringComparison.OrdinalIgnoreCase)
                && MongoConvert.TryInt32(value, out bsonValue))
                return bsonValue;
            
            if (string.Equals(field.BsonType, "Int64", StringComparison.OrdinalIgnoreCase)
                && MongoConvert.TryInt64(value, out bsonValue))
                return bsonValue;

            return new BsonString(value);
        }