Skip to content

Commit 4d96eb0

Browse files
committed
Update EventIds to be unique and be consistent with ILogger/DiagnosticSource
Issues dotnet#218 dotnet#6802 (Doesn't cover dotnet#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 dotnet#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.
1 parent 8e0c0b2 commit 4d96eb0

156 files changed

Lines changed: 6027 additions & 2455 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/EFCore.Design/Design/Internal/DesignTimeServicesBuilder.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Diagnostics;
56
using System.IO;
67
using System.Reflection;
78
using JetBrains.Annotations;
@@ -93,6 +94,8 @@ protected virtual IServiceCollection ConfigureContextServices(
9394
.AddTransient<MigrationsScaffolder>()
9495
.AddTransient(_ => contextServices.GetService<ILoggingOptions>())
9596
.AddTransient(_ => contextServices.GetService(typeof(IInterceptingLogger<>)))
97+
.AddTransient(_ => contextServices.GetService(typeof(IDiagnosticsLogger<>)))
98+
.AddTransient(_ => contextServices.GetService<DiagnosticSource>())
9699
.AddTransient(_ => contextServices.GetService<ICurrentDbContext>())
97100
.AddTransient(_ => contextServices.GetService<IDatabaseProvider>())
98101
.AddTransient(_ => contextServices.GetService<IDbContextOptions>())

src/EFCore.Design/Design/Internal/OperationLogger.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.Text;
66
using JetBrains.Annotations;
77
using Microsoft.EntityFrameworkCore.Infrastructure;
8-
using Microsoft.EntityFrameworkCore.Storage.Internal;
98
using Microsoft.Extensions.Logging;
109

1110
namespace Microsoft.EntityFrameworkCore.Design.Internal
@@ -55,8 +54,8 @@ public virtual void Log<TState>(
5554
Func<TState, Exception, string> formatter)
5655
{
5756
// Only show SQL when verbose
58-
if (_categoryName == typeof(RelationalCommandBuilderFactory).FullName
59-
&& eventId.Id == (int)RelationalEventId.ExecutedCommand)
57+
if (_categoryName == LoggerCategory.Database.Sql.Name
58+
&& eventId.Id == RelationalEventId.CommandExecuted.Id)
6059
{
6160
logLevel = LogLevel.Debug;
6261
}
Lines changed: 129 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,137 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System.Diagnostics;
5+
using Microsoft.Extensions.Logging;
6+
47
namespace Microsoft.EntityFrameworkCore.Infrastructure
58
{
6-
public enum DesignEventId
9+
/// <summary>
10+
/// <para>
11+
/// Event IDs for design events that correspond to messages logged to an <see cref="ILogger" />
12+
/// and events sent to a <see cref="DiagnosticSource" />.
13+
/// </para>
14+
/// <para>
15+
/// These IDs are also used with <see cref="WarningsConfigurationBuilder" /> to configure the
16+
/// behavior of warnings.
17+
/// </para>
18+
/// </summary>
19+
public static class DesignEventId
720
{
8-
ForceRemoveMigration = 1,
9-
RemovingMigration,
10-
NoMigrationFile,
11-
NoMigrationMetadataFile,
12-
ManuallyDeleted,
13-
RemovingSnapshot,
14-
NoSnapshotFile,
15-
WritingSnapshot,
16-
ReusingNamespace,
17-
ReusingDirectory,
18-
RevertingSnapshot,
19-
WritingMigration,
20-
ReusingSnapshotName,
21-
DestructiveOperation,
22-
ForeignMigrations
21+
// Warning: These values must not change between releases.
22+
// Only add new values to the end of sections, never in the middle.
23+
// Try to use <Noun><Verb> naming and be consistent with existing names.
24+
private enum Id
25+
{
26+
// Migrations events
27+
MigrationForceRemove = CoreEventId.CoreDesignBaseId,
28+
MigrationRemoving,
29+
MigrationFileNotFound,
30+
MigrationMetadataFileNotFound,
31+
MigrationManuallyDeleted,
32+
SnapshotRemoving,
33+
SnapshotFileNotFound,
34+
SnapshotWriting,
35+
NamespaceReusing,
36+
DirectoryReusing,
37+
SnapshotReverting,
38+
MigrationWriting,
39+
SnapshotNameReusing,
40+
DestructiveOperation,
41+
ForeignMigrations
42+
}
43+
44+
private static readonly string _migrationsPrefix = LoggerCategory.Migrations.Name + ".";
45+
private static EventId MakeMigrationsId(Id id) => new EventId((int)id, _migrationsPrefix + id);
46+
47+
/// <summary>
48+
/// Removing a migration without checking the database.
49+
/// This event is in the <see cref="LoggerCategory.Migrations" /> category.
50+
/// </summary>
51+
public static readonly EventId MigrationForceRemove = MakeMigrationsId(Id.MigrationForceRemove);
52+
53+
/// <summary>
54+
/// Removing migration.
55+
/// This event is in the <see cref="LoggerCategory.Migrations" /> category.
56+
/// </summary>
57+
public static readonly EventId MigrationRemoving = MakeMigrationsId(Id.MigrationRemoving);
58+
59+
/// <summary>
60+
/// A migration file was not found.
61+
/// This event is in the <see cref="LoggerCategory.Migrations" /> category.
62+
/// </summary>
63+
public static readonly EventId MigrationFileNotFound = MakeMigrationsId(Id.MigrationFileNotFound);
64+
65+
/// <summary>
66+
/// A metadata file was not found.
67+
/// This event is in the <see cref="LoggerCategory.Migrations" /> category.
68+
/// </summary>
69+
public static readonly EventId MigrationMetadataFileNotFound = MakeMigrationsId(Id.MigrationMetadataFileNotFound);
70+
71+
/// <summary>
72+
/// A manual migration deletion was detected.
73+
/// This event is in the <see cref="LoggerCategory.Migrations" /> category.
74+
/// </summary>
75+
public static readonly EventId MigrationManuallyDeleted = MakeMigrationsId(Id.MigrationManuallyDeleted);
76+
77+
/// <summary>
78+
/// Removing model snapshot.
79+
/// This event is in the <see cref="LoggerCategory.Migrations" /> category.
80+
/// </summary>
81+
public static readonly EventId SnapshotRemoving = MakeMigrationsId(Id.SnapshotRemoving);
82+
83+
/// <summary>
84+
/// No model snapshot file named was found.
85+
/// This event is in the <see cref="LoggerCategory.Migrations" /> category.
86+
/// </summary>
87+
public static readonly EventId SnapshotFileNotFound = MakeMigrationsId(Id.SnapshotFileNotFound);
88+
89+
/// <summary>
90+
/// Writing model snapshot to file.
91+
/// This event is in the <see cref="LoggerCategory.Migrations" /> category.
92+
/// </summary>
93+
public static readonly EventId SnapshotWriting = MakeMigrationsId(Id.SnapshotWriting);
94+
95+
/// <summary>
96+
/// Reusing namespace of a type.
97+
/// This event is in the <see cref="LoggerCategory.Migrations" /> category.
98+
/// </summary>
99+
public static readonly EventId NamespaceReusing = MakeMigrationsId(Id.NamespaceReusing);
100+
101+
/// <summary>
102+
/// Reusing directory for a file.
103+
/// This event is in the <see cref="LoggerCategory.Migrations" /> category.
104+
/// </summary>
105+
public static readonly EventId DirectoryReusing = MakeMigrationsId(Id.DirectoryReusing);
106+
107+
/// <summary>
108+
/// Reverting model snapshot.
109+
/// This event is in the <see cref="LoggerCategory.Migrations" /> category.
110+
/// </summary>
111+
public static readonly EventId SnapshotReverting = MakeMigrationsId(Id.SnapshotReverting);
112+
113+
/// <summary>
114+
/// Writing migration to file.
115+
/// This event is in the <see cref="LoggerCategory.Migrations" /> category.
116+
/// </summary>
117+
public static readonly EventId MigrationWriting = MakeMigrationsId(Id.MigrationWriting);
118+
119+
/// <summary>
120+
/// Resuing model snapshot name.
121+
/// This event is in the <see cref="LoggerCategory.Migrations" /> category.
122+
/// </summary>
123+
public static readonly EventId SnapshotNameReusing = MakeMigrationsId(Id.SnapshotNameReusing);
124+
125+
/// <summary>
126+
/// An operation was scaffolded that may result in the loss of data. Please review the migration for accuracy.
127+
/// This event is in the <see cref="LoggerCategory.Migrations" /> category.
128+
/// </summary>
129+
public static readonly EventId DestructiveOperation = MakeMigrationsId(Id.DestructiveOperation);
130+
131+
/// <summary>
132+
/// The namespace contains migrations for a different context.
133+
/// This event is in the <see cref="LoggerCategory.Migrations" /> category.
134+
/// </summary>
135+
public static readonly EventId ForeignMigrations = MakeMigrationsId(Id.ForeignMigrations);
23136
}
24137
}

0 commit comments

Comments
 (0)