Skip to content

Commit 2a66ac9

Browse files
author
Fitzchak Yitzchaki
committed
dotnet#7105 Trace when query is done. This is needed in order to get SelectRows statistics. Take dotnet#2: Use the DiagnosticSource on RelationalDataReader. Take dotnet#3: Fix requested changes.
1 parent 89d1503 commit 2a66ac9

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

src/Microsoft.EntityFrameworkCore.Relational/Internal/RelationalDiagnostics.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Data.Common;
66
using System.Diagnostics;
7+
using Microsoft.EntityFrameworkCore.Storage;
78

89
namespace Microsoft.EntityFrameworkCore.Internal
910
{
@@ -15,6 +16,8 @@ internal static class RelationalDiagnostics
1516
public const string AfterExecuteCommand = NamePrefix + nameof(AfterExecuteCommand);
1617
public const string CommandExecutionError = NamePrefix + nameof(CommandExecutionError);
1718

19+
public const string DataReaderDisposing = NamePrefix + nameof(DataReaderDisposing);
20+
1821
public static void WriteCommandBefore(
1922
this DiagnosticSource diagnosticSource,
2023
DbCommand command, string executeMethod,
@@ -88,5 +91,13 @@ public static void WriteCommandError(
8891
});
8992
}
9093
}
94+
95+
public static void WriteDataReaderDisposing(this DiagnosticSource diagnosticSource, DbDataReader dataReader)
96+
{
97+
if (diagnosticSource.IsEnabled(DataReaderDisposing))
98+
{
99+
diagnosticSource.Write(DataReaderDisposing, dataReader);
100+
}
101+
}
91102
}
92103
}

src/Microsoft.EntityFrameworkCore.Relational/Storage/Internal/RelationalCommand.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,8 @@ protected virtual object Execute(
226226
= new RelationalDataReader(
227227
connection,
228228
dbCommand,
229-
dbCommand.ExecuteReader());
229+
dbCommand.ExecuteReader(),
230+
DiagnosticSource);
230231
}
231232
catch
232233
{
@@ -344,7 +345,8 @@ protected virtual async Task<object> ExecuteAsync(
344345
result = new RelationalDataReader(
345346
connection,
346347
dbCommand,
347-
await dbCommand.ExecuteReaderAsync(cancellationToken));
348+
await dbCommand.ExecuteReaderAsync(cancellationToken),
349+
DiagnosticSource);
348350
}
349351
catch
350352
{

src/Microsoft.EntityFrameworkCore.Relational/Storage/RelationalDataReader.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33

44
using System;
55
using System.Data.Common;
6+
using System.Diagnostics;
67
using JetBrains.Annotations;
8+
using Microsoft.EntityFrameworkCore.Internal;
79
using Microsoft.EntityFrameworkCore.Utilities;
810

911
namespace Microsoft.EntityFrameworkCore.Storage
@@ -22,6 +24,7 @@ public class RelationalDataReader : IDisposable
2224
private readonly IRelationalConnection _connection;
2325
private readonly DbCommand _command;
2426
private readonly DbDataReader _reader;
27+
private readonly DiagnosticSource _diagnosticSource;
2528

2629
private bool _disposed;
2730

@@ -31,17 +34,21 @@ public class RelationalDataReader : IDisposable
3134
/// <param name="connection"> The connection. </param>
3235
/// <param name="command"> The command that was executed. </param>
3336
/// <param name="reader"> The underlying reader for the result set. </param>
37+
/// <param name="diagnosticSource"> The diagnostic source. </param>
3438
public RelationalDataReader(
3539
[CanBeNull] IRelationalConnection connection,
3640
[NotNull] DbCommand command,
37-
[NotNull] DbDataReader reader)
41+
[NotNull] DbDataReader reader,
42+
[NotNull] DiagnosticSource diagnosticSource)
3843
{
3944
Check.NotNull(command, nameof(command));
4045
Check.NotNull(reader, nameof(reader));
46+
Check.NotNull(diagnosticSource, nameof(diagnosticSource));
4147

4248
_connection = connection;
4349
_command = command;
4450
_reader = reader;
51+
_diagnosticSource = diagnosticSource;
4552
}
4653

4754
/// <summary>
@@ -65,6 +72,7 @@ public virtual void Dispose()
6572
{
6673
if (!_disposed)
6774
{
75+
_diagnosticSource.WriteDataReaderDisposing(_reader);
6876
_reader.Dispose();
6977
_command.Dispose();
7078
_connection?.Close();

0 commit comments

Comments
 (0)