Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create diagnosticsource-getting-started.md #29255

Merged
merged 29 commits into from
Jun 2, 2022
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
1901847
Create diagnosticsource-getting-started.md
mikelle-rogers May 2, 2022
ebf9141
DiagnosticSource Overview page
mikelle-rogers May 3, 2022
d29f7b0
Alter formatting and clarify contents of the file
mikelle-rogers May 3, 2022
3cdb16c
Update diagnosticsource.md
mikelle-rogers May 3, 2022
48c76a0
Fixing lint failures
mikelle-rogers May 3, 2022
012a00d
Fix up the code and finish up the documentation
mikelle-rogers May 5, 2022
14cd364
Not needed
mikelle-rogers May 5, 2022
0e80142
Update the date
mikelle-rogers May 5, 2022
f530c99
Combine into one file
mikelle-rogers May 5, 2022
f1998d9
Not needed
mikelle-rogers May 5, 2022
7bd1010
Remove whitespace
mikelle-rogers May 5, 2022
9e27b55
Fixed suggestions from code review
mikelle-rogers May 9, 2022
672fb48
Two additional edits
mikelle-rogers May 9, 2022
19ebd1e
Response to suggestions
mikelle-rogers May 10, 2022
9a72826
Update to working example
mikelle-rogers May 13, 2022
15c251e
Add a line, remove a space
mikelle-rogers May 13, 2022
3b085cf
Updating from feedback on code review
mikelle-rogers May 23, 2022
5c42c6f
Add DiagnosticSource and DiagnosticListener to toc
mikelle-rogers May 25, 2022
3edf4ce
Add .csproj and .cs files
mikelle-rogers May 25, 2022
5eb2e4f
Add links to the .cs and .csproj folders
mikelle-rogers May 25, 2022
3504d75
Merge branch 'main' into dev/mirogers/diagnosticsource-getting-started
mikelle-rogers May 25, 2022
6f2f3bc
Move the file to the correct place
mikelle-rogers May 25, 2022
d8c5a42
move location, add blocking
mikelle-rogers May 25, 2022
a9b6a75
Moving to correct address for real
mikelle-rogers May 25, 2022
f1550c3
Linking the code to the snippits
mikelle-rogers May 25, 2022
7971595
Correct filepath
mikelle-rogers May 25, 2022
04eb980
Responding to feedback
mikelle-rogers May 25, 2022
2802ddc
Added missing character
mikelle-rogers May 25, 2022
5f0fc2c
removing links
mikelle-rogers Jun 2, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions docs/core/diagnostics/DiagnosticSource.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
mikelle-rogers marked this conversation as resolved.
Show resolved Hide resolved

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
71 changes: 71 additions & 0 deletions docs/core/diagnostics/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System.Diagnostics;
MyListener TheListener = new MyListener();
TheListener.Listening();
HTTPClient Client = new HTTPClient();
Client.SendWebRequest("https://docs.microsoft.com/dotnet/core/diagnostics/");
class HTTPClient
{
private static DiagnosticSource httpLogger = new DiagnosticListener("System.Net.Http");
public byte[] SendWebRequest(string url)
{
if (httpLogger.IsEnabled("RequestStart"))
{
httpLogger.Write("RequestStart", new { Url = url });
}
//Pretend this sends an HTTP request to the url and gets back a reply.
byte[] reply = new byte[] { };
return reply;
}
}
class Observer<T> : IObserver<T>
{
public Observer(Action<T> onNext, Action onCompleted)
{
_onNext = onNext ?? new Action<T>(_ => { });
_onCompleted = onCompleted ?? new Action(() => { });
}
public void OnCompleted() { _onCompleted(); }
public void OnError(Exception error) { }
public void OnNext(T value) { _onNext(value); }
private Action<T> _onNext;
private Action _onCompleted;
}
class MyListener
{
IDisposable networkSubscription;
IDisposable listenerSubscription;
private readonly object allListeners = new();
public void Listening()
{
Action<KeyValuePair<string, object>> whenHeard = delegate (KeyValuePair<string, object> data)
{
Console.WriteLine($"Data received: {data.Key}: {data.Value}");
};
Action<DiagnosticListener> onNewListener = delegate (DiagnosticListener listener)
{
Console.WriteLine($"New Listener discovered: {listener.Name}");
//Suscribe to the specific DiagnosticListener of interest.
if (listener.Name == "System.Net.Http")
{
//Use lock to ensure the callback code is thread safe.
lock (allListeners)
{
if (networkSubscription != null)
{
networkSubscription.Dispose();
}
IObserver<KeyValuePair<string, object>> iobserver = new Observer<KeyValuePair<string, object>>(whenHeard, null);
networkSubscription = listener.Subscribe(iobserver);
}

}
};
//Subscribe to discover all DiagnosticListeners
IObserver<DiagnosticListener> observer = new Observer<DiagnosticListener>(onNewListener, null);
//When a listener is created, invoke the onNext function which calls the delegate.
listenerSubscription = DiagnosticListener.AllListeners.Subscribe(observer);
}
// Typically you leave the listenerSubscription subscription active forever.
// However when you no longer want your callback to be called, you can
// call listenerSubscription.Dispose() to cancel your subscription to the IObservable.
}
Loading