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);
        }
示例#2
0
        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);
        }
示例#3
0
        public BsonValue Create(Exception exception)
        {
            if (exception == null)
            {
                return(BsonNull.Value);
            }
            var document = new BsonDocument();

            _bsonDocumentValueAppender.Append(document, "Message", _bsonStructConverter.BsonString(exception.Message));
            _bsonDocumentValueAppender.Append(document, "BaseMessage", _bsonStructConverter.BsonString(exception.GetBaseException().Message));
            _bsonDocumentValueAppender.Append(document, "Text", _bsonStructConverter.BsonString(exception.ToString()));
            _bsonDocumentValueAppender.Append(document, "Type", _bsonStructConverter.BsonString(exception.GetType().ToString()));
            _bsonDocumentValueAppender.Append(document, "Stack", _bsonStructConverter.BsonString(exception.StackTrace));


            var external = exception as ExternalException;

            if (external != null)
            {
                _bsonDocumentValueAppender.Append(document, "ErrorCode", new BsonInt32(external.ErrorCode));
            }

            _bsonDocumentValueAppender.Append(document, "Source", _bsonStructConverter.BsonString(exception.Source));

            var method = exception.TargetSite;

            if (method != null)
            {
                var assembly = method.Module.Assembly.GetName();

                _bsonDocumentValueAppender.Append(document, "MethodName", _bsonStructConverter.BsonString(method.Name));
                _bsonDocumentValueAppender.Append(document, "ModuleName", _bsonStructConverter.BsonString(assembly.Name));
                _bsonDocumentValueAppender.Append(document, "ModuleVersion", _bsonStructConverter.BsonString(assembly.Version?.ToString()));
            }

            return(document);
        }