-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
96 lines (78 loc) · 3.01 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using Microsoft.Extensions.DiagnosticAdapter;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net.Http;
using System.Threading.Tasks;
namespace ConsoleApp1
{
public class HttpRequestDiagnosticsListener : IObserver<DiagnosticListener>, IObserver<KeyValuePair<string, object>>
{
private readonly List<IDisposable> _subscriptions;
public HttpRequestDiagnosticsListener()
{
_subscriptions = new List<IDisposable>();
}
void IObserver<DiagnosticListener>.OnNext(DiagnosticListener diagnosticListener)
{
if (diagnosticListener.Name == "HttpHandlerDiagnosticListener")
{
var subscription = diagnosticListener.SubscribeWithAdapter(this, (s)=>true); // No HttpRequestOut events ((
//var subscription = diagnosticListener.Subscribe(this); // Works OK
_subscriptions.Add(subscription);
}
}
void IObserver<DiagnosticListener>.OnError(Exception error)
{ }
void IObserver<DiagnosticListener>.OnCompleted()
{
_subscriptions.ForEach(x => x.Dispose());
_subscriptions.Clear();
}
void IObserver<KeyValuePair<string, object>>.OnNext(KeyValuePair<string, object> pair)
{
Write(pair.Key, pair.Value);
}
void IObserver<KeyValuePair<string, object>>.OnError(Exception error)
{ }
void IObserver<KeyValuePair<string, object>>.OnCompleted()
{ }
private void Write(string name, object value)
{
Console.WriteLine(name);
Console.WriteLine(value);
Console.WriteLine();
}
[DiagnosticName("System.Net.Http.HttpRequestOut.Start")]
public virtual void OnHttpRequestOutStart(System.Net.Http.HttpRequestMessage request)
{
Console.WriteLine("OnHttpRequestOutStart");
}
[DiagnosticName("System.Net.Http.HttpRequestOut.Stop")]
public virtual void OnHttpRequestOutStop(System.Net.Http.HttpRequestMessage request, System.Net.Http.HttpResponseMessage response, TaskStatus requestTaskStatus)
{
Console.WriteLine("OnHttpRequestOutStop");
}
[DiagnosticName("System.Net.Http.Request")]
public virtual void OnRequest(System.Net.Http.HttpRequestMessage request)
{
Console.WriteLine("OnRequest");
}
[DiagnosticName("System.Net.Http.Response")]
public virtual void OnResponse(System.Net.Http.HttpResponseMessage response)
{
Console.WriteLine("OnResponse");
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Start!");
DiagnosticListener.AllListeners.Subscribe(new HttpRequestDiagnosticsListener());
var client = new HttpClient();
var r = client.GetAsync("http://www.google.com").Result;
Console.WriteLine("Hello World!");
}
}
}