I have a WebAPI write in .net core 5, that connects to the correct database based on a key passed through JWT.
es.
--> Key = A dbconnection string "Data Source=1.1.1.1;Database=A;Integrated Security=SSPI;MultipleActiveResultSets=true"
--> Key = B dbconnection string "Data Source=1.1.1.1;Database=B;Integrated Security=SSPI;MultipleActiveResultSets=true"
--> Key = B dbconnection string "Data Source=1.1.1.1;Database=C;Integrated Security=SSPI;MultipleActiveResultSets=true"
and so on...
The connections are stored in another database which is used for general configurations, including connection strings for various clients.
Now we want to add serilog that inserts the logs into the various databases. But I can't figure out how to tell Serilog to have dynamic connection strings, at this moment it is only possible to insert one connection string.
Is it possible to retrieve the connection strings for Serilog on the fly before entering the log?
It sounds like you need to create a custom implementation of Serilog's ILogEventSink
. It defines a single method void Emit(LogEvent logEvent)
which you would implement yourself to emit the log to the desired destination.
Once that is done, and you are setting up your LoggerConfiguration
, you would call myLoggerConfiguration.Sink(mySink, minLogLevel);
to tell Serilog to log to that "sink".