Skip to content

Commit

Permalink
Update EventIds to be unique and be consistent with ILogger/Diagnosti…
Browse files Browse the repository at this point in the history
…cSource

Issues #218 #6802

(Doesn't cover #6946 - Template-based format strings.)

This change introduces unique event IDs and a pattern such that every time an event is logged it is sent to both ILogger and DiagnosticSource. Specifically:
* EventIds are globally unique across core/relational/any given provider. (As discussed in #218)
* The same EventIds are used for ILogger and DiagnosticSource (ILogger uses the int/name tuple, DiagnosticSource uses just the name)
* EventIds align with logger categories
* Automated tests for the EventId/LoggerExtensions pattern--every EventId has a LoggerExtension method that works as expected.
* Warnings configuration is updated to use the new event ids
* Updated IsEnabled to handle warnings as errors and ignored warnings more transparently.
  • Loading branch information
ajcvickers committed Apr 27, 2017
1 parent 8e0c0b2 commit 4d96eb0
Show file tree
Hide file tree
Showing 156 changed files with 6,027 additions and 2,455 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using JetBrains.Annotations;
Expand Down Expand Up @@ -93,6 +94,8 @@ protected virtual IServiceCollection ConfigureContextServices(
.AddTransient<MigrationsScaffolder>()
.AddTransient(_ => contextServices.GetService<ILoggingOptions>())
.AddTransient(_ => contextServices.GetService(typeof(IInterceptingLogger<>)))
.AddTransient(_ => contextServices.GetService(typeof(IDiagnosticsLogger<>)))
.AddTransient(_ => contextServices.GetService<DiagnosticSource>())
.AddTransient(_ => contextServices.GetService<ICurrentDbContext>())
.AddTransient(_ => contextServices.GetService<IDatabaseProvider>())
.AddTransient(_ => contextServices.GetService<IDbContextOptions>())
Expand Down
5 changes: 2 additions & 3 deletions src/EFCore.Design/Design/Internal/OperationLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Text;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.Internal;
using Microsoft.Extensions.Logging;

namespace Microsoft.EntityFrameworkCore.Design.Internal
Expand Down Expand Up @@ -55,8 +54,8 @@ public virtual void Log<TState>(
Func<TState, Exception, string> formatter)
{
// Only show SQL when verbose
if (_categoryName == typeof(RelationalCommandBuilderFactory).FullName
&& eventId.Id == (int)RelationalEventId.ExecutedCommand)
if (_categoryName == LoggerCategory.Database.Sql.Name
&& eventId.Id == RelationalEventId.CommandExecuted.Id)
{
logLevel = LogLevel.Debug;
}
Expand Down
145 changes: 129 additions & 16 deletions src/EFCore.Design/Infrastructure/DesignEventId.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,137 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Diagnostics;
using Microsoft.Extensions.Logging;

namespace Microsoft.EntityFrameworkCore.Infrastructure
{
public enum DesignEventId
/// <summary>
/// <para>
/// Event IDs for design events that correspond to messages logged to an <see cref="ILogger" />
/// and events sent to a <see cref="DiagnosticSource" />.
/// </para>
/// <para>
/// These IDs are also used with <see cref="WarningsConfigurationBuilder" /> to configure the
/// behavior of warnings.
/// </para>
/// </summary>
public static class DesignEventId
{
ForceRemoveMigration = 1,
RemovingMigration,
NoMigrationFile,
NoMigrationMetadataFile,
ManuallyDeleted,
RemovingSnapshot,
NoSnapshotFile,
WritingSnapshot,
ReusingNamespace,
ReusingDirectory,
RevertingSnapshot,
WritingMigration,
ReusingSnapshotName,
DestructiveOperation,
ForeignMigrations
// Warning: These values must not change between releases.
// Only add new values to the end of sections, never in the middle.
// Try to use <Noun><Verb> naming and be consistent with existing names.
private enum Id
{
// Migrations events
MigrationForceRemove = CoreEventId.CoreDesignBaseId,
MigrationRemoving,
MigrationFileNotFound,
MigrationMetadataFileNotFound,
MigrationManuallyDeleted,
SnapshotRemoving,
SnapshotFileNotFound,
SnapshotWriting,
NamespaceReusing,
DirectoryReusing,
SnapshotReverting,
MigrationWriting,
SnapshotNameReusing,
DestructiveOperation,
ForeignMigrations
}

private static readonly string _migrationsPrefix = LoggerCategory.Migrations.Name + ".";
private static EventId MakeMigrationsId(Id id) => new EventId((int)id, _migrationsPrefix + id);

/// <summary>
/// Removing a migration without checking the database.
/// This event is in the <see cref="LoggerCategory.Migrations" /> category.
/// </summary>
public static readonly EventId MigrationForceRemove = MakeMigrationsId(Id.MigrationForceRemove);

/// <summary>
/// Removing migration.
/// This event is in the <see cref="LoggerCategory.Migrations" /> category.
/// </summary>
public static readonly EventId MigrationRemoving = MakeMigrationsId(Id.MigrationRemoving);

/// <summary>
/// A migration file was not found.
/// This event is in the <see cref="LoggerCategory.Migrations" /> category.
/// </summary>
public static readonly EventId MigrationFileNotFound = MakeMigrationsId(Id.MigrationFileNotFound);

/// <summary>
/// A metadata file was not found.
/// This event is in the <see cref="LoggerCategory.Migrations" /> category.
/// </summary>
public static readonly EventId MigrationMetadataFileNotFound = MakeMigrationsId(Id.MigrationMetadataFileNotFound);

/// <summary>
/// A manual migration deletion was detected.
/// This event is in the <see cref="LoggerCategory.Migrations" /> category.
/// </summary>
public static readonly EventId MigrationManuallyDeleted = MakeMigrationsId(Id.MigrationManuallyDeleted);

/// <summary>
/// Removing model snapshot.
/// This event is in the <see cref="LoggerCategory.Migrations" /> category.
/// </summary>
public static readonly EventId SnapshotRemoving = MakeMigrationsId(Id.SnapshotRemoving);

/// <summary>
/// No model snapshot file named was found.
/// This event is in the <see cref="LoggerCategory.Migrations" /> category.
/// </summary>
public static readonly EventId SnapshotFileNotFound = MakeMigrationsId(Id.SnapshotFileNotFound);

/// <summary>
/// Writing model snapshot to file.
/// This event is in the <see cref="LoggerCategory.Migrations" /> category.
/// </summary>
public static readonly EventId SnapshotWriting = MakeMigrationsId(Id.SnapshotWriting);

/// <summary>
/// Reusing namespace of a type.
/// This event is in the <see cref="LoggerCategory.Migrations" /> category.
/// </summary>
public static readonly EventId NamespaceReusing = MakeMigrationsId(Id.NamespaceReusing);

/// <summary>
/// Reusing directory for a file.
/// This event is in the <see cref="LoggerCategory.Migrations" /> category.
/// </summary>
public static readonly EventId DirectoryReusing = MakeMigrationsId(Id.DirectoryReusing);

/// <summary>
/// Reverting model snapshot.
/// This event is in the <see cref="LoggerCategory.Migrations" /> category.
/// </summary>
public static readonly EventId SnapshotReverting = MakeMigrationsId(Id.SnapshotReverting);

/// <summary>
/// Writing migration to file.
/// This event is in the <see cref="LoggerCategory.Migrations" /> category.
/// </summary>
public static readonly EventId MigrationWriting = MakeMigrationsId(Id.MigrationWriting);

/// <summary>
/// Resuing model snapshot name.
/// This event is in the <see cref="LoggerCategory.Migrations" /> category.
/// </summary>
public static readonly EventId SnapshotNameReusing = MakeMigrationsId(Id.SnapshotNameReusing);

/// <summary>
/// An operation was scaffolded that may result in the loss of data. Please review the migration for accuracy.
/// This event is in the <see cref="LoggerCategory.Migrations" /> category.
/// </summary>
public static readonly EventId DestructiveOperation = MakeMigrationsId(Id.DestructiveOperation);

/// <summary>
/// The namespace contains migrations for a different context.
/// This event is in the <see cref="LoggerCategory.Migrations" /> category.
/// </summary>
public static readonly EventId ForeignMigrations = MakeMigrationsId(Id.ForeignMigrations);
}
}
Loading

0 comments on commit 4d96eb0

Please sign in to comment.