-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Simple Logging API for database interactions (similar to Database.Log) #1199
Comments
Is there a simpler way for logging (e.g. to console for debugging) other than the example described in #2795 by now? |
Feel free to reuse my naive extension method here: https://github.com/ErikEJ/EntityFramework7.SqlServerCompact/blob/master/src/Provider40/Extensions/SqlCeDbContextExtensions.cs and here: https://github.com/ErikEJ/EntityFramework7.SqlServerCompact/tree/master/src/Provider40/Extensions/Logging |
@leak - not yet, you still need to plug all the low level stuff together |
BTW, just logging everything is relatively simple: db.GetService<ILoggerFactory>().AddProvider(new ConsoleLoggerProvider()); |
@bricelam that needs to go in app startup right since the ServiceProvider is shared between context instances? Otherwise you add it multiple times. |
Hmm yes, I think you're right... |
FYI http://docs.efproject.net/en/latest/miscellaneous/logging.html shows how to achieve the same thing before we have full support for this feature |
Another method is described in this blog. |
Fixes #16200 Fixes #1199 Examples: Log to the Console: ```C# protected override void OnConfiguring(DbContextOptionsBuilder options) => options .UseMyProvider("...") .LogTo(Console.WriteLine); ``` Log to the Console for a given level: ```C# protected override void OnConfiguring(DbContextOptionsBuilder options) => options .UseMyProvider("...") .LogTo(Console.WriteLine, LogLevel.Information)); ``` Log to the Console for specific events: ```C# protected override void OnConfiguring(DbContextOptionsBuilder options) => options .UseMyProvider("...") .LogTo(Console.WriteLine, new[] { CoreEventId.ContextInitialized, CoreEventId.ContextDisposed })); ``` Log to the Console for specific categories: ```C# protected override void OnConfiguring(DbContextOptionsBuilder options) => options .UseMyProvider("...") .LogTo(Console.WriteLine, new[] { DbLoggerCategory.Infrastructure.Name, DbLoggerCategory.Update.Name })); ``` Log to the Console with custom filter: ```C# protected override void OnConfiguring(DbContextOptionsBuilder options) => options .UseMyProvider("...") .LogTo(Console.WriteLine, (e, l) => e == CoreEventId.SaveChangesCompleted))); ``` Log to the Console for events in specific categories and a given level formatted as a single line using UTC timestamps: ```C# protected override void OnConfiguring(DbContextOptionsBuilder options) => options .UseMyProvider("...") .LogTo( Console.WriteLine, new[] { DbLoggerCategory.Infrastructure.Name, DbLoggerCategory.Update.Name }, LogLevel.Information, SimpleLoggerFormatOptions.SingleLine | SimpleLoggerFormatOptions.DefaultWithUtcTime | )); ```
Fixes #16200 Fixes #1199 Examples: Log to the Console: ```C# protected override void OnConfiguring(DbContextOptionsBuilder options) => options .UseMyProvider("...") .LogTo(Console.WriteLine); ``` Log to the Console for a given level: ```C# protected override void OnConfiguring(DbContextOptionsBuilder options) => options .UseMyProvider("...") .LogTo(Console.WriteLine, LogLevel.Information)); ``` Log to the Console for specific events: ```C# protected override void OnConfiguring(DbContextOptionsBuilder options) => options .UseMyProvider("...") .LogTo(Console.WriteLine, new[] { CoreEventId.ContextInitialized, CoreEventId.ContextDisposed })); ``` Log to the Console for specific categories: ```C# protected override void OnConfiguring(DbContextOptionsBuilder options) => options .UseMyProvider("...") .LogTo(Console.WriteLine, new[] { DbLoggerCategory.Infrastructure.Name, DbLoggerCategory.Update.Name })); ``` Log to the Console with custom filter: ```C# protected override void OnConfiguring(DbContextOptionsBuilder options) => options .UseMyProvider("...") .LogTo(Console.WriteLine, (e, l) => e == CoreEventId.SaveChangesCompleted))); ``` Log to the Console for events in specific categories and a given level formatted as a single line using UTC timestamps: ```C# protected override void OnConfiguring(DbContextOptionsBuilder options) => options .UseMyProvider("...") .LogTo( Console.WriteLine, new[] { DbLoggerCategory.Infrastructure.Name, DbLoggerCategory.Update.Name }, LogLevel.Information, SimpleLoggerFormatOptions.SingleLine | SimpleLoggerFormatOptions.DefaultWithUtcTime | )); ```
Fixes #16200 Fixes #1199 Examples: Log to the Console: ```C# protected override void OnConfiguring(DbContextOptionsBuilder options) => options .UseMyProvider("...") .LogTo(Console.WriteLine); ``` Log to the Console for a given level: ```C# protected override void OnConfiguring(DbContextOptionsBuilder options) => options .UseMyProvider("...") .LogTo(Console.WriteLine, LogLevel.Information)); ``` Log to the Console for specific events: ```C# protected override void OnConfiguring(DbContextOptionsBuilder options) => options .UseMyProvider("...") .LogTo(Console.WriteLine, new[] { CoreEventId.ContextInitialized, CoreEventId.ContextDisposed })); ``` Log to the Console for specific categories: ```C# protected override void OnConfiguring(DbContextOptionsBuilder options) => options .UseMyProvider("...") .LogTo(Console.WriteLine, new[] { DbLoggerCategory.Infrastructure.Name, DbLoggerCategory.Update.Name })); ``` Log to the Console with custom filter: ```C# protected override void OnConfiguring(DbContextOptionsBuilder options) => options .UseMyProvider("...") .LogTo(Console.WriteLine, (e, l) => e == CoreEventId.SaveChangesCompleted))); ``` Log to the Console for events in specific categories and a given level formatted as a single line using UTC timestamps: ```C# protected override void OnConfiguring(DbContextOptionsBuilder options) => options .UseMyProvider("...") .LogTo( Console.WriteLine, new[] { DbLoggerCategory.Infrastructure.Name, DbLoggerCategory.Update.Name }, LogLevel.Information, SimpleLoggerFormatOptions.SingleLine | SimpleLoggerFormatOptions.DefaultWithUtcTime | )); ```
Fixes #16200 Fixes #1199 Examples: Log to the Console: ```C# protected override void OnConfiguring(DbContextOptionsBuilder options) => options .UseMyProvider("...") .LogTo(Console.WriteLine); ``` Log to the Console for a given level: ```C# protected override void OnConfiguring(DbContextOptionsBuilder options) => options .UseMyProvider("...") .LogTo(Console.WriteLine, LogLevel.Information)); ``` Log to the Console for specific events: ```C# protected override void OnConfiguring(DbContextOptionsBuilder options) => options .UseMyProvider("...") .LogTo(Console.WriteLine, new[] { CoreEventId.ContextInitialized, CoreEventId.ContextDisposed })); ``` Log to the Console for specific categories: ```C# protected override void OnConfiguring(DbContextOptionsBuilder options) => options .UseMyProvider("...") .LogTo(Console.WriteLine, new[] { DbLoggerCategory.Infrastructure.Name, DbLoggerCategory.Update.Name })); ``` Log to the Console with custom filter: ```C# protected override void OnConfiguring(DbContextOptionsBuilder options) => options .UseMyProvider("...") .LogTo(Console.WriteLine, (e, l) => e == CoreEventId.SaveChangesCompleted))); ``` Log to the Console for events in specific categories and a given level formatted as a single line using UTC timestamps: ```C# protected override void OnConfiguring(DbContextOptionsBuilder options) => options .UseMyProvider("...") .LogTo( Console.WriteLine, new[] { DbLoggerCategory.Infrastructure.Name, DbLoggerCategory.Update.Name }, LogLevel.Information, SimpleLoggerFormatOptions.SingleLine | SimpleLoggerFormatOptions.DefaultWithUtcTime | )); ```
Fixes #16200 Fixes #1199 Examples: Log to the Console: ```C# protected override void OnConfiguring(DbContextOptionsBuilder options) => options .UseMyProvider("...") .LogTo(Console.WriteLine); ``` Log to the Console for a given level: ```C# protected override void OnConfiguring(DbContextOptionsBuilder options) => options .UseMyProvider("...") .LogTo(Console.WriteLine, LogLevel.Information)); ``` Log to the Console for specific events: ```C# protected override void OnConfiguring(DbContextOptionsBuilder options) => options .UseMyProvider("...") .LogTo(Console.WriteLine, new[] { CoreEventId.ContextInitialized, CoreEventId.ContextDisposed })); ``` Log to the Console for specific categories: ```C# protected override void OnConfiguring(DbContextOptionsBuilder options) => options .UseMyProvider("...") .LogTo(Console.WriteLine, new[] { DbLoggerCategory.Infrastructure.Name, DbLoggerCategory.Update.Name })); ``` Log to the Console with custom filter: ```C# protected override void OnConfiguring(DbContextOptionsBuilder options) => options .UseMyProvider("...") .LogTo(Console.WriteLine, (e, l) => e == CoreEventId.SaveChangesCompleted))); ``` Log to the Console for events in specific categories and a given level formatted as a single line using UTC timestamps: ```C# protected override void OnConfiguring(DbContextOptionsBuilder options) => options .UseMyProvider("...") .LogTo( Console.WriteLine, new[] { DbLoggerCategory.Infrastructure.Name, DbLoggerCategory.Update.Name }, LogLevel.Information, SimpleLoggerFormatOptions.SingleLine | SimpleLoggerFormatOptions.DefaultWithUtcTime | )); ```
Fixes #16200 Fixes #1199 Examples: Log to the Console: ```C# protected override void OnConfiguring(DbContextOptionsBuilder options) => options .UseMyProvider("...") .LogTo(Console.WriteLine); ``` Log to the Console for a given level: ```C# protected override void OnConfiguring(DbContextOptionsBuilder options) => options .UseMyProvider("...") .LogTo(Console.WriteLine, LogLevel.Information)); ``` Log to the Console for specific events: ```C# protected override void OnConfiguring(DbContextOptionsBuilder options) => options .UseMyProvider("...") .LogTo(Console.WriteLine, new[] { CoreEventId.ContextInitialized, CoreEventId.ContextDisposed })); ``` Log to the Console for specific categories: ```C# protected override void OnConfiguring(DbContextOptionsBuilder options) => options .UseMyProvider("...") .LogTo(Console.WriteLine, new[] { DbLoggerCategory.Infrastructure.Name, DbLoggerCategory.Update.Name })); ``` Log to the Console with custom filter: ```C# protected override void OnConfiguring(DbContextOptionsBuilder options) => options .UseMyProvider("...") .LogTo(Console.WriteLine, (e, l) => e == CoreEventId.SaveChangesCompleted))); ``` Log to the Console for events in specific categories and a given level formatted as a single line using UTC timestamps: ```C# protected override void OnConfiguring(DbContextOptionsBuilder options) => options .UseMyProvider("...") .LogTo( Console.WriteLine, new[] { DbLoggerCategory.Infrastructure.Name, DbLoggerCategory.Update.Name }, LogLevel.Information, SimpleLoggerFormatOptions.SingleLine | SimpleLoggerFormatOptions.DefaultWithUtcTime | )); ```
#1199 * Stop exposing the interface in LogTo and replace with delegates overload. * Rename SimpleLoggerFormatOptions to LogToOptions * Similar renaming across the internal implementation
#1199 * Stop exposing the interface in LogTo and replace with delegates overload. * Rename SimpleLoggerFormatOptions to LogToOptions * Similar renaming across the internal implementation
#1199 * Stop exposing the interface in LogTo and replace with delegates overload. * Rename SimpleLoggerFormatOptions to LogToOptions * Similar renaming across the internal implementation
#1199 * Stop exposing the interface in LogTo and replace with delegates overload. * Rename SimpleLoggerFormatOptions to LogToOptions * Similar renaming across the internal implementation
This would make use of the core logging services without the need to do any wire-up.
We may want a
Database.Log
for SQL and then something likeContext.Log
for everything.The text was updated successfully, but these errors were encountered: