使用 EF Core 2 和 Nlog 记录生成的 SQL [英] Logging Generated SQL with EF Core 2 and Nlog
问题描述
我对如何使用 asp.net core 2 和 EntityFrameworkCore 2 记录生成的 SQL 以及正确的方法感到有些困惑.
从 MS 文档中阅读此
有人可以告诉我添加记录器以记录 SQL 以进行调试的最佳方法吗?
我还计划将 Nlog 用于我的所有日志记录,而不是内置的日志记录工具,我假设这种方法(因为 Nlog loggerfactory 被注入而不是默认的 MS 实例)将是相同的或是否有任何配置差异(关于使用 NLog)?
首先按照
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.
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;
Could someone please tell me the best way to add the logger to log the SQL for debug purposes?
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)?
First register NLog as described on the official docs
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));
}
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 statements are now logged using NLog
这篇关于使用 EF Core 2 和 Nlog 记录生成的 SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!