Mongo C# driver (version 1.4.2) throw NullReferenceException from NLog.LogManager.GetCurrentClassLogger()
Error happen only in the Release build. Works fine with debug build.
Not sure if it is related to NLog vs. Structuremap
Stack trace,
System.NullReferenceException
at NLog.LogManager.GetCurrentClassLogger()
at lambda_method(Closure )
at MongoDB.Bson.Serialization.BsonClassMap.CreateInstance() in C:\work\10gen\mongodb\mongo-csharp-driver\Bson\Serialization\BsonClassMap.cs:line 429
at MongoDB.Bson.Serialization.BsonClassMapSerializer.Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) in C:\work\10gen\mongodb\mongo-csharp-driver\Bson\Serialization\BsonClassMapSerializer.cs:line 111
at MongoDB.Bson.Serialization.BsonClassMapSerializer.Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options) in C:\work\10gen\mongodb\mongo-csharp-driver\Bson\Serialization\BsonClassMapSerializer.cs:line 78
at MongoDB.Bson.Serialization.BsonSerializer.Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options) in C:\work\10gen\mongodb\mongo-csharp-driver\Bson\Serialization\BsonSerializer.cs:line 223
at MongoDB.Driver.Internal.MongoReplyMessage1.ReadFrom(BsonBuffer buffer, IBsonSerializationOptions serializationOptions) in C:\work\10gen\mongodb\mongo-csharp-driver\Driver\Internal\MongoReplyMessage.cs:line 105
1.GetReply(MongoConnection connection, MongoRequestMessage message) in C:\work\10gen\mongodb\mongo-csharp-driver\Driver\Core\MongoCursorEnumerator.cs:line 296
at MongoDB.Driver.Internal.MongoConnection.ReceiveMessage[TDocument](BsonBinaryReaderSettings readerSettings, IBsonSerializationOptions serializationOptions) in C:\work\10gen\mongodb\mongo-csharp-driver\Driver\Internal\MongoConnection.cs:line 442
at MongoDB.Driver.MongoCursorEnumerator
at MongoDB.Driver.MongoCursorEnumerator1.GetFirst() in C:\work\10gen\mongodb\mongo-csharp-driver\Driver\Core\MongoCursorEnumerator.cs:line 251
1.MoveNext() in C:\work\10gen\mongodb\mongo-csharp-driver\Driver\Core\MongoCursorEnumerator.cs:line 141
at MongoDB.Driver.MongoCursorEnumerator
at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)
The exception is not actually being thrown by the C# driver, but rather by some code outside the driver.
Looking at the stack trace, we can see that the C# driver has read a document from the database and is trying to deserialize it (see BsonSerializer.Deserialize on the stack trace). As part of the deserialization it needs to create an instance of your class (see BsonClassMapSerializer.CreateInstance on the stack trace). The constructor of the object (or possibly a static constructor of the object's class) is throwing a NullReferenceException.
You probably know what class you are using here and can go look at your source code to figure out why NLog.LogManager (which is not part of the C# driver) is throwing a NullReferenceException.