private BsonDocument BuildBsonDocument(LoggingEvent log) { if (_fields.Count == 0) { return(BackwardCompatibility.BuildBsonDocument(log)); } var doc = new BsonDocument(); foreach (MongoAppenderFileld field in _fields) { 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(); } doc.Add(field.Name, bsonValue); } return(doc); }
private BsonValue GetBsonValueWithField(LogEventInfo logEvent, MongoDBLayoutField field) { var type = field.BsonType; var layout = field.Layout; if (layout == null) { return(null); } var value = RenderLogEvent(layout, logEvent); if (string.IsNullOrEmpty(value)) { return(null); } BsonValue bsonValue; if (!BsonTypeMapper.TryMapToBsonValue(value, out bsonValue)) { bsonValue = bsonValue.ToBsonDocument(); } return(bsonValue); }
public BsonDocument LoggingEventToBSON(LoggingEvent loggingEvent) { if (loggingEvent == null) { return(null); } var toReturn = new BsonDocument(); toReturn[FieldNames.Timestamp] = loggingEvent.TimeStamp; toReturn[FieldNames.Level] = loggingEvent.Level.ToString(); toReturn[FieldNames.Thread] = loggingEvent.ThreadName ?? String.Empty; if (!LooseFix) { toReturn[FieldNames.Username] = loggingEvent.UserName ?? String.Empty; } toReturn[FieldNames.Message] = loggingEvent.RenderedMessage; toReturn[FieldNames.Loggername] = loggingEvent.LoggerName ?? String.Empty; toReturn[FieldNames.Domain] = loggingEvent.Domain ?? String.Empty; toReturn[FieldNames.Machinename] = MachineName ?? String.Empty; toReturn[FieldNames.ProgramName] = ProgramName ?? String.Empty; // location information, if available if (!LooseFix && loggingEvent.LocationInformation != null) { toReturn[FieldNames.Filename] = loggingEvent.LocationInformation.FileName ?? String.Empty; toReturn[FieldNames.Method] = loggingEvent.LocationInformation.MethodName ?? String.Empty; toReturn[FieldNames.Linenumber] = loggingEvent.LocationInformation.LineNumber ?? String.Empty; toReturn[FieldNames.Classname] = loggingEvent.LocationInformation.ClassName ?? String.Empty; } // exception information if (loggingEvent.ExceptionObject != null) { toReturn[FieldNames.Exception] = ExceptionToBSON(loggingEvent.ExceptionObject); if (loggingEvent.ExceptionObject.InnerException != null) { //Serialize all inner exception in a bson array var innerExceptionList = new BsonArray(); var actualEx = loggingEvent.ExceptionObject.InnerException; while (actualEx != null) { var ex = ExceptionToBSON(actualEx); innerExceptionList.Add(ex); if (actualEx.InnerException == null) { //this is the first exception toReturn[FieldNames.FirstException] = ExceptionToBSON(actualEx); } actualEx = actualEx.InnerException; } toReturn[FieldNames.Innerexception] = innerExceptionList; } } // properties PropertiesDictionary compositeProperties = loggingEvent.GetProperties(); if (compositeProperties != null && compositeProperties.Count > 0) { var properties = new BsonDocument(); foreach (DictionaryEntry entry in compositeProperties) { if (entry.Value == null) { continue; } //remember that no property can have a point in it because it cannot be saved. String key = entry.Key.ToString().Replace(".", "|"); BsonValue value; if (!BsonTypeMapper.TryMapToBsonValue(entry.Value, out value)) { properties[key] = entry.Value.ToBsonDocument(); } else { properties[key] = value; } } toReturn[FieldNames.Customproperties] = properties; } return(toReturn); }
static BsonValue ToBsonValue(object value, ScriptBlock convert, int depth) { IncSerializationDepth(ref depth); if (value == null) { return(BsonNull.Value); } value = BaseObject(value, out PSObject custom); // case: custom if (custom != null) { return(ToBsonDocumentFromProperties(null, custom, convert, null, depth)); } // case: BsonValue if (value is BsonValue bson) { return(bson); } // case: string if (value is string text) { return(new BsonString(text)); } // case: document if (value is IConvertibleToBsonDocument cd) { return(cd.ToBsonDocument()); } // case: dictionary if (value is IDictionary dictionary) { return(ToBsonDocumentFromDictionary(null, dictionary, convert, null, depth)); } // case: bytes or collection if (value is IEnumerable en) { //_191108_183844 if (en is byte[] bytes) { return(new BsonBinaryData(bytes)); } var array = new BsonArray(); foreach (var it in en) { array.Add(ToBsonValue(it, convert, depth)); } return(array); } // try to map BsonValue if (BsonTypeMapper.TryMapToBsonValue(value, out BsonValue bson2)) { return(bson2); } // try to serialize class var type = value.GetType(); if (TypeIsDriverSerialized(type)) { return(BsonExtensionMethods.ToBsonDocument(value, type)); } // no converter? die if (convert == null) { //! use this type throw new ArgumentException(Res.CannotConvert2(type, nameof(BsonValue))); } try { value = DocumentInput.ConvertValue(convert, value); } catch (RuntimeException re) { //! use this type throw new ArgumentException($"Converter script was called for '{type}' and failed with '{re.Message}'.", re); } // do not pass converter twice return(ToBsonValue(value, null, depth)); }