使用EF Core 2和Nlog记录生成的SQL [英] Logging Generated SQL with EF Core 2 and Nlog

查看:786
本文介绍了使用EF Core 2和Nlog记录生成的SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对如何使用asp.net core 2和EntityFrameworkCore 2记录生成的SQL以及正确的处理方法感到困惑.

I'm getting a little confused with how to log the generated SQL with asp.net core 2 and EntityFrameworkCore 2 and the correct way to go about it.

从MS文档中阅读此链接之后,是说我应该在startup.cs的服务配置期间使用.UseLoggerFactory(<LoggerFactory>)添加.

After reading the this link from the MS docs it is saying that I should add during the services configuration in the startup.cs using .UseLoggerFactory(<LoggerFactory>).

但是,这似乎已过时,因为当我希望添加记录器时,我收到此消息;

However, this seems outdated as when I look to add the logger I get this message;

有人可以告诉我添加记录器以记录SQL以便进行调试的最佳方法吗?

Could someone please tell me the best way to add the logger to log the SQL for debug purposes?

我还计划继续使用Nlog进行所有日志记录,而不是使用内置的日志记录工具,我假设这样做的方法(因为注入了Nlog loggerfactory而不是默认的MS实例) (在使用NLog方面)有什么配置差异吗?

I also plan to use Nlog going forward for all my logging as opposed to the built in logging facilities, I assume the approach (In that the Nlog loggerfactory is injected instead of the default MS instance) for this would be the same or are there any configuration differences (with regards to using NLog)?

推荐答案

首先按照然后在您的Startup类中插入一个ILoggerFactory

Then inject an ILoggerFactory in your Startup class

private readonly ILoggerFactory _factory;
public Startup(IHostingEnvironment env, ILoggerFactory factory)
{ 
     _factory = factory ?? throw new ArgumentNullException(nameof(factory)); 
}

最后将工厂链接为记录器工厂,以用于DbContext

Finally link the factory as the logger factory to use for the DbContext

public void ConfigureServices(IServiceCollection services)
{
     services.AddDbContext<YourContext>(options => { options.UseLoggerFactory(_factory); });
}

EF Core语句现在使用NLog记录

EF Core statements are now logged using NLog

这篇关于使用EF Core 2和Nlog记录生成的SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆