Home > Software engineering >  How can I set the name of the Database using Serilog.Sink.MongoDB in appsettings.json in ASP.net/Bla
How can I set the name of the Database using Serilog.Sink.MongoDB in appsettings.json in ASP.net/Bla

Time:10-10

I have my logger Serilog setup to write to mongodb via serilog.sink.Mongodb. I use the configuration in appsettings.json. It looks like this:

      {
    "Name": "MongoDB",
    "Args": {
      "databaseUrl": "mongodb srv://AtlasUser:XXXXXXX/test?retryWrites=true&w=majority&ssl=true",
      "databaseName": "MyLogs",
      "collectionName": "MyLoggerCollection",
      "cappedMaxSizeMb": "1024",
      "cappedMaxDocuments": "50000"
    }

XXXXX=SecretPart of my Connectionstring

This results in a Database with the name 'test' and in there a Collection with name 'MyLoggerCollection'.

The config-part: "databaseName": "MyLogs", has no effect. The database created is still named 'test' (I guess the default). How can I set the DB-Name to whatever I want? Is it a setting-pair in the datanbaseUrl?

CodePudding user response:

You're specifying the database name as test in your connection string, which is where the database name used comes from - not the databaseName property.

"databaseUrl": "mongodb://username:password@ip:port**/dbName?authSource=admin"`

This should work:

{
  "Name": "MongoDB",
  "Args": {
  "databaseUrl": "mongodb srv://AtlasUser:XXXXXXX/MyLogs?retryWrites=true&w=majority&ssl=true",
  "collectionName": "MyLoggerCollection",
  "cappedMaxSizeMb": "1024",
  "cappedMaxDocuments": "50000"
}
  • Related