private BsonDocument BuildBsonDocument(LogEventInfo logEvent)
        {
            var doc = Fields.Count == 0 || AppendFields
                                ? logEvent.ToBsonDocument()
                                : new BsonDocument();

            if (UseCappedCollection && CreateIdField)
            {
                doc.AddField("_id", ObjectId.GenerateNewId());
            }

            foreach (var field in Fields)
            {
                if (field.Layout != null)
                {
                    var renderedField = field.Layout.Render(logEvent);
                    if (!string.IsNullOrWhiteSpace(renderedField))
                    {
                        doc[field.Name] = renderedField;
                    }
                    continue;
                }

                var searchResult = logEvent.GetValue(field.Name);
                if (!searchResult.Succeded)
                {
                    throw new InvalidOperationException(string.Format("Invalid field name '{0}'.", field.Name));
                }

                doc.AddField(field.Name, searchResult.Value);
            }

            return(doc);
        }
Beispiel #2
0
        private BsonDocument BuildBsonDocument(LogEventInfo logEvent)
        {
            var doc = Fields.Count == 0 || AppendFields
                                ? logEvent.ToBsonDocument()
                                : new BsonDocument();

            if (UseCappedCollection && CreateIdField)
            {
                doc.AddField("_id", ObjectId.GenerateNewId());
            }

            foreach (var field in Fields)
            {
                if (field.Layout != null)
                {
                    var renderedField = field.Layout.Render(logEvent);
                    if (!string.IsNullOrWhiteSpace(renderedField))
                    {
                        var bsonTypeValue = field.BsonType;
                        if (!string.IsNullOrWhiteSpace(bsonTypeValue))
                        {
                            const bool IgnoreCase = true;
                            BsonType   bsonType;
                            if (Enum.TryParse(bsonTypeValue, IgnoreCase, out bsonType))
                            {
                                var bsonValue = BsonTypeMapper.MapToBsonValue(renderedField, bsonType);
                                doc[field.Name] = bsonValue;
                            }
                            else
                            {
                                var msg = "Unknown BsonType specified: " + bsonTypeValue;
                                msg += ". Note that the type name must match enum names from the MongoDB.Bson.BsonType in the C# driver and do not necessarily match the MongoDB internal type names.";
                                throw new InvalidOperationException(msg);
                            }
                        }
                        else
                        {
                            doc[field.Name] = renderedField;
                        }
                    }
                    continue;
                }

                var searchResult = logEvent.GetValue(field.Name);
                if (!searchResult.Succeded)
                {
                    throw new InvalidOperationException(string.Format("Invalid field name '{0}'.", field.Name));
                }

                doc.AddField(field.Name, searchResult.Value);
            }

            return(doc);
        }
Beispiel #3
0
        private BsonDocument BuildBsonDocument(LogEventInfo logEvent)
        {
            var doc = Fields.Count == 0 || AppendFields
				? logEvent.ToBsonDocument()
				: new BsonDocument();

			if (UseCappedCollection && CreateIdField)
				doc.AddField("_id", ObjectId.GenerateNewId());

			foreach (var field in Fields)
			{
				if (field.Layout != null)
				{
					var renderedField = field.Layout.Render(logEvent);
					if (!string.IsNullOrWhiteSpace(renderedField))
						doc[field.Name] = renderedField;
					continue;
				}

				var searchResult = logEvent.GetValue(field.Name);
				if (!searchResult.Succeded)
					throw new InvalidOperationException(string.Format("Invalid field name '{0}'.", field.Name));

				doc.AddField(field.Name, searchResult.Value);
			}

			return doc;
		}
Beispiel #4
0
        private BsonDocument BuildBsonDocument(LogEventInfo logEvent)
        {
            var doc = Fields.Count == 0 || AppendFields
                ? logEvent.ToBsonDocument()
                : new BsonDocument();

            if (UseCappedCollection && CreateIdField)
                doc.AddField("_id", ObjectId.GenerateNewId());

            foreach (var field in Fields)
            {
                if (field.Layout != null)
                {
                    var renderedField = field.Layout.Render(logEvent);
                    if (!string.IsNullOrWhiteSpace(renderedField))
                    {
                        var bsonTypeValue = field.BsonType;
                        if (!string.IsNullOrWhiteSpace(bsonTypeValue))
                        {
                            const bool IgnoreCase = true;
                            BsonType bsonType;
                            if (Enum.TryParse(bsonTypeValue, IgnoreCase, out bsonType))
                            {
                                var bsonValue = BsonTypeMapper.MapToBsonValue(renderedField, bsonType);
                                doc[field.Name] = bsonValue;
                            }
                            else
                            {
                                var msg = "Unknown BsonType specified: " + bsonTypeValue;
                                msg += ". Note that the type name must match enum names from the MongoDB.Bson.BsonType in the C# driver and do not necessarily match the MongoDB internal type names.";
                                throw new InvalidOperationException(msg);
                            }
                        }
                        else
                        {
                            doc[field.Name] = renderedField;
                        }
                    }

                    continue;
                }

                var searchResult = logEvent.GetValue(field.Name);
                if (!searchResult.Succeded)
                    throw new InvalidOperationException(string.Format("Invalid field name '{0}'.", field.Name));

                doc.AddField(field.Name, searchResult.Value);
            }

            return doc;
        }