Beispiel #1
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));
        }
Beispiel #2
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));
        }