简体   繁体   English

Serilog,将日志插入多租户应用程序中的特定数据库

[英]Serilog, insert log into specific database in a multi-tenant application

I have a WebAPI write in .net core 5, that connects to the correct database based on a key passed through JWT.我在 .net 核心 5 中编写了一个 WebAPI,它根据通过 JWT 传递的密钥连接到正确的数据库。

es. 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.现在我们要添加将日志插入各种数据库的 serilog。 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.但是我不知道如何告诉Serilog有动态连接字符串,此时只能插入一个连接字符串。

Is it possible to retrieve the connection strings for Serilog on the fly before entering the log?是否可以在输入日志之前即时检索 Serilog 的连接字符串?

It sounds like you need to create a custom implementation of Serilog's ILogEventSink .听起来您需要创建 Serilog 的ILogEventSink的自定义实现。 It defines a single method void Emit(LogEvent logEvent) which you would implement yourself to emit the log to the desired destination.它定义了一个方法void Emit(LogEvent logEvent) ,您可以自己实现该方法以将日志发送到所需的目的地。

Once that is done, and you are setting up your LoggerConfiguration , you would call myLoggerConfiguration.Sink(mySink, minLogLevel);完成后,您正在设置LoggerConfiguration ,您将调用myLoggerConfiguration.Sink(mySink, minLogLevel); to tell Serilog to log to that "sink".告诉 Serilog 登录到那个“接收器”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM