Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Suppress SQL Queries logging in Entity Framework core

I have a console .net core app that uses entity framework core. The app uses logging framework to write to file and console:

 serviceProvider = new ServiceCollection()         .AddLogging()         .AddDbContext<DataStoreContext>(options =>             options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")))         .BuildServiceProvider();      //configure console logging     serviceProvider.GetService<ILoggerFactory>()         .AddConsole(LogLevel.Debug)         .AddSerilog();      Log.Logger = new LoggerConfiguration()         .MinimumLevel.Information()         .WriteTo.RollingFile(Path.Combine(Directory.GetCurrentDirectory(), "logs/vcibot-{Date}.txt"))         .WriteTo.RollingFile(Path.Combine(Directory.GetCurrentDirectory(), "logs/vcibot-errors-{Date}.txt"), LogEventLevel.Error)         .CreateLogger();      logger = serviceProvider.GetService<ILoggerFactory>()         .CreateLogger<Program>(); 

Min Level for file output is set to Information. But with this setup output also contains SQL queries, here is the example:

2017-02-06 10:31:38.282 -08:00 [Information] Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] SELECT [f].[BuildIdentifier], [f].[Branch], [f].[BuildDate], [f].[StaticAssetSizeInKb] FROM [FileSizesHistoryEntries] AS [f]

Is there a way to disable the SQL queries logging (log them only in Debug log level)

like image 601
vmg Avatar asked Feb 07 '17 01:02

vmg


People also ask

What is logging in EF Core?

Logging is an extensible logging mechanism with plug-in providers for many common logging systems. EF Core fully integrates with Microsoft. Extensions. Logging and this form of logging is used by default for ASP.NET Core applications. See Using Microsoft.

How can EF core prevent SQL injection?

To avoid the risk of SQL injection, you should never combine user input with Entity SQL command text. Entity SQL queries accept parameters everywhere that literals are accepted. You should use parameterized queries instead of injecting literals from an external agent directly into the query.

How to log sql with Entity Framework Core logging?

Entity Framework Core integrates with the .NET Core logging to log SQL and change tracking information to the various output targets. First, install the Nuget package for logging provider of your choice and then tie up the DbContext to ILoggerFactory.

How to filter logs in EF Core?

As you can see, it logs all the information. In the above example, the DbContext logged all the information while saving an entity. Sometime you don't want to log all the information and filter some unwanted logs. In EF Core, you can filter logs by specifying the logger category and log level.

How do I access EF Core logs from dbcontext?

EF Core logs can be accessed from any type of application through the use of LogTo when configuring a DbContext instance. This configuration is commonly done in an override of DbContext.OnConfiguring. For example: This concept is similar to Database.Log in EF6. See Simple Logging for more information.

How to integrate Entity Framework Core with ILogger?

Entity Framework Core integrates with the.NET Core logging to log SQL and change tracking information to the various output targets. First, install the Nuget package for logging provider of your choice and then tie up the DbContext to ILoggerFactory. Let's install the logging provider's NuGet package.


1 Answers

If you're using built-in logger, you can add filter to you ILoggingBuilder in Program.cs.

So, it can look like:

WebHost.CreateDefaultBuilder(args)     // ...     .ConfigureLogging((context, logging) => {         var env = context.HostingEnvironment;         var config = context.Configuration.GetSection("Logging");         // ...         logging.AddConfiguration(config);         logging.AddConsole();         // ...         logging.AddFilter("Microsoft.EntityFrameworkCore.Database.Command", LogLevel.Warning);     })     // ...     .UseStartup<Startup>()     .Build(); 
like image 130
lambidu Avatar answered Oct 09 '22 02:10

lambidu