How EF core outputs logs to the Output window of Visual Studio

Source: Internet
Author: User
Tags what sql



When we use EF core, we often need to know in the Output window of Visual Studio What SQL statements the EF core generates in the background, and this requirement can be achieved by customizing the Iloggerfactory and ILogger classes of EF Core:



First, we define a class Eflogger that implements the ILogger interface, the main purpose of which is to output the log information generated by EF core to the Output window of Visual Studio:


using Microsoft.Extensions.Logging;
using System;
using System.Diagnostics;
namespace WebCore.Utils
{
public class EFLogger : ILogger
{
protected string categoryName;
public EFLogger(string categoryName)
{
this.categoryName = categoryName;
}
public IDisposable BeginScope<TState>(TState state)
{
Return null;
}
public bool IsEnabled(LogLevel logLevel)
{
Return true;
}
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{
//Output the log information generated by EF core to the output window of visual studio through the debugger.log method
Debugger.Log(0, categoryName, "=============================== EF Core log started ===============================\r\n");
Debugger.Log(0, categoryName, formatter(state,exception)+"\r\n");
Debugger.Log(0, categoryName, "=============================== EF Core log finished ===============================\r\n");
}
}
} 


Then define a class efloggerfactory that implements the Iloggerfactory interface to create an instance of the Eflogger class defined above:


using Microsoft.Extensions.Logging;
namespace WebCore.Utils
{
public class EFLoggerFactory : ILoggerFactory
{
public void AddProvider(ILoggerProvider provider)
{
}
public ILogger CreateLogger(string categoryName)
{
Return new eflogger (CategoryName); / / create an instance of the eflogger class
}
public void Dispose()
{
}
}
} 


Finally, in DbContext's Onconfiguring method, call Optionsbuilder.useloggerfactory to inject an instance of the Efloggerfactory class to EF Core, The log information for all dbcontext will be output by the Eflogger class to the Output window of Visual Studio.


using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using WebCore.Utils;
namespace WebCore.Entities
{
public partial class TestDBContext : DbContext
{
public TestDBContext()
{
This. Database. Setcommandtimeout (0); / / set SqlCommand never to timeout
}
public TestDBContext(DbContextOptions<TestDBContext> options)
: base(options)
{
}
public virtual DbSet<Person> Person { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
Optionsbuilder. Useloggerfactory (New efloggerfactory()); / / inject an instance of the efloggerfactory class into the EF core, so that all the log information of dbcontext will be output by the eflogger class to the output window of visual studio
optionsBuilder.UseSqlServer("Server=localhost;User Id=sa;Password=1qaz!QAZ;Database=TestDB");
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
//Omit code
}
}
} 


Note that the Onconfiguring method is virtual, so we can also choose to override this method in the subclass of DbContext, injecting an instance of the Efloggerfactory class into DbContext in the onconfiguring of the subclass.






Finally, let's look at the effect of the EF core log output, which is pretty neat, and most importantly we can get the SQL statements generated by EF Core in the background in real time:









How EF core outputs logs to the Output window of Visual Studio


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.