public BsonValue Create(IEnumerable <MongoField> props, LogEventInfo logEvent) { if (props == null) { throw new ArgumentNullException(nameof(props)); } if (logEvent == null) { throw new ArgumentNullException(nameof(logEvent)); } var propertiesDocument = new BsonDocument(); foreach (var prop in props) { _bsonDocumentValueAppender.Append(propertiesDocument, prop.Name, _bsonConverter.GetValue(prop, logEvent)); } var properties = logEvent.Properties ?? Enumerable.Empty <KeyValuePair <object, object> >(); foreach (var property in properties.Where(property => property.Key != null && property.Value != null)) { _bsonDocumentValueAppender.Append(propertiesDocument, property.Key.ToString(), _bsonStructConverter.BsonString(property.Value.ToString())); } return(propertiesDocument.ElementCount > 0 ? (BsonValue)propertiesDocument : BsonNull.Value); }
public BsonDocument CreateDocument(LogEventInfo logEvent, IReadOnlyCollection <MongoField> fields, IReadOnlyCollection <MongoField> properties, bool includeDefaults) { if (logEvent == null) { throw new ArgumentNullException(nameof(logEvent)); } if (fields == null) { throw new ArgumentNullException(nameof(fields)); } if (properties == null) { throw new ArgumentNullException(nameof(properties)); } var document = new BsonDocument(); if (includeDefaults || !fields.Any()) { foreach (var keyValuePair in _defaultsFactory.Create(logEvent)) { _bsonDocumentValueAppender.Append(document, keyValuePair.Key, keyValuePair.Value); } } foreach (var field in fields) { var value = _bsonConverter.GetValue(field, logEvent); _bsonDocumentValueAppender.Append(document, field.Name, value); } var props = _bsonPropertiesFactory.Create(properties, logEvent); _bsonDocumentValueAppender.Append(document, "Properties", props); return(document); }