I have a local MongoDb database instance (created by running mongod from the Windows command line), and a simple console program that tries to log a string to the MongoDb database using Serilog and its MongoDb sink:
I have a local MongoDb database instance (created by running mongod from the Windows command line), and a simple console program that attempts to use Serilog and Its MongoDb sink logs strings to the MongoDb database:
var log = new LoggerConfiguration() .MinimumLevel.Debug() .WriteTo.ColoredConsole() .WriteTo.MongoDB("mongodb://localhost/mydb") .CreateLogger(); log.Fatal("Fatal message");
The “Fatal message” message is written correctly to the console, but not to my MongoDb database.
The “fatal message” message is correctly written to the console, but not to my MongoDb database.
My current MongoDb database is “mydb”. According to “show collections”, I only have collections system.indexes and testData, and “db.testData.find()” produces nothing.
My current MongoDb database is “mydb”. According to “show collections”, I only have collections system. Indexing and testData and “db.testData.find()” produce no results.
The Serilog site says to use connection string “mongo://mydb/log”, but that throws an exception “An unhandled exception of type ‘System.FormatException’ occurred in MongoDB.Driver.dll”. The connection string I used in my code is specified on the MongoDb site, at http://docs.mongodb.org/manual/reference/connection-string/
The Serilog site says to use the connection string “mongo://mydb/log”, but this throws an exception “Unhandled exception of type ‘system” . MongoDB.Driver.dll FormatException “occurred”. The connection string I use in the code is specified on the MongoDb site (http://docs.mongodb.org/manual/reference/connecting-string/)
How can I log to MongoDb using Serilog?
How to use Serilog to log in to MongoDb?
It’s possible that you’re just experiencing the default (two second?) buffering delay – if you close a console app in Windows it hard-terminates the program so buffers can’t always be flushed. Waiting a few seconds before closing the app will fix this if so.
You may just be experiencing the default buffering delay (2 seconds?) – if you close a console application in Windows, it will hard terminate the program , so the buffer cannot always be flushed. If so, waiting a few seconds before closing the app may resolve the issue.
Otherwise, the way to tackle all sink debugging in Serilog is to set
Otherwise, the way to handle all sink debugging in Serilog is to set selflof . out:
SelfLog.Out = Console.Error;
This will print any exceptions raised by the sink, allowing you to zoom in on the error pretty quickly if that’s where it is.
This will print any exceptions caused by the sink, allowing you to quickly zoom in on the error if it is there.
I took the answers by Nicholas and redwards (thanks to both of you!), did some more experimenting, and captured the results at: http://jsnlog.com/Documentation/GetStartedLogging/StructuredLogging
I accepted the answers from Nicholas and Redward (thanks to both of you!), did some more experiments, and posted them in http://jsnlog. com/Documentation/GetStartedLogging/StructuredLoggingGet results
Interesting, I was getting the exact same error until I switched the connection string to be
Interestingly, I got the same error until I converted the connection string to
mongodb:/ / myMachineName / mydb
Before, I had mongo://localhost/mydb.
Before, I had mongo://localhost/mydb.
But while mongo vs mongodb solved the Exception problem, it wasn’t inserting records into mydb! So I changed it to
Although mongo vs mongodb solved the exception issue, it didn’t insert the record into mydb! So I changed it to
.WriteTo.MongoDB(“mongodb://localhost/logs”, restrictedToMinimumLevel: LogEventLevel.Verbose)
and now it is working! “logs” was created with “log” as a collection which held the JSON data.
Now it starts working! “logs” is created using “log” as a collection holding JSON data.