Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for BsonDocuments as values, allowing nested documents #47

Merged
merged 1 commit into from
Nov 14, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/Log4Mongo/MongoDBAppender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,15 @@ private BsonDocument BuildBsonDocument(LoggingEvent log)
var doc = new BsonDocument();
foreach (MongoAppenderFileld field in _fields)
{
object value = field.Layout.Format(log);
var bsonValue = value as BsonValue ?? BsonValue.Create(value);
doc.Add(field.Name, bsonValue);
}
object value = field.Layout.Format(log);
BsonValue bsonValue;
// if the object is complex and can't be mapped to a simple object, convert to bson document
if (!BsonTypeMapper.TryMapToBsonValue(value, out bsonValue))
{
bsonValue = value.ToBsonDocument();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't the ToBsonDocument() call redundant?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe so, since we need a BsonValue type to pass in to the doc.Add() function and an object type doesn't fulfill that. The BsonDocument type is a subclass of BsonValue and so is more specific. Or maybe I'm missing your point all together?

}
doc.Add(field.Name, bsonValue);
}
return doc;
}

Expand Down