public void GetValueTest2() { var l = _mockFactory.Create <TestLayout>(); const BsonType bsonType = BsonType.Boolean; var mf = new MongoField("Name", l.Object, bsonType.ToString()); const string value = "1122332"; BsonValue bv = new BsonBoolean(true); var invoked = false; BsonTryConvertMethod m = (string s, out BsonValue converted) => { converted = null; invoked = true; return(false); }; l.Protected().Setup("InitializeLayout").Verifiable(); l.Protected().Setup <string>("GetFormattedMessage", _lei).Returns(value).Verifiable(); _methodFactory.Setup(x => x.Create(bsonType)).Returns(m).Verifiable(); _structConverter.Setup(x => x.BsonString(value)).Returns(bv).Verifiable(); var res = Create().GetValue(mf, _lei); Assert.AreEqual(bv, res); Assert.IsTrue(invoked); }
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)); }
public void GetValueTest1() { var l = _mockFactory.Create <TestLayout>(); var mf = new MongoField("Name", l.Object, "skjgsdkljghsdkl;rghsdr;gh"); const string value = "1122332"; var bv = new BsonBoolean(true); l.Protected().Setup("InitializeLayout").Verifiable(); l.Protected().Setup <string>("GetFormattedMessage", _lei).Returns(value).Verifiable(); _structConverter.Setup(x => x.BsonString(value)).Returns(bv).Verifiable(); var res = Create().GetValue(mf, _lei); Assert.AreEqual(bv, res); }
public BsonValue GetValue(MongoField field, LogEventInfo logEvent) { var value = field?.Layout.Render(logEvent)?.Trim(); if (string.IsNullOrWhiteSpace(value)) { return(null); } BsonType type; BsonValue bsonValue; if (!Enum.TryParse(field.BsonType, true, out type) || !_bsonStructConvertMethodFactory.Create(type)(value, out bsonValue)) { bsonValue = _bsonStructConverter.BsonString(value); } return(bsonValue); }