log4net.Appender.MongoDB.BackwardCompatibility.BuildBsonDocument C# (CSharp) Method

BuildBsonDocument() public static method

public static BuildBsonDocument ( log4net.Core.LoggingEvent loggingEvent ) : BsonDocument
loggingEvent log4net.Core.LoggingEvent
return BsonDocument
		public static BsonDocument BuildBsonDocument(LoggingEvent loggingEvent)
		{
			if(loggingEvent == null)
			{
				return null;
			}

			var toReturn = new BsonDocument();
			toReturn["timestamp"] = loggingEvent.TimeStamp;
			toReturn["level"] = loggingEvent.Level.ToString();
			toReturn["thread"] = loggingEvent.ThreadName;
			toReturn["userName"] = loggingEvent.UserName;
			toReturn["message"] = loggingEvent.RenderedMessage;
			toReturn["loggerName"] = loggingEvent.LoggerName;
			toReturn["domain"] = loggingEvent.Domain;
			toReturn["machineName"] = Environment.MachineName;

			// location information, if available
			if(loggingEvent.LocationInformation != null)
			{
				toReturn["fileName"] = loggingEvent.LocationInformation.FileName;
				toReturn["method"] = loggingEvent.LocationInformation.MethodName;
				toReturn["lineNumber"] = loggingEvent.LocationInformation.LineNumber;
				toReturn["className"] = loggingEvent.LocationInformation.ClassName;
			}

			// exception information
			if(loggingEvent.ExceptionObject != null)
			{
				toReturn["exception"] = BuildExceptionBsonDocument(loggingEvent.ExceptionObject);
			}

			// properties
			PropertiesDictionary compositeProperties = loggingEvent.GetProperties();
			if(compositeProperties != null && compositeProperties.Count > 0)
			{
				var properties = new BsonDocument();
				foreach(DictionaryEntry entry in compositeProperties)
				{
					properties[entry.Key.ToString()] = entry.Value.ToString();
				}

				toReturn["properties"] = properties;
			}

			return toReturn;
		}

Usage Example

        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 = BsonValue.Create(value);
                doc.Add(field.Name, bsonValue);
            }
            return(doc);
        }