Skip to content

Commit 71ab7c4

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.
1 parent 89d1503 commit 71ab7c4

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-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 DataReaderDisposed = NamePrefix + nameof(DataReaderDisposed);
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 WriteDataReaderDisposed(this DiagnosticSource diagnosticSource, RelationalDataReader dataReader)
96+
{
97+
if (diagnosticSource.IsEnabled(DataReaderDisposed))
98+
{
99+
diagnosticSource.Write(DataReaderDisposed, dataReader);
100+
}
101+
}
91102
}
92103
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ protected virtual object Execute(
226226
= new RelationalDataReader(
227227
connection,
228228
dbCommand,
229-
dbCommand.ExecuteReader());
229+
dbCommand.ExecuteReader(), DiagnosticSource);
230230
}
231231
catch
232232
{
@@ -344,7 +344,8 @@ protected virtual async Task<object> ExecuteAsync(
344344
result = new RelationalDataReader(
345345
connection,
346346
dbCommand,
347-
await dbCommand.ExecuteReaderAsync(cancellationToken));
347+
await dbCommand.ExecuteReaderAsync(cancellationToken),
348+
DiagnosticSource);
348349
}
349350
catch
350351
{

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

Lines changed: 8 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,20 @@ 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));
4146

4247
_connection = connection;
4348
_command = command;
4449
_reader = reader;
50+
_diagnosticSource = diagnosticSource;
4551
}
4652

4753
/// <summary>
@@ -65,6 +71,7 @@ public virtual void Dispose()
6571
{
6672
if (!_disposed)
6773
{
74+
_diagnosticSource.WriteDataReaderDisposed(this);
6875
_reader.Dispose();
6976
_command.Dispose();
7077
_connection?.Close();

0 commit comments

Comments
 (0)