Serilog记录MongoDB日志报错:requires the binary sub type to be UuidLegacy, not UuidStandard

Serilog

Serilog是.NET开源结构化日志类库
开源地址:https://github.com/serilog
官网:https://serilog.net/

Serilog能做什么:

  1. 记录代码中的BUG和错误
  2. 更快的找到生产环境中的问题
  3. 深入的了解系统运行表现

想对Serilog多点了解,请查阅[译]Serilog Tutorial,翻译的很棒。

重现Error出现的场景

在自己的项目中使用Serilog并使用MongoDB记录日志,需要nuget引用 SerilogSerilog.Sinks.MongoDB
我引用的是 Serilog 2.7.1Serilog.Sinks.MongoDB 3.1.0
Serilog.Sinks.MongoDB 3.1.0 依赖的是的MongoDB组件库是 MongoDB.Driver 2.3.0MongoDB.Driver.Core 2.3.0MongoDB.Bson 2.3.0,所以当nuget引用Serilog.Sinks.MongoDB 3.1.0 自然会引用 MongoDB.Driver 2.3.0MongoDB.Driver.Core 2.3.0MongoDB.Bson 2.3.0
在记录MongoDB日志时报错,内容如下

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 UuidStandard

文中给了三种解决方案:
1、修改全局配置 BsonDefaults.GuidRepresentation

BsonDefaults.GuidRepresentation = GuidRepresentation.Standard;

2、当你创建collection时指定配置

MongoDatabase db = ???;
string collectionName = ???;
var collectionSettings = new MongoCollectionSettings {
  GuidRepresentation = GuidRepresentation.Standard
};
var collection = db.GetCollection<BsonDocument>(collectionName, collectionSettings);

3、更新驱动
原文如下

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.0MongoDB.Driver.Core 2.3.0MongoDB.Bson 2.3.0 统统更新至2.7.0版本(注:当前最新的稳定版本)。然后,嘣!问题解决!

猜你喜欢

转载自www.cnblogs.com/AlienXu/p/9382857.html