C# 将DbContext注入LoggerProvider会在.NET内核中引发StackOverflowException

C# 将DbContext注入LoggerProvider会在.NET内核中引发StackOverflowException,c#,logging,dependency-injection,.net-core,entity-framework-core,C#,Logging,Dependency Injection,.net Core,Entity Framework Core,我正在使用.NETCore2.2和EntityFrameworkCore。我想使用entityframework在数据库中写入日志。所以我尝试将DbContext注入LoggerProvider //主要功能 new WebHostBuilder().ConfigureLogging((hostingContext, logging) => { logging.ClearProviders();

我正在使用.NETCore2.2和EntityFrameworkCore。我想使用entityframework在数据库中写入日志。所以我尝试将DbContext注入LoggerProvider

//主要功能

new WebHostBuilder().ConfigureLogging((hostingContext, logging) =>
{
    logging.ClearProviders();                                        
    logging.AddDatabase(hostingContext.Configuration);
}).UseStartup<Startup>();
//LoggerConfigurationOptions

public class LoggerConfigurationOptions : ConfigureFromConfigurationOptions<LoggerOptions>
{
    public LoggerConfigurationOptions(ILoggerProviderConfiguration<DatabaseLoggerProvider> providerConfiguration) : base(providerConfiguration.Configuration)
    {
    }
}
公共类LoggerConfigurationOptions:ConfigureFromConfigurationOptions
{
公共日志配置选项(ILogerProviderConfiguration providerConfiguration):基本(providerConfiguration.Configuration)
{
}
}
//日志上下文

public class LoggingContext : DbContext
{
    public LoggingContext(DbContextOptions<LoggingContext> options) : base(options) //In base constructor exception is thrown
    {
    }
}
公共类LoggingContext:DbContext
{
public LoggingContext(DbContextOptions选项):引发基本构造函数中的基本(选项)//异常
{
}
}
//记录器提供程序

[Microsoft.Extensions.Logging.ProviderAlias("Database")]
public class DatabaseLoggerProvider : ILoggerProvider
{
    public DatabaseLoggerProvider(IOptionsMonitor<LoggerOptions> Settings, LoggingContext context) //I cannot inject context here
    {
    }
}
[Microsoft.Extensions.Logging.ProviderAlias(“数据库”)]
公共类DatabaseLoggerProvider:ILoggerProvider
{
公共数据库LoggerProvider(IOptionsMonitor设置,LoggingContext)//我无法在此处注入上下文
{
}
}

问题是DbContext构造函数抛出
StackOverflowException
。有人能告诉我哪里做错了吗?

使用LoggingContext的人应该创建一个新实例,可能在构造函数或基类构造函数中。

使用LoggingContext的人应该创建一个新实例,可能在构造函数或基类构造函数中。

所以您将上下文注入到提供程序中,但是第一个DI需要将选项注入上下文,但是第一个DI需要将
提供程序注入
选项,但是第一个DI需要将上下文注入提供程序,但是第一个DI需要将选项注入上下文*,但是第一个DI需要将
提供者注入
选项
。@CaiusJard,我明白你的意思。但是这个问题的解决方案是什么呢?所以您将上下文注入到提供程序中,但是第一个DI需要将选项注入到上下文中,但是第一个DI需要将
提供程序注入到
选项中,但是第一个DI需要将上下文注入到提供程序中,但是第一个DI需要将选项注入到上下文中*,但是第一个DI需要将
提供者注入
选项
。@CaiusJard,我明白你的意思。但这个问题的解决方案是什么呢?让我进一步澄清,
DatabaseLoggerProvider
将上下文实例传递给
DatabaseLogger
。然后在
数据库记录器的
Log
方法中使用它。我无法将其注入其中。让我进一步澄清,
DatabaseLoggerProvider
将上下文实例传递给
DatabaseLogger
。然后在
数据库记录器的
Log
方法中使用它。我也不能注射。
public class LoggingContext : DbContext
{
    public LoggingContext(DbContextOptions<LoggingContext> options) : base(options) //In base constructor exception is thrown
    {
    }
}
[Microsoft.Extensions.Logging.ProviderAlias("Database")]
public class DatabaseLoggerProvider : ILoggerProvider
{
    public DatabaseLoggerProvider(IOptionsMonitor<LoggerOptions> Settings, LoggingContext context) //I cannot inject context here
    {
    }
}