sql-server - 如何在 net core 3 中配置 Serilog MSSqlSink 为每个 Web 请求使用不同的连接字符串?
问题描述
我有一个访问单租户数据库的 .net core 3.1 多租户 Web API 应用程序。因此,每个请求都作用于n 个数据库中的任何一个。我配置了 EF Core DbContext,以便它从 http 请求中的声明中解析连接字符串,但我不知道为 Serilog 的 MSSqlSink 进行类似设置的正确方法。这是缩写的 DbContext 设置,可让您了解我要完成的工作:
services.AddDbContext<MyContext>((serviceProvider, options) =>
{
// IHttpContextAccessor enables us to get at the claims
var httpContext = serviceProvider.GetRequiredService<IHttpContextAccessor>().HttpContext;
var httpRequest = httpContext.Request;
var claim = httpContext.User.Claims.FirstOrDefault(x => x.Type == "MyClaim");
var customerIdentifier = claim.Value;
var connectionString = GetConnectionString(customerIdentifier);
options.UseSqlServer(connectionString);
});
到目前为止,我只能找到通过 appsettings 或环境变量使用已知的单个连接字符串设置 Serilog 的 MSSqlSink 的示例。
解决方案
推荐阅读
- c# - 我的自定义编辑器找不到我要查找的字段
- python - 如何使用不纯函数将单行从一个熊猫数据帧移动到另一个数据帧?
- python - kpathsea:运行 mktexfmt xelatex.fmt 找不到格式文件 `xelatex.fmt'
- matlab - 在matlab中拟合单变量数据的自定义分布
- nlp - 创建神经机器翻译基础知识
- airflow - 在 Airflow webUI 上创建连接时出错
- laravel - Laravel 会话数据库会话存在,但会话从浏览器检查会话中删除
- laravel - 如何从 laravel 中的数组中获取单个值?
- c++ - seekg 读到文件末尾
- c# - 只有在有观察者的情况下,如何生成数据?