Skip to content

Commit

Permalink
Add CommandSource to CommandEventData (#24421)
Browse files Browse the repository at this point in the history
* Add CommandSource to CommandEventData

* Create constructor overload for CommandEventData and its subclasses

* Move CommandSource to RelationalCommandParameterObject

* Make CommandSource virtual

* Updated interceptor tests to assert CommandSource property

* Add CommandSource to CommandCreating event

* Comment CommandSource assertion

* Comment all CommandSource assertions

* Obsolete constructor overloads that do not have CommandSource parameter

* Change command source values
  • Loading branch information
Giorgi authored Jul 28, 2021
1 parent 63a8966 commit 85aaf40
Show file tree
Hide file tree
Showing 28 changed files with 581 additions and 82 deletions.
46 changes: 46 additions & 0 deletions src/EFCore.Relational/Diagnostics/CommandCorrelatedEventData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class CommandCorrelatedEventData : DbContextEventData
/// <param name="connectionId"> A correlation ID that identifies the <see cref="DbConnection" /> instance being used. </param>
/// <param name="async"> Indicates whether or not the command was executed asynchronously. </param>
/// <param name="startTime"> The start time of this event. </param>
[Obsolete("Use the overload with CommandSource")]
public CommandCorrelatedEventData(
EventDefinitionBase eventDefinition,
Func<EventDefinitionBase, EventData, string> messageGenerator,
Expand All @@ -34,6 +35,45 @@ public CommandCorrelatedEventData(
Guid connectionId,
bool async,
DateTimeOffset startTime)
: this(
eventDefinition,
messageGenerator,
connection,
context,
executeMethod,
commandId,
connectionId,
async,
startTime,
CommandSource.Unknown)
{

}

/// <summary>
/// Constructs the event payload.
/// </summary>
/// <param name="eventDefinition"> The event definition. </param>
/// <param name="messageGenerator"> A delegate that generates a log message for this event. </param>
/// <param name="connection"> The <see cref="DbConnection" /> being used. </param>
/// <param name="context"> The <see cref="DbContext" /> currently being used, to null if not known. </param>
/// <param name="executeMethod"> The <see cref="DbCommand" /> method. </param>
/// <param name="commandId"> A correlation ID that identifies the <see cref="DbCommand" /> instance being used. </param>
/// <param name="connectionId"> A correlation ID that identifies the <see cref="DbConnection" /> instance being used. </param>
/// <param name="async"> Indicates whether or not the command was executed asynchronously. </param>
/// <param name="startTime"> The start time of this event. </param>
/// <param name="commandSource">Source of the command.</param>
public CommandCorrelatedEventData(
EventDefinitionBase eventDefinition,
Func<EventDefinitionBase, EventData, string> messageGenerator,
DbConnection connection,
DbContext? context,
DbCommandMethod executeMethod,
Guid commandId,
Guid connectionId,
bool async,
DateTimeOffset startTime,
CommandSource commandSource)
: base(eventDefinition, messageGenerator, context)
{
Connection = connection;
Expand All @@ -42,6 +82,7 @@ public CommandCorrelatedEventData(
ExecuteMethod = executeMethod;
IsAsync = async;
StartTime = startTime;
CommandSource = commandSource;
}

/// <summary>
Expand Down Expand Up @@ -73,5 +114,10 @@ public CommandCorrelatedEventData(
/// The start time of this event.
/// </summary>
public virtual DateTimeOffset StartTime { get; }

/// <summary>
/// Source of the command.
/// </summary>
public virtual CommandSource CommandSource { get; }
}
}
46 changes: 46 additions & 0 deletions src/EFCore.Relational/Diagnostics/CommandEndEventData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class CommandEndEventData : CommandEventData
/// <param name="logParameterValues"> Indicates whether or not the application allows logging of parameter values. </param>
/// <param name="startTime"> The start time of this event. </param>
/// <param name="duration"> The duration this event. </param>
[Obsolete("Use the overload with CommandSource")]
public CommandEndEventData(
EventDefinitionBase eventDefinition,
Func<EventDefinitionBase, EventData, string> messageGenerator,
Expand Down Expand Up @@ -55,6 +56,51 @@ public CommandEndEventData(
startTime)
=> Duration = duration;

/// <summary>
/// Constructs the event payload.
/// </summary>
/// <param name="eventDefinition"> The event definition. </param>
/// <param name="messageGenerator"> A delegate that generates a log message for this event. </param>
/// <param name="connection"> The <see cref="DbConnection" /> being used. </param>
/// <param name="command"> The <see cref="DbCommand" />. </param>
/// <param name="context"> The <see cref="DbContext" /> currently being used, to null if not known. </param>
/// <param name="executeMethod"> The <see cref="DbCommand" /> method. </param>
/// <param name="commandId"> A correlation ID that identifies the <see cref="DbCommand" /> instance being used. </param>
/// <param name="connectionId"> A correlation ID that identifies the <see cref="DbConnection" /> instance being used. </param>
/// <param name="async"> Indicates whether or not the command was executed asynchronously. </param>
/// <param name="logParameterValues"> Indicates whether or not the application allows logging of parameter values. </param>
/// <param name="startTime"> The start time of this event. </param>
/// <param name="commandSource">Source of the command.</param>
/// <param name="duration"> The duration this event. </param>
public CommandEndEventData(
EventDefinitionBase eventDefinition,
Func<EventDefinitionBase, EventData, string> messageGenerator,
DbConnection connection,
DbCommand command,
DbContext? context,
DbCommandMethod executeMethod,
Guid commandId,
Guid connectionId,
bool async,
bool logParameterValues,
DateTimeOffset startTime,
CommandSource commandSource,
TimeSpan duration)
: base(
eventDefinition,
messageGenerator,
connection,
command,
context,
executeMethod,
commandId,
connectionId,
async,
logParameterValues,
startTime,
commandSource)
=> Duration = duration;

/// <summary>
/// The duration this event.
/// </summary>
Expand Down
55 changes: 54 additions & 1 deletion src/EFCore.Relational/Diagnostics/CommandErrorEventData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class CommandErrorEventData : CommandEndEventData, IErrorEventData
/// <param name="logParameterValues"> Indicates whether or not the application allows logging of parameter values. </param>
/// <param name="startTime"> The start time of this event. </param>
/// <param name="duration"> The duration this event. </param>
[Obsolete("Use the overload with CommandSource")]
public CommandErrorEventData(
EventDefinitionBase eventDefinition,
Func<EventDefinitionBase, EventData, string> messageGenerator,
Expand Down Expand Up @@ -55,7 +56,59 @@ public CommandErrorEventData(
logParameterValues,
startTime,
duration)
=> Exception = exception;
{
Exception = exception;
}

/// <summary>
/// Constructs the event payload.
/// </summary>
/// <param name="eventDefinition"> The event definition. </param>
/// <param name="messageGenerator"> A delegate that generates a log message for this event. </param>
/// <param name="connection"> The <see cref="DbConnection" /> being used. </param>
/// <param name="command"> The <see cref="DbCommand" /> that was executing when it failed. </param>
/// <param name="context"> The <see cref="DbContext" /> currently being used, to null if not known. </param>
/// <param name="executeMethod"> The <see cref="DbCommand" /> method that was used to execute the command. </param>
/// <param name="commandId"> A correlation ID that identifies the <see cref="DbCommand" /> instance being used. </param>
/// <param name="connectionId"> A correlation ID that identifies the <see cref="DbConnection" /> instance being used. </param>
/// <param name="exception"> The exception that was thrown when execution failed. </param>
/// <param name="async"> Indicates whether or not the command was executed asynchronously. </param>
/// <param name="logParameterValues"> Indicates whether or not the application allows logging of parameter values. </param>
/// <param name="startTime"> The start time of this event. </param>
/// <param name="commandSource">Source of the command.</param>
/// <param name="duration"> The duration this event. </param>
public CommandErrorEventData(
EventDefinitionBase eventDefinition,
Func<EventDefinitionBase, EventData, string> messageGenerator,
DbConnection connection,
DbCommand command,
DbContext? context,
DbCommandMethod executeMethod,
Guid commandId,
Guid connectionId,
Exception exception,
bool async,
bool logParameterValues,
DateTimeOffset startTime,
CommandSource commandSource,
TimeSpan duration)
: base(
eventDefinition,
messageGenerator,
connection,
command,
context,
executeMethod,
commandId,
connectionId,
async,
logParameterValues,
startTime,
commandSource,
duration)
{
Exception = exception;
}

/// <summary>
/// The exception that was thrown when execution failed.
Expand Down
48 changes: 47 additions & 1 deletion src/EFCore.Relational/Diagnostics/CommandEventData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class CommandEventData : CommandCorrelatedEventData
/// <param name="async"> Indicates whether or not the command was executed asynchronously. </param>
/// <param name="logParameterValues"> Indicates whether or not the application allows logging of parameter values. </param>
/// <param name="startTime"> The start time of this event. </param>
[Obsolete("Use the overload with CommandSource")]
public CommandEventData(
EventDefinitionBase eventDefinition,
Func<EventDefinitionBase, EventData, string> messageGenerator,
Expand All @@ -39,6 +40,50 @@ public CommandEventData(
bool async,
bool logParameterValues,
DateTimeOffset startTime)
: this(
eventDefinition,
messageGenerator,
connection,
command,
context,
executeMethod,
commandId,
connectionId,
async,
logParameterValues,
startTime,
CommandSource.Unknown)
{
}

/// <summary>
/// Constructs the event payload.
/// </summary>
/// <param name="eventDefinition"> The event definition. </param>
/// <param name="messageGenerator"> A delegate that generates a log message for this event. </param>
/// <param name="connection"> The <see cref="DbConnection" /> being used. </param>
/// <param name="command"> The <see cref="DbCommand" />. </param>
/// <param name="context"> The <see cref="DbContext" /> currently being used, to null if not known. </param>
/// <param name="executeMethod"> The <see cref="DbCommand" /> method. </param>
/// <param name="commandId"> A correlation ID that identifies the <see cref="DbCommand" /> instance being used. </param>
/// <param name="connectionId"> A correlation ID that identifies the <see cref="DbConnection" /> instance being used. </param>
/// <param name="async"> Indicates whether or not the command was executed asynchronously. </param>
/// <param name="logParameterValues"> Indicates whether or not the application allows logging of parameter values. </param>
/// <param name="startTime"> The start time of this event. </param>
/// <param name="commandSource">Source of the command.</param>
public CommandEventData(
EventDefinitionBase eventDefinition,
Func<EventDefinitionBase, EventData, string> messageGenerator,
DbConnection connection,
DbCommand command,
DbContext? context,
DbCommandMethod executeMethod,
Guid commandId,
Guid connectionId,
bool async,
bool logParameterValues,
DateTimeOffset startTime,
CommandSource commandSource)
: base(
eventDefinition,
messageGenerator,
Expand All @@ -48,7 +93,8 @@ public CommandEventData(
commandId,
connectionId,
async,
startTime)
startTime,
commandSource)
{
Command = command;
LogParameterValues = logParameterValues;
Expand Down
49 changes: 49 additions & 0 deletions src/EFCore.Relational/Diagnostics/CommandExecutedEventData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class CommandExecutedEventData : CommandEndEventData
/// <param name="logParameterValues"> Indicates whether or not the application allows logging of parameter values. </param>
/// <param name="startTime"> The start time of this event. </param>
/// <param name="duration"> The duration this event. </param>
[Obsolete("Use the overload with CommandSource")]
public CommandExecutedEventData(
EventDefinitionBase eventDefinition,
Func<EventDefinitionBase, EventData, string> messageGenerator,
Expand Down Expand Up @@ -57,6 +58,54 @@ public CommandExecutedEventData(
duration)
=> Result = result;

/// <summary>
/// Constructs the event payload.
/// </summary>
/// <param name="eventDefinition"> The event definition. </param>
/// <param name="messageGenerator"> A delegate that generates a log message for this event. </param>
/// <param name="connection"> The <see cref="DbConnection" /> being used. </param>
/// <param name="command"> The <see cref="DbCommand" /> that was executing when it failed. </param>
/// <param name="context"> The <see cref="DbContext" /> currently being used, to null if not known. </param>
/// <param name="executeMethod"> The <see cref="DbCommand" /> method that was used to execute the command. </param>
/// <param name="commandId"> A correlation ID that identifies the <see cref="DbCommand" /> instance being used. </param>
/// <param name="connectionId"> A correlation ID that identifies the <see cref="DbConnection" /> instance being used. </param>
/// <param name="result"> The result of executing the operation. </param>
/// <param name="async"> Indicates whether or not the command was executed asynchronously. </param>
/// <param name="logParameterValues"> Indicates whether or not the application allows logging of parameter values. </param>
/// <param name="startTime"> The start time of this event. </param>
/// <param name="commandSource">Source of the command.</param>
/// <param name="duration"> The duration this event. </param>
public CommandExecutedEventData(
EventDefinitionBase eventDefinition,
Func<EventDefinitionBase, EventData, string> messageGenerator,
DbConnection connection,
DbCommand command,
DbContext? context,
DbCommandMethod executeMethod,
Guid commandId,
Guid connectionId,
object? result,
bool async,
bool logParameterValues,
DateTimeOffset startTime,
CommandSource commandSource,
TimeSpan duration)
: base(
eventDefinition,
messageGenerator,
connection,
command,
context,
executeMethod,
commandId,
connectionId,
async,
logParameterValues,
startTime,
commandSource,
duration)
=> Result = result;

/// <summary>
/// The result of executing the command.
/// </summary>
Expand Down
59 changes: 59 additions & 0 deletions src/EFCore.Relational/Diagnostics/CommandSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// 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.Data.Common;

namespace Microsoft.EntityFrameworkCore.Diagnostics
{
/// <summary>
/// Enum used by <see cref="CommandEventData" />, and subclasses to indicate the
/// source of the <see cref="DbCommand" /> being used to execute the command.
/// </summary>
public enum CommandSource
{
/// <summary>
/// Unknown
/// </summary>
Unknown,

/// <summary>
/// Linq Query
/// </summary>
LinqQuery,

/// <summary>
/// Save Changes
/// </summary>
SaveChanges,

/// <summary>
/// Migrations
/// </summary>
Migrations,

/// <summary>
/// FromSqlQuery
/// </summary>
FromSqlQuery,

/// <summary>
/// ExecuteSqlRaw
/// </summary>
ExecuteSqlRaw,

/// <summary>
/// ValueGenerator
/// </summary>
ValueGenerator,

/// <summary>
/// Scaffolding
/// </summary>
Scaffolding,

/// <summary>
/// BulkUpdate
/// </summary>
BulkUpdate
}
}
Loading

0 comments on commit 85aaf40

Please sign in to comment.