-
Notifications
You must be signed in to change notification settings - Fork 299
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Supporting Event counter in .Net core (#719)
* Add event counters * Add support netstandard 2.1 & fix the conflict in event source * Support new event source types & add the test unit * Remove supporting obsolete types * fix unit test * Add snippet sample code * Address comments * Fix minor typo (#3) * Reformatting counter methods * Fix minor typo * Removed IsEnabled condition and reformatted counter methods * Unit tests for Microsoft.Data.SqlClient.SqlClientEventSource (#2) * Implemented tests for Microsoft.Data.SqlClient.SqlClientEventSource * Updated the EventCounter test to reflect the recent changes in the code * Working on EventCounter tests access event counters through reflection * Updated the EventCounterTest to use reflection * Fixing dangling SqlConnection's left in tests * EventCountersTest now checks hard/soft connects/disconnects counters * Reverted the DataTestUtility changes * Reverted using statements to the old-style in tests * Reverted the ConnectionPoolTest.ReclaimEmancipatedOnOpenTest() * Reverted using statements to the old-style in tests * Reverted using statements to the old-style in tests * Rewrite the EventCounterTest assertions not to conflict with other tests * Code review cleanup * Add more tests (#5) Added EventCounter_ReclaimedConnectionsCounter_Functional & EventCounter_ConnectionPoolGroupsCounter_Functional tests. * Address comments Co-authored-by: Davoud Eshtehari <[email protected]> Co-authored-by: Davoud Eshtehari <[email protected]> Co-authored-by: Karina Zhou <[email protected]> Co-authored-by: Nikita Kobzev <[email protected]>
- Loading branch information
1 parent
0005645
commit 884b113
Showing
20 changed files
with
1,097 additions
and
137 deletions.
There are no files selected for viewing
This file contains 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,62 @@ | ||
// <Snippet1> | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.Tracing; | ||
using System.Linq; | ||
|
||
// This listener class will listen for events from the SqlClientEventSource class. | ||
// SqlClientEventSource is an implementation of the EventSource class which gives | ||
// it the ability to create events. | ||
public class EventCounterListener : EventListener | ||
{ | ||
protected override void OnEventSourceCreated(EventSource eventSource) | ||
{ | ||
// Only enable events from SqlClientEventSource. | ||
if (eventSource.Name.Equals("Microsoft.Data.SqlClient.EventSource")) | ||
{ | ||
var options = new Dictionary<string, string>(); | ||
// define time interval 1 second | ||
// without defining this parameter event counters will not enabled | ||
options.Add("EventCounterIntervalSec", "1"); | ||
// enable for the None keyword | ||
EnableEvents(eventSource, EventLevel.Informational, EventKeywords.None, options); | ||
} | ||
} | ||
|
||
// This callback runs whenever an event is written by SqlClientEventSource. | ||
// Event data is accessed through the EventWrittenEventArgs parameter. | ||
protected override void OnEventWritten(EventWrittenEventArgs eventData) | ||
{ | ||
if (eventData.Payload.FirstOrDefault(p => p is IDictionary<string, object> x && x.ContainsKey("Name")) is IDictionary<string, object> counters) | ||
{ | ||
if (counters.TryGetValue("DisplayName", out object name) && name is string cntName | ||
&& counters.TryGetValue("Mean", out object value) && value is double cntValue) | ||
{ | ||
// print event counter's name and mean value | ||
Console.WriteLine($"{cntName}\t\t{cntValue}"); | ||
} | ||
} | ||
} | ||
} | ||
|
||
class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
// Create a new event listener | ||
using (var listener = new EventCounterListener()) | ||
{ | ||
string connectionString = "Data Source=localhost; Integrated Security=true"; | ||
|
||
for (int i = 0; i < 50; i++) | ||
{ | ||
// Open a connection | ||
SqlConnection cnn = new SqlConnection(connectionString); | ||
cnn.Open(); | ||
// wait for sampling interval happens | ||
System.Threading.Thread.Sleep(500); | ||
} | ||
} | ||
} | ||
} | ||
// </Snippet1> |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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.