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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Scan Your Truck Using Nexiq Adapter: Simplifying Your Diagnostic ProcessAPT
- [Javascript] Circular dependencyJavaScript
- Circular Spanning Tree
- MySQL 8.0 Reference Manual(讀書筆記61節--Examining Server Thread (Process) Information(2))MySql筆記ServerthreadORM
- MySQL 8.0 Reference Manual(讀書筆記61節--Examining Server Thread (Process) Information(1))MySql筆記ServerthreadORM
- 解決 Git 更新本地衝突:commit your changes or stash them before you can mergeGitMIT
- 2.7 Lab: Circular Shift
- 【Mongo】mongo聚合操作Go
- 妙用ConstraintLayout的Circular positioningAI
- 【Mongo】mongo配置檔案Go
- search(9)- elastic4s logback-appenderASTAPP
- Git更新本地倉庫及衝突"Commit your changes or stash them before you can merge"解決GitMIT
- [LeetCode] 641. Design Circular DequeLeetCode
- 【mongo】mongo 欄位型別互轉Go型別
- mongo GridFSBucketGo
- 【Mongo】shell命令列模式執行mongo命令Go命令列模式
- 【Mongo】Mongo讀寫分離的實現Go
- Your title
- BZOJ3497 : Pa2009 Circular GameGAM
- logseq 隱藏/hide linked reference and unlinked referenceGseIDE
- 自定義log4j的appender寫es日誌APP
- Java referenceJava
- Python - ReferencePython
- mongo基本操作Go
- mongo 索引解析Go索引
- 管理索引mongo索引Go
- 安裝mongoGo
- 【node】process
- docker操作mongo+docker的常用操作+mongo與.netDockerGo
- Prettier your projectProject
- Offering Your Seat
- crash日誌分析
- Crash日誌解析
- 解析 crash log(一)
- Android Native Crash 收集Android
- 【Mongo】Mongodump 與 mongorestore對mongo進行備份恢復GoREST
- 【Mongo】mongo分片加複製集的備份恢復Go
- 【Mongo】mongo更新欄位為另一欄位的值Go
- Event Reference(zt)