Skip to content

Commit

Permalink
Another try at simple logging...
Browse files Browse the repository at this point in the history
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 | ));
```
  • Loading branch information
ajcvickers committed Nov 23, 2019
1 parent 7a71eb0 commit c5a8c1c
Show file tree
Hide file tree
Showing 69 changed files with 2,586 additions and 1,741 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,8 @@ public static void ExecutingSqlQuery(
CoreEventId.ProviderBaseId,
"Executing Sql Query [Parameters=[{parameters}]]{newLine}{commandText}"));

var warningBehavior = definition.GetLogBehavior(diagnosticsLogger);

definition.Log(
diagnosticsLogger,
warningBehavior,
FormatParameters(cosmosSqlQuery.Parameters),
Environment.NewLine,
cosmosSqlQuery.Query);
Expand Down
2 changes: 1 addition & 1 deletion src/EFCore.Cosmos/Query/Internal/SqlParameterExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Microsoft.EntityFrameworkCore.Cosmos.Query.Internal
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public class SqlParameterExpression : SqlExpression
public sealed class SqlParameterExpression : SqlExpression
{
private readonly ParameterExpression _parameterExpression;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public static IServiceCollection AddEntityFrameworkDesignTimeServices(
.AddSingleton<IScaffoldingModelFactory, RelationalScaffoldingModelFactory>()
.AddSingleton<IScaffoldingTypeMapper, ScaffoldingTypeMapper>()
.AddSingleton<IValueConverterSelector, ValueConverterSelector>()
.AddSingleton<ISimpleLogger, NullSimpleLogger>()
.AddTransient<MigrationsScaffolderDependencies>()
.AddTransient<IMigrationsScaffolder, MigrationsScaffolder>()
.AddTransient<ISnapshotModelProcessor, SnapshotModelProcessor>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,18 @@ public static void TransactionIgnoredWarning(
{
var definition = InMemoryResources.LogTransactionsNotSupported(diagnostics);

var warningBehavior = definition.GetLogBehavior(diagnostics);
if (warningBehavior != WarningBehavior.Ignore)
if (diagnostics.ShouldLog(definition))
{
definition.Log(diagnostics, warningBehavior);
definition.Log(diagnostics);
}

if (diagnostics.DiagnosticSource.IsEnabled(definition.EventId.Name))
if (diagnostics.NeedsEventData(definition, out var diagnosticSourceEnabled, out var simpleLogEnabled))
{
diagnostics.DiagnosticSource.Write(
definition.EventId.Name,
new EventData(
definition,
(d, _) => ((EventDefinition)d).GenerateMessage()));
var eventData = new EventData(
definition,
(d, _) => ((EventDefinition)d).GenerateMessage());

diagnostics.DispatchEventData(definition, eventData, diagnosticSourceEnabled, simpleLogEnabled);
}
}

Expand All @@ -57,24 +56,20 @@ public static void ChangesSaved(
{
var definition = InMemoryResources.LogSavedChanges(diagnostics);

var warningBehavior = definition.GetLogBehavior(diagnostics);
if (warningBehavior != WarningBehavior.Ignore)
if (diagnostics.ShouldLog(definition))
{
definition.Log(
diagnostics,
warningBehavior,
rowsAffected);
definition.Log(diagnostics, rowsAffected);
}

if (diagnostics.DiagnosticSource.IsEnabled(definition.EventId.Name))
if (diagnostics.NeedsEventData(definition, out var diagnosticSourceEnabled, out var simpleLogEnabled))
{
diagnostics.DiagnosticSource.Write(
definition.EventId.Name,
new SaveChangesEventData(
definition,
ChangesSaved,
entries,
rowsAffected));
var eventData = new SaveChangesEventData(
definition,
ChangesSaved,
entries,
rowsAffected);

diagnostics.DispatchEventData(definition, eventData, diagnosticSourceEnabled, simpleLogEnabled);
}
}

Expand Down
Loading

0 comments on commit c5a8c1c

Please sign in to comment.