Logging in EF Core 2.2 Has a Simpler Syntax–More like ASP.NET Core

Logging EF Core's memory operations and SQL operations has evolved a few times since EF Core arrived.它利用了与ASP.NET核心使用的相同的底层功能。如果您使用的是ASP.NET核心,logging is baked in and it is really simple to turn it on for EF Core and add filtering.See Shawn Wildermuth's blog post aboutEF Core logging in ASP.NET Core.

但是如果您不使用ASP.NET核心,it's a little more complicated.Not terribly,but still there's some extra work to do.It involves setting up an ILoggerFactory in your DbContext and defining any filters at the same time.

I wrote an article about this (with the focus being on taking advantage of the various available filters for EF Core logging) in MSDN Magazine earlier this …oh wait,今天是1月1日,so I can say "last  year".数据点–在EF核心中记录SQL和更改跟踪事件.我也在我的EF核心2入门course,EF Core 2:MappingsandEF Core 2.1: What's Newcourses on Pluralsight.(请注意,我已经将入门课程的示例代码更新为ef core 2.2,并将其放到了github上github.com/julielerman/multiplesightefcore2getingstarted

我的文章和课程使用控制台应用程序来演示EF核心行为,因此控制台日志提供者将日志程序与控制台联系起来。请注意,数据点文章包含了许多关于各种过滤类型的详细信息。So you can use the new syntax (below) to specify that there should be a filter,but be sure to read the article to learn about the flavors of filtering and what type of details you'll be able to see based on the choices you make.

但是日志API不断发展,并提供了一些与ASP.NET创建的相同的快捷方式。并且consoleloggerprovider已被弃用。API不是EF核心的一部分。It's part of .NET Core.EF核心和ASP.NET核心都使用它。

如果您使用的是EF Core 2.2,语法已经改变了(简化了),在3.0中它将变得更加流线型。

In fact,如果将前面的语法与2.2一起使用,you'll get a warning about the ConsoleLoggerProvider:

Obsolete("This method is obsolete and will be removed in a future version.The recommended alternative is using LoggerFactory to configure filtering and ConsoleLoggerOptions to configure logging options.")

For a point of comparison,下面是一个使用旧语法打开日志记录的示例,只显示与数据库命令相关的日志,只显示标记为“信息”的消息。

EF Core 2.0 & 2.1 Logic

public static readonly loggerfactory myconsoleloggerfactory=new loggerfactory(new[]new consoleloggerprovider((category,level)=>category==dbloggercategory.database.command.name&&level==loglevel.information,真实)};

Once your logger factory field is defined in the context class you tell the DbContext to use it when configuring.

protected override void onconfiguring(dbContextOptionsBuilder OptionsBuilder)var connectionString=configurationManager.connectionStrings[“wpfdatabase”].toString();选项builder.useloggerfactory(myconsoleloggerfactory)。启用敏感数据记录(true)。usesqlserver(connectionString);}

所以这是记录器工厂的创建,它的语法有点复杂。The newer API follows how ASP.NET Core lets you filter with an AddFilter method that takes the filters as parameters.No lambdas needed.Also configuring the filter is a separate bit of logic that tellig the logger that it should be tied to the console.

EF Core 2.2 Logic

有了EF核心2.2,you can set up the logger factory in the constructor or another method as long as it's available when you are configuring the option builder.I'm creating it in a method then using that method as a parameter of UseLoggerFactory.I'm still filtering on showing only database commands and log details flagged as Information.

private ILoggerFactory GetLoggerFactory()  {    IServiceCollection serviceCollection = new ServiceCollection();serviceCollection.AddLogging(builder =>           builder.AddConsole()                  .AddFilter(DbLoggerCategory.Database.Command.Name,loglevel.information));return serviceCollection.BuildServiceProvider()            .GetService
             
              ();}
             

and then I'm calling GetLoggerFactory() in the UseLogging method on the optionsbuilder:

optionsBuilder.UseLoggerFactory(GetLoggerFactory())

Packages and References

In order to use the AddConsole() method,you still have to use theMicrosoft.Extensions.Logging.Console以前的consoleloggerprovider所在的包。然而,命名空间不需要using语句(就像对consoleloggerprovider所做的那样)。

报名参加我的时事通讯so you don't miss my conference & Pluralsight course announcements!

One thought on "Logging in EF Core 2.2 Has a Simpler Syntax–More like ASP.NET Core"

  1. Hi,

    请为ef core 2.2日志逻辑共享一个简单完整的工作示例。尝试在Xamarin表单中使用ef core 2.2逻辑/ef core应用程序数据库上下文类,使用vs 2017社区和控制台窗口甚至没有启动。
    Apart from this piece of code,is there anything else that needed be added?

Leave a Reply

Your email address will not be published.Required fields are marked*

此网站使用Akismet来减少垃圾邮件。Learn how your comment data is processed.