ChangemakerStudios / serilog-sinks-mongodb

A sink for Serilog that writes events to MongoDB
Apache License 2.0
92 stars 53 forks source link

.NET type System.Uri cannot be mapped to a BsonValue. #74

Closed wuzhenda closed 2 years ago

wuzhenda commented 2 years ago

when I use masstransit with serilong-sinks-mongodb, the masstranist log the host info, then the exception happened.

I debug the source code and find out that the Bson convert with url make that exception happened. image

wuzhenda commented 2 years ago

the serilog-sinks-mongodb version is 5.1.2; the setting is { "Name": "MongoDBBson", "Args": { "databaseUrl": "mongodb://1.1.1.1", "collectionName": "logdebug", "cappedMaxSizeMb": "1024", "cappedMaxDocuments": "50" } }

wuzhenda commented 2 years ago

" Timespan" type will cause the exception too.

so I changed the code from line 54 in the MongoDbDocumentHelpers.cs :

            if (value is ScalarValue scalar)
            {
                if(scalar.Value is Uri)
                {
                    return BsonValue.Create(scalar.Value.ToString());
                }

                if (scalar.Value is TimeSpan)
                {
                    return BsonValue.Create(scalar.Value.ToString());
                }

                return BsonValue.Create(scalar.Value);
            }
wuzhenda commented 2 years ago

when we use DI to other 3rd lib,we don't know what they output to the log, so maybe there are other types will lead to the exceptions.

wuzhenda commented 2 years ago

I think there maybe other type will cause the exception. and the exception stoped all the next logs.

maybe we should catch other exceptions or turn all the ScalarValue type to string then to BsonValue?