Skip to content
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

Closed
ajcvickers opened this issue Dec 2, 2014 · 12 comments · Fixed by #18936
Closed

Simple Logging API for database interactions (similar to Database.Log) #1199

ajcvickers opened this issue Dec 2, 2014 · 12 comments · Fixed by #18936
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. punted-for-2.0 punted-for-3.0 type-enhancement
Milestone

Comments

@ajcvickers
Copy link
Member

ajcvickers commented Dec 2, 2014

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 like Context.Log for everything.

@leak
Copy link

leak commented Aug 27, 2015

Is there a simpler way for logging (e.g. to console for debugging) other than the example described in #2795 by now?

@rowanmiller
Copy link
Contributor

@leak - not yet, you still need to plug all the low level stuff together

@rowanmiller rowanmiller added the help wanted This issue involves technologies where we are not experts. Expert help would be appreciated. label Sep 29, 2015
@bricelam
Copy link
Contributor

BTW, just logging everything is relatively simple:

db.GetService<ILoggerFactory>().AddProvider(new ConsoleLoggerProvider());

@rowanmiller
Copy link
Contributor

@bricelam that needs to go in app startup right since the ServiceProvider is shared between context instances? Otherwise you add it multiple times.

@bricelam
Copy link
Contributor

Hmm yes, I think you're right...

@rowanmiller rowanmiller changed the title Consider something similar to Database.Log as very simple logging sugar Simple way to log generated SQL (similar to Database.Lo) Dec 8, 2015
@rowanmiller rowanmiller changed the title Simple way to log generated SQL (similar to Database.Lo) Simple way to log generated SQL (similar to Database.Log) Dec 8, 2015
@rowanmiller rowanmiller removed the pri1 label Dec 8, 2015
@rowanmiller rowanmiller modified the milestones: Backlog, 7.0.0 Dec 8, 2015
@rowanmiller
Copy link
Contributor

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

@rowanmiller rowanmiller changed the title Simple way to log generated SQL (similar to Database.Log) Simple way to get log (similar to Database.Log) Jan 17, 2017
@rowanmiller rowanmiller changed the title Simple way to get log (similar to Database.Log) Simple Logging API (similar to Database.Log) Jan 17, 2017
@rowanmiller rowanmiller modified the milestones: 2.0.0, Backlog Jan 17, 2017
@rowanmiller rowanmiller removed the help wanted This issue involves technologies where we are not experts. Expert help would be appreciated. label Jan 17, 2017
@mguinness
Copy link

Another method is described in this blog.

@ajcvickers ajcvickers modified the milestones: 2.0.0-preview1, 2.0.0 Apr 19, 2017
@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Jun 21, 2019
@ajcvickers ajcvickers modified the milestones: 3.0.0, Backlog Jun 21, 2019
@ajcvickers ajcvickers added punted-for-3.0 and removed closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. labels Jun 21, 2019
ajcvickers added a commit that referenced this issue Nov 15, 2019
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 | ));
```
@ajcvickers ajcvickers modified the milestones: Backlog, 5.0.0 Nov 15, 2019
ajcvickers added a commit that referenced this issue Nov 23, 2019
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 | ));
```
ajcvickers added a commit that referenced this issue Nov 25, 2019
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 | ));
```
ajcvickers added a commit that referenced this issue Nov 25, 2019
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 | ));
```
ajcvickers added a commit that referenced this issue Nov 25, 2019
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 | ));
```
ajcvickers added a commit that referenced this issue Nov 26, 2019
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 | ));
```
@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Dec 5, 2019
ajcvickers added a commit that referenced this issue Dec 8, 2019
#1199

* Stop exposing the interface in LogTo and replace with delegates overload.
* Rename SimpleLoggerFormatOptions to LogToOptions
  * Similar renaming across the internal implementation
ajcvickers added a commit that referenced this issue Dec 11, 2019
#1199

* Stop exposing the interface in LogTo and replace with delegates overload.
* Rename SimpleLoggerFormatOptions to LogToOptions
  * Similar renaming across the internal implementation
ajcvickers added a commit that referenced this issue Dec 11, 2019
#1199

* Stop exposing the interface in LogTo and replace with delegates overload.
* Rename SimpleLoggerFormatOptions to LogToOptions
  * Similar renaming across the internal implementation
ajcvickers added a commit that referenced this issue Dec 11, 2019
#1199

* Stop exposing the interface in LogTo and replace with delegates overload.
* Rename SimpleLoggerFormatOptions to LogToOptions
  * Similar renaming across the internal implementation
@ajcvickers ajcvickers modified the milestones: 5.0.0, 5.0.0-preview1 Mar 13, 2020
@ajcvickers ajcvickers modified the milestones: 5.0.0-preview1, 5.0.0 Nov 7, 2020
@ajcvickers ajcvickers removed their assignment Sep 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. punted-for-2.0 punted-for-3.0 type-enhancement
Projects
None yet
8 participants