Circular Reference with Mongo Appender can Crash Your Process
I’ve blogged some days ago on the possibility to save log4net logs inside a Mongo database, but you should be aware that this technique can be dangerous if your objects have circular references.
Acircular reference happens when object A reference object B and object B directly or indirectly reference object A again and this is a high risk when you work with Mongo Serializer.
Mongo Serializer does not likes circular references (it is perfectly acceptable, because documents with circular references cannot be saved into a document database), but the problem is: if you try to serialize an object that has a circular reference you will get a StackOverflowException and your process will crash, as stated in official documentation from MSDN.
Starting with the .NET Framework version 2.0, a StackOverflowException object cannot be caught by a try-catch block and the corresponding process is terminated by default. Consequently, users are advised to write their code to detect and prevent a stack overflow.
If you remember how I modified MongoDb log4net appender, I decided to save into MongoDB complex objects with this code:
{
var properties = new BsonDocument();
foreach (DictionaryEntry entry in compositeProperties)
{
BsonValue value;
if (!BsonTypeMapper.TryMapToBsonValue(entry.Value, out value))
{
properties[entry.Key.ToString()] = entry.Value.ToBsonDocument();
}
else
{
properties[entry.Key.ToString()] = value;
}
}
toReturn["customproperties"] = properties;
}
Acircular reference happens when object A reference object B and object B directly or indirectly reference object A again and this is a high risk when you work with Mongo Serializer.
Mongo Serializer does not likes circular references (it is perfectly acceptable, because documents with circular references cannot be saved into a document database), but the problem is: if you try to serialize an object that has a circular reference you will get a StackOverflowException and your process will crash, as stated in official documentation from MSDN.
Starting with the .NET Framework version 2.0, a StackOverflowException object cannot be caught by a try-catch block and the corresponding process is terminated by default. Consequently, users are advised to write their code to detect and prevent a stack overflow.
If you remember how I modified MongoDb log4net appender, I decided to save into MongoDB complex objects with this code:
CODE:
if (compositeProperties != null && compositeProperties.Count > 0){
var properties = new BsonDocument();
foreach (DictionaryEntry entry in compositeProperties)
{
BsonValue value;
if (!BsonTypeMapper.TryMapToBsonValue(entry.Value, out value))
{
properties[entry.Key.ToString()] = entry.Value.ToBsonDocument();
}
else
{
properties[entry.Key.ToString()] = value;
}
}
toReturn["customproperties"] = properties;
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/301743/viewspace-732425/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- spring 迴圈注入:is there an unresolvable circular reference的解決方案Spring
- Scan Your Truck Using Nexiq Adapter: Simplifying Your Diagnostic ProcessAPT
- "alter system kill session" can crash the instance if IO slaves usedSession
- [Javascript] Circular dependencyJavaScript
- Circular Spanning Tree
- MySQL錯誤1042-Can't get hostname for your addressMySql
- logback解析——AppenderAPP
- 【Mongo】mongo聚合操作Go
- Git衝突:commit your changes or stash them before you can merge.GitMIT
- 【Mongo】mongo配置檔案Go
- owa Your request can't be completed right now. Please try again later.AI
- MySQL錯誤1042-Can't get hostname for your address解決方法MySql
- 解決 Git 更新本地衝突:commit your changes or stash them before you can mergeGitMIT
- MySQL 8.0 Reference Manual(讀書筆記61節--Examining Server Thread (Process) Information(1))MySql筆記ServerthreadORM
- MySQL 8.0 Reference Manual(讀書筆記61節--Examining Server Thread (Process) Information(2))MySql筆記ServerthreadORM
- 妙用ConstraintLayout的Circular positioningAI
- Your Prediction Gets As Good As Your DataGo
- 【mongo】mongo 欄位型別互轉Go型別
- Git衝突:commit your changes or stash them before you can merge. 解決辦法GitMIT
- 【Mongo】Mongo讀寫分離的實現Go
- 【Mongo】shell命令列模式執行mongo命令Go命令列模式
- [LeetCode] 641. Design Circular DequeLeetCode
- mongo 索引解析Go索引
- mongo基本操作Go
- mongo GridFSBucketGo
- 安裝mongoGo
- 管理索引mongo索引Go
- Your title
- Process Monitor Process (PMON) (121)
- postgres crash recovery
- Git更新本地倉庫及衝突"Commit your changes or stash them before you can merge"解決GitMIT
- docker操作mongo+docker的常用操作+mongo與.netDockerGo
- Java referenceJava
- undefined reference toUndefined
- search(9)- elastic4s logback-appenderASTAPP
- 【Mongo】mongo更新欄位為另一欄位的值Go
- 【Mongo】mongo分片加複製集的備份恢復Go
- 【Mongo】Mongodump 與 mongorestore對mongo進行備份恢復GoREST