Skip to content

Commit

Permalink
Simple logging changes
Browse files Browse the repository at this point in the history
See dotnet/efcore#16200
Follows 690ebc4540334c1d0938660a5960a25d4338acd4 in EF Core
  • Loading branch information
roji committed Jan 28, 2020
1 parent 2438841 commit 5b22ea1
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 83 deletions.
100 changes: 40 additions & 60 deletions src/EFCore.PG/Internal/NpgsqlLoggerExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.Extensions.Logging;

namespace Npgsql.EntityFrameworkCore.PostgreSQL.Internal
{
Expand All @@ -12,14 +13,11 @@ public static void MissingSchemaWarning(
{
var definition = NpgsqlResources.LogMissingSchema(diagnostics);

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

// No DiagnosticsSource events because these are purely design-time messages
}

Expand All @@ -29,14 +27,11 @@ public static void MissingTableWarning(
{
var definition = NpgsqlResources.LogMissingTable(diagnostics);

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

// No DiagnosticsSource events because these are purely design-time messages
}

Expand All @@ -48,40 +43,43 @@ public static void ForeignKeyReferencesMissingPrincipalTableWarning(
{
var definition = NpgsqlResources.LogPrincipalTableNotInSelectionSet(diagnostics);

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

// No DiagnosticsSource events because these are purely design-time messages
}

/// <summary>
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
public static void ColumnFound(
[NotNull] this IDiagnosticsLogger<DbLoggerCategory.Scaffolding> diagnostics,
[NotNull] string tableName,
[NotNull] string columnName,
[NotNull] string dataTypeName,
bool nullable,
[CanBeNull] string defaultValue)
bool identity,
[CanBeNull] string defaultValue,
[CanBeNull] string computedValue)
{
var definition = NpgsqlResources.LogFoundColumn(diagnostics);

var warningBehavior = definition.GetLogBehavior(diagnostics);
if (warningBehavior != WarningBehavior.Ignore)
if (diagnostics.ShouldLog(definition))
{
definition.Log(
diagnostics,
warningBehavior,
tableName, columnName, dataTypeName, nullable, defaultValue);
l => l.LogDebug(
definition.EventId,
null,
definition.MessageFormat,
tableName,
columnName,
dataTypeName,
nullable,
identity,
defaultValue,
computedValue));
}

// No DiagnosticsSource events because these are purely design-time messages
}

Expand All @@ -92,14 +90,11 @@ public static void UniqueConstraintFound(
{
var definition = NpgsqlResources.LogFoundUniqueConstraint(diagnostics);

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

// No DiagnosticsSource events because these are purely design-time messages
}

Expand All @@ -109,14 +104,11 @@ public static void EnumColumnSkippedWarning(
{
var definition = NpgsqlResources.LogEnumColumnSkipped(diagnostics);

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

// No DiagnosticsSource events because these are purely design-time messages
}

Expand All @@ -127,15 +119,11 @@ public static void ExpressionIndexSkippedWarning(
{
var definition = NpgsqlResources.LogExpressionIndexSkipped(diagnostics);

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

// No DiagnosticsSource events because these are purely design-time messages
}

Expand All @@ -146,15 +134,11 @@ public static void UnsupportedColumnIndexSkippedWarning(
{
var definition = NpgsqlResources.LogUnsupportedColumnIndexSkipped(diagnostics);

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

// No DiagnosticsSource events because these are purely design-time messages
}

Expand All @@ -165,15 +149,11 @@ public static void UnsupportedColumnConstraintSkippedWarning(
{
var definition = NpgsqlResources.LogUnsupportedColumnConstraintSkipped(diagnostics);

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

// No DiagnosticsSource events because these are purely design-time messages
}
}
Expand Down
13 changes: 5 additions & 8 deletions src/EFCore.PG/Properties/NpgsqlStrings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/EFCore.PG/Properties/NpgsqlStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@
<value>The property '{property}' on entity type '{entityType}' is configured to use 'SequenceHiLo' value generator, which is only intended for keys. If this was intentional configure an alternate key on the property, otherwise call 'ValueGeneratedNever' or configure store generation for this property.</value>
</data>
<data name="LogFoundColumn" xml:space="preserve">
<value>Found column with table: {tableName}, column name: {columnName}, data type: {dataType}, nullable: {isNullable}, default value: {defaultValue}</value>
<comment>Debug NpgsqlEventId.ColumnFound string string string bool string</comment>
<value>Found column with table: {tableName}, column name: {columnName}, data type: {dataType}, nullable: {isNullable}, identity: {isIdentity}, default value: {defaultValue}, computed value: {computedValue}</value>
<comment>Debug NpgsqlEventId.ColumnFound string string string bool bool string string</comment>
</data>
<data name="LogFoundForeignKey" xml:space="preserve">
<value>Found foreign key on table: {tableName}, name: {foreignKeyName}, principal table: {principalTableName}, delete action: {deleteAction}.</value>
Expand Down
16 changes: 9 additions & 7 deletions src/EFCore.PG/Scaffolding/Internal/NpgsqlDatabaseModelFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -357,13 +357,6 @@ nspname NOT IN ('pg_catalog', 'information_schema') AND
continue;
}

logger.ColumnFound(
DisplayName(tableSchema, tableName),
column.Name,
formattedTypeName,
column.IsNullable,
column.DefaultValueSql);

// Default values and PostgreSQL 12 generated columns
if (record.GetValueOrDefault<char>("attgenerated") == 's')
column.ComputedColumnSql = record.GetValueOrDefault<string>("default");
Expand Down Expand Up @@ -437,6 +430,15 @@ nspname NOT IN ('pg_catalog', 'information_schema') AND
if (record.GetValueOrDefault<string>("description") is string comment)
column.Comment = comment;

logger.ColumnFound(
DisplayName(tableSchema, tableName),
column.Name,
formattedTypeName,
column.IsNullable,
isIdentity,
column.DefaultValueSql,
column.ComputedColumnSql);

table.Columns.Add(column);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Diagnostics;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics.Internal;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
Expand Down Expand Up @@ -1901,7 +1902,8 @@ void Test(string createSql, IEnumerable<string> tables, IEnumerable<string> sche
Fixture.ListLoggerFactory,
new LoggingOptions(),
new DiagnosticListener("Fake"),
new NpgsqlLoggingDefinitions()));
new NpgsqlLoggingDefinitions(),
new NullDbContextLogger()));

var databaseModel = databaseModelFactory.Create(
Fixture.TestStore.ConnectionString,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using System.Text;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics.Internal;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore.Scaffolding;
Expand All @@ -31,7 +32,8 @@ protected override IDatabaseModelFactory CreateDatabaseModelFactory(ILoggerFacto
loggerFactory,
new LoggingOptions(),
new DiagnosticListener("Fake"),
new NpgsqlLoggingDefinitions()));
new NpgsqlLoggingDefinitions(),
new NullDbContextLogger()));

protected override bool AcceptIndex(DatabaseIndex index)
=> false;
Expand Down
7 changes: 5 additions & 2 deletions test/EFCore.PG.Tests/NpgsqlRelationalConnectionTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Diagnostics;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics.Internal;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.Storage.Internal;
Expand Down Expand Up @@ -56,12 +57,14 @@ public static RelationalConnectionDependencies CreateDependencies(DbContextOptio
new LoggerFactory(),
new LoggingOptions(),
new DiagnosticListener("FakeDiagnosticListener"),
new NpgsqlLoggingDefinitions()),
new NpgsqlLoggingDefinitions(),
new NullDbContextLogger()),
new DiagnosticsLogger<DbLoggerCategory.Database.Connection>(
new LoggerFactory(),
new LoggingOptions(),
new DiagnosticListener("FakeDiagnosticListener"),
new NpgsqlLoggingDefinitions()),
new NpgsqlLoggingDefinitions(),
new NullDbContextLogger()),
new NamedConnectionStringResolver(options),
new RelationalTransactionFactory(new RelationalTransactionFactoryDependencies()),
new CurrentDbContext(new FakeDbContext()));
Expand Down
3 changes: 3 additions & 0 deletions test/EFCore.PG.Tests/TestUtilities/FakeDiagnosticsLogger.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Diagnostics;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Diagnostics.Internal;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore.TestUtilities;
using Microsoft.Extensions.Logging;
Expand All @@ -18,6 +19,8 @@ public class FakeDiagnosticsLogger<T> : IDiagnosticsLogger<T>, ILogger

public DiagnosticSource DiagnosticSource { get; } = new DiagnosticListener("Fake");

public IDbContextLogger DbContextLogger { get; } = new NullDbContextLogger();

public void Log<TState>(
LogLevel logLevel,
EventId eventId,
Expand Down
4 changes: 2 additions & 2 deletions tools/Resources.tt
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ namespace <#= model.Namespace #>
<#= model.AccessModifier #> static class <#= model.Class #>
{
private static readonly ResourceManager _resourceManager
= new ResourceManager("<#= model.ResourceName #>", typeof(<#= model.Class #>).GetTypeInfo().Assembly);
= new ResourceManager("<#= model.ResourceName #>", typeof(<#= model.Class #>).Assembly);
<#
foreach (var resource in model.Resources)
{
Expand Down Expand Up @@ -136,7 +136,7 @@ namespace <#= model.Namespace.EndsWith(".Internal") ? model.Namespace : (model.N
<#= model.AccessModifier #> static class <#= model.DiagnosticsClass #>
{
private static readonly ResourceManager _resourceManager
= new ResourceManager("<#= model.ResourceName #>", typeof(<#= model.DiagnosticsClass #>).GetTypeInfo().Assembly);
= new ResourceManager("<#= model.ResourceName #>", typeof(<#= model.DiagnosticsClass #>).Assembly);
<#
foreach (var resource in model.Resources)
{
Expand Down

0 comments on commit 5b22ea1

Please sign in to comment.