Serilog是.NET開源結構化日誌類庫
開源地址:https://github.com/serilog
官網:https://serilog.net/html
Serilog能作什麼:git
想對Serilog多點了解,請查閱[譯]Serilog Tutorial,翻譯的很棒。程序員
在本身的項目中使用Serilog並使用MongoDB記錄日誌,須要nuget引用 Serilog
和 Serilog.Sinks.MongoDB
。
我引用的是 Serilog 2.7.1
和 Serilog.Sinks.MongoDB 3.1.0
Serilog.Sinks.MongoDB 3.1.0
依賴的是的MongoDB組件庫是 MongoDB.Driver 2.3.0
、MongoDB.Driver.Core 2.3.0
、MongoDB.Bson 2.3.0
,因此當nuget引用Serilog.Sinks.MongoDB 3.1.0 天然會引用 MongoDB.Driver 2.3.0
、MongoDB.Driver.Core 2.3.0
、MongoDB.Bson 2.3.0
。
在記錄MongoDB日誌時報錯,內容以下github
The GuidRepresentation for the reader is CSharpLegacy, which requires the binary sub type to be UuidLegacy, not UuidStandard
程序員google大法,找到一樣的錯誤。點擊查看:
The GuidRepresentation for the reader is CSharpLegacy, which requires the binary sub type to be UuidLegacy, not UuidStandardmongodb
文中給了三種解決方案:
一、修改全局配置 BsonDefaults.GuidRepresentation
ui
BsonDefaults.GuidRepresentation = GuidRepresentation.Standard;
二、當你建立collection時指定配置google
MongoDatabase db = ???; string collectionName = ???; var collectionSettings = new MongoCollectionSettings { GuidRepresentation = GuidRepresentation.Standard }; var collection = db.GetCollection<BsonDocument>(collectionName, collectionSettings);
三、更新驅動
原文以下.net
Solution 3.update to .NET Driver Version 2.5.x
.NET Driver Version 2.5.0 Release Notes say below:
The main new feature of 2.5.0 is support for the new features of the 3.6 version of the server:
...
Improved support for reading and writing UUIDs in BsonBinary subtype 4 format翻譯
解決方案1和2都有點瞎啊,推測要改Serilog.Sinks.MongoDB的源碼。天然先按照最簡單的開始嘗試,更新MongoDB驅動。日誌
個人嘗試解決方案:MongoDB.Driver 2.3.0
、MongoDB.Driver.Core 2.3.0
、MongoDB.Bson 2.3.0
通通更新至2.7.0版本(注:當前最新的穩定版本)。而後,嘣!問題解決!