2012-10-26 3 views
1

여기에 제안 된 방법을 사용하여 JSON.NET JSson 문자열을 BsonDocument로 변환하는 데 문제가 있습니다. Convert string into MongoDB BsonDocument. LogMessages를 mongodb를 다시 삽입하는 MongoDbLog4net appender를 작성하고 있습니다. 이러한 메시지에는 예외가 포함될 수 있으며 경우에 따라 예외 객체가 점 '을 포함하는 json 문자열로 직렬화됩니다.' 일부 키에서 BsonSerializer.Desrialize 메서드가 불평을 일으켰습니다. JsonConvert에게 무효 한 문자를 다른 것으로 바꾸거나 바꾸지 말라고 말하는 쉽고 효율적인 방법이 있습니까?문자열을 MongoDB로 변환 BsonDocument (속편)

protected override void Append(LoggingEvent loggingEvent) 
    { 
     // the log message here is used to filter and collect the 
     // fields from loggingEvent we are interested in 
     var logMessage = new LogMessage(loggingEvent); 

     // since mongodb does not serialize exceptions very well we 
     // will use JSON.NET to serialize the LogMessage instance 
     // and build the BSON document from it 
     string jsonLogMessage = JsonConvert.SerializeObject(logMessage); 

     var bsonLogMessage = BsonSerializer.Deserialize<BsonDocument>(jsonLogMessage); 

     this.logCollection.Insert(bsonLogMessage); 
    } 
+0

디시리얼라이저가 유효한 json 문서에서 오류가 발생하는 경우 재현 할 수 있도록 jer.mongodb.org에서 간단한 예제로 버그 보고서를 제출하십시오. –

+0

Deserializer의 버그가 아닙니다. { "Key.with.points": "Value"} 같은 것을 BsonDocument로 변환하는 것을 허용하지 않습니다. 나는 그 어딘가를 읽었다. ' 및 '$'는 BsonDocument의 키에서 허용되지 않습니다. –

답변

0

왜 간단한 문자열이 StringBuilder와 같은 변경 가능한 문자열로 대체되지 않습니까?

protected override void Append(LoggingEvent loggingEvent) 
{ 
    // the log message here is used to filter and collect the 
    // fields from loggingEvent we are interested in 
    var logMessage = new LogMessage(loggingEvent); 

    // since mongodb does not serialize exceptions very well we 
    // will use JSON.NET to serialize the LogMessage instance 
    // and build the BSON document from it 
    StringBuilder jsonLogMessageStringBuilder = new StringBuilder(JsonConvert.SerializeObject(logMessage)); 
    var jsonLogMessage = jsonLogMessageStringBuilder.Replace(".", "_").ToString(); 

    var bsonLogMessage = BsonSerializer.Deserialize<BsonDocument>(jsonLogMessage); 

    this.logCollection.Insert(bsonLogMessage); 
} 
+0

물론 유일한 단점은 키가 변형 될뿐만 아니라 값도 변형된다는 것입니다. 그러나, 나는 그 거래가 너무 나쁘다고 생각하지 않는다. 그러나 그것은 구현 자에게 달려있다. :) –