简体   繁体   English

是否有任何上下文。数据库.NET核心

[英]is there any context.Database.Log in .net core

In entity framework you can direct sql out on debug with this command 在实体框架中,您可以使用此命令在调试时将sql导出

context.Database.Log = sql => Debug.WriteLine(sql);

But I cannot find any .Net core alternative. 但是我找不到任何.Net核心替代品。 Does anyone know how this is achieved in .Net core? 有谁知道这是如何在.Net core中实现的?

I found solution here : 我在这里找到解决方案:

public static class DbContextExtensions
{
    public static void LogToConsole(this DbContext context)
    {
        var contextServices = ((IInfrastructure<IServiceProvider>) context).Instance;
        var loggerFactory = contextServices.GetRequiredService<ILoggerFactory>();
        loggerFactory.AddConsole(LogLevel.Verbose);
    }
}

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

相关问题 .net 核心 api 数据库上下文 foreach - .net core api database context foreach 数据库连接上下文是否应该是使用MongoDB的.net核心应用程序中的Singleton? - Should the database connexion Context be a Singleton in a .net core app using MongoDB? 无法访问数据库上下文的模型属性 - .net core (3.1) - Unable to access model properties of database context - .net core (3.1) ASP.NET Core和EF应用程序在初始化数据库上下文时停止 - ASP.NET Core and EF application stops at initialising Database context Web API [Net.Core]上的授权失败时登录数据库 - Log to database when authorization fail on Web API [Net.Core] 在.Net Core 中使用术语“上下文” - Use of the term “context” in .Net Core .Net Core Context ServiceLifetime 类型 - .Net Core Context ServiceLifetime types 在 asp.net core 3.1 中使用 log4net 和 Microknights 登录数据库不起作用 - Logging into Database using log4net and Microknights in asp.net core 3.1 didn't work .Net Core 2.0 Web Api将数据库上下文移动到DAL类库后的循环依赖 - .Net Core 2.0 Web Api circular dependency after moving database context to DAL class library 在 .NET Core 3 中记录请求有效负载 - Log request payloads in .NET Core 3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM