Exemple #1
0
        public IEnumerable <KeyValuePair <string, BsonValue> > Create(LogEventInfo logEvent)
        {
            yield return(new KeyValuePair <string, BsonValue>("Date", new BsonDateTime(logEvent.TimeStamp)));

            yield return(new KeyValuePair <string, BsonValue>("Level", _bsonStructConverter.BsonString(logEvent.Level?.Name)));

            yield return(new KeyValuePair <string, BsonValue>("Logger", _bsonStructConverter.BsonString(logEvent.LoggerName)));

            yield return(new KeyValuePair <string, BsonValue>("Message", _bsonStructConverter.BsonString(logEvent.FormattedMessage)));

            yield return(new KeyValuePair <string, BsonValue>("Exception", _bsonExceptionFactory.Create(logEvent.Exception)));
        }
Exemple #2
0
        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);
        }
Exemple #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);
        }
        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);
        }