This repository was archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Added diagnostics to sqlclient #8420
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
src/System.Data.SqlClient/src/System/Data/SqlClient/SqlClientDiagnosticListenerExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| using System.Collections; | ||
| using System.Diagnostics; | ||
| using System.Runtime.CompilerServices; | ||
|
|
||
| namespace System.Data.SqlClient | ||
| { | ||
| /// <summary> | ||
| /// Extension methods on the DiagnosticListener class to log SqlCommand data | ||
| /// </summary> | ||
| internal static class SqlClientDiagnosticListenerExtensions | ||
| { | ||
| public const string DiagnosticListenerName = "SqlClientDiagnosticListener"; | ||
|
|
||
| private const string SqlClientPrefix = "System.Data.SqlClient."; | ||
| public const string SqlBeforeExecuteCommand = SqlClientPrefix + nameof(WriteCommandBefore); | ||
| public const string SqlAfterExecuteCommand = SqlClientPrefix + nameof(WriteCommandAfter); | ||
| public const string SqlErrorExecuteCommand = SqlClientPrefix + nameof(WriteCommandError); | ||
|
|
||
| public static Guid WriteCommandBefore(this DiagnosticListener @this, SqlCommand sqlCommand, [CallerMemberName] string operation = "") | ||
| { | ||
| if (@this.IsEnabled(SqlBeforeExecuteCommand)) | ||
| { | ||
| Guid operationId = Guid.NewGuid(); | ||
|
|
||
| @this.Write( | ||
| SqlBeforeExecuteCommand, | ||
| new | ||
| { | ||
| OperationId = operationId, | ||
| Operation = operation, | ||
| Command = sqlCommand | ||
| }); | ||
|
|
||
| return operationId; | ||
| } | ||
| else | ||
| return Guid.Empty; | ||
| } | ||
|
|
||
| public static void WriteCommandAfter(this DiagnosticListener @this, Guid operationId, SqlCommand sqlCommand, [CallerMemberName] string operation = "") | ||
| { | ||
| if (@this.IsEnabled(SqlAfterExecuteCommand)) | ||
| { | ||
| @this.Write( | ||
| SqlAfterExecuteCommand, | ||
| new | ||
| { | ||
| OperationId = operationId, | ||
| Operation = operation, | ||
| Command = sqlCommand, | ||
| Statistics = sqlCommand.Statistics?.GetDictionary(), | ||
| Timestamp = Stopwatch.GetTimestamp() | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| public static void WriteCommandError(this DiagnosticListener @this, Guid operationId, SqlCommand sqlCommand, Exception ex, [CallerMemberName] string operation = "") | ||
| { | ||
| if (@this.IsEnabled(SqlErrorExecuteCommand)) | ||
| { | ||
| @this.Write( | ||
| SqlErrorExecuteCommand, | ||
| new | ||
| { | ||
| OperationId = operationId, | ||
| Operation = operation, | ||
| Command = sqlCommand, | ||
| Exception = ex, | ||
| Timestamp = Stopwatch.GetTimestamp() | ||
| }); | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What "enables" the diagnosticListener? How can this be disabled?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A listener is enabled when an observer is registered and self-reports as listening to the diagnosticListener. So in practice it will be disabled by default and then enabled once an interested observer shows up.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nbilling How is the observer registered? Any samples on the usage of this capability?
What code / class should I look at, to see what the observer is going to look like?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the tests there is a toy observer called
FakeDiagnosticListenerObserver. It gets hooked up to the SqlClient DiagnosticListener on lineDiagnosticListener.AllListeners.Subscribe(diagnosticListenerObserver);(wherediagnosticListenerObserveris aFakeDiagnosticListenerObserver).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, @nbilling, for answering these questions! @saurabh500 let me know if you have any more questions about this.