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)
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.
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.
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.
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.
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.
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.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With