Home > Mobile >  How can I set the database name using Serilog.Sink.MongoDB in appsettings.json in ASP.NET (Blazor)?
How can I set the database name using Serilog.Sink.MongoDB in appsettings.json in ASP.NET (Blazor)?

Time:10-12

I have Serilog set up to write to MongoDB via Serilog.Sinks.MongoDB.

I'm using appsettings.json for my configuration (with my password masked by XXXXXXX):

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

This results in a database with the name test and a collection called 'MyLoggerCollection'.

"databaseName": "MyLogs" seems to be taking no effect.

The database created is still named test (I guess the default?).

How can I set the database name to MyLogs?

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