Logging
Oracle EF Core integrates directly with EF Core logging mechanisms. Oracle EF Core logs are filtered using DbLoggerCategory
class and LogLevel
enumeration.
The following DbLoggerCategory
properties can be used to filter the logs:
-
Database
-
Database.Command
-
Database.Connection
-
DbLoggerCategory.Infrastructure
-
Migrations
-
Model
-
Model.Validation
-
Query
-
Scaffolding
-
Update
The following LogLevel
properties are available:
-
Debug
: Displays entry and exit traces. Also displays key Oracle EF Core activities and metadata, such as SQL executed, table and column metadata, and mappings. -
Error
: Displays error related information, including the stack trace. -
None
To setup logging using DebugLoggerProvider
, include the Microsoft.Extensions.Logging.Debug.dll
assembly in your project. Similarly to setup logging using ConsoleLoggerProvider
, include the Microsoft.Extensions.Logging.Console.dll
assembly in your project. Next, add the following namespace in your project:
using Microsoft.Extensions.Logging;
Next, configure the DbContext
to use the logger factory.
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseOracle(@<connection string>) .UseLoggerFactory(_myLoggerFactory); }
Finally, set the DbLoggerCategory
and LogLevel
properties that you would like to be written to the log. In an EnsureCreated
scenario, you can create the following LoggerFactory
:
public static readonly ILoggerFactory _myLoggerFactory = LoggerFactory.Create(builder => { builder .AddFilter((category, level) => category == DbLoggerCategory.Database.Name && // Filter the logs based on DbLoggerCategory. Comment this line above if you do not want to filter logs based on DbLoggerCategory. level >= LogLevel.Trace // Filter the logs based on LogLevel. All levels greater than or equal to "Trace" will be enabled. Comment this line above if you do not want to filter logs based on LogLevel. ) .AddDebug(); });
In a migrations scenario, you can create the following LoggerFactory
:
public static readonly ILoggerFactory _myLoggerFactory = LoggerFactory.Create(builder => { builder .AddFilter((category, level) => category == DbLoggerCategory.Database.Command.Name && // Filter the logs based on DbLoggerCategory. Comment this line above if you do not want to filter logs based on DbLoggerCategory. level >= LogLevel.Trace // Filter the logs based on LogLevel. All levels greater than or equal to "Trace" will be enabled. Comment this line above if you do not want to filter logs based on LogLevel. ) .AddConsole(); });
In Scaffolding scenario, use the -verbose
option to generate traces.
Scaffold-DbContext .. -verbose