- [DEVEXPRESS] Chia sẻ source code cách tạo biểu đồ sơ đồ tổ chức công ty Org Chart trên Winform C#
- [C#] Hướng dẫn tạo Auto Number trên Datagridview winform
- [DATABASE] Hướng dẫn tạo Procedure String Split in Mysql
- [C#] Thiết lập dấu (,) hay dấu (.) ở định dạng số đúng với định dạng số Việt Nam
- [C#] Chia sẻ source code Game Spin Lucky Wheel
- [C#] Hướng dẫn Encode and Decode HTML
- Hướng dẫn tạo tài khoản Chat Open AI GPT tại Việt Nam
- [C#] Hướng dẫn thay đổi giao diện ứng dụng Winform theo giao diện của Windows
- [VB.NET] Hiệu ứng Acrylic, Mica, Tabbed Blur Effect trên Winform
- [DEVEXPRESS] Hướng dẫn sử dụng HTML Template trên Combobox Edit
- [C#] Chia sẻ source code Orange Rain in Winform
- [DEVEXPRESS] Hướng dẫn sử dụng HTML Template trên XtraMessageBox Winform Devexpress 22.2.3
- [DEVEXPRESS] Hướng dẫn sử dụng HTML and CSS Code Viewer trên Winform
- [C#] Number Effect Counter up and down in winform
- [C#] Hướng dẫn Supend and Resume Process ID in Winform
- [C#] Hiển thị line number trên Richtextbox Winform
- [C#] Fake Blue Screen BSOD in winform
- [C#] Chia sẽ code demo sử dụng Async Parallel Foreach and For in Winform
- [C#] Sử dụng ActionBlock run X task at time winform
- [C#] Hướng dẫn sử dụng Property Grid để lưu và tải lại thông tin cấu hình user trên winform
[C#] Theo dõi Monitoring all tất cả các Request Http từ ứng dụng .NET
Xin chào các bạn, bài viết hôm nay mình tiếp tục chia sẻ các bạn cách theo dõi các Http Request bằng ngôn ngữ lập trình C#.
Bài viết này dùng để theo dõi các dữ liệu request từ ứng dụng, để ghi log.
[C#] Monitoring all Http requests in NET Application
Ví dụ: Trên ứng dụng của bạn chạy request đến các website sau.
https://laptrinhvb.net
https://howkteam.vn
https://freetuts.net
https://genk.vn
https://zing.vn
https://nld.com.vn
https://thanhnien.vn
https://kenh14.vn
Code thực hiện C# trên Net Core 6:
using System.ComponentModel.DataAnnotations;
using System.Net;
using var eventListener = new HttpEventListenerAsyncLocal();
var client = new HttpClient();
await client.GetStringAsync("https://laptrinhvb.net");
await client.GetStringAsync("https://howkteam.vn");
await client.GetStringAsync("https://freetuts.net");
await client.GetStringAsync("https://genk.vn");
await client.GetStringAsync("https://zing.vn");
await client.GetStringAsync("https://nld.com.vn");
await client.GetStringAsync("https://thanhnien.vn");
await client.GetStringAsync("https://kenh14.vn");
var webClient = new WebClient();
webClient.DownloadStringAsync(new Uri("https://quantrimang.com.vn"));
Console.Read();
Và Sử dụng nhiều API phương thức để request như: HttpClient, Httpwebrequest hay Webclient...
và các bạn muốn theo dõi các request này như ứng dụng bên dưới đây.
Các bạn thấy code ở trên mình request các website, và mình có 1 class Handler, để theo dõi thời gian xứ lý truy cập hoàn thành của từng http request đó.
Tạo 1 class HttpEventListenerAsyncLocal.cs
using System.Diagnostics;
using System.Diagnostics.Tracing;
internal sealed class HttpEventListenerAsyncLocal : EventListener
{
private readonly AsyncLocal<Request> _currentRequest = new();
private sealed record Request(string Url, Stopwatch ExecutionTime);
protected override void OnEventSourceCreated(EventSource eventSource)
{
if (eventSource.Name == "System.Net.Http")
{
EnableEvents(eventSource, EventLevel.Informational, EventKeywords.All);
}
base.OnEventSourceCreated(eventSource);
}
protected override void OnEventWritten(EventWrittenEventArgs eventData)
{
if (eventData.EventId == 1) // eventData.EventName == "RequestStart"
{
var scheme = (string)eventData.Payload[0];
var host = (string)eventData.Payload[1];
var port = (int)eventData.Payload[2];
var pathAndQuery = (string)eventData.Payload[3];
_currentRequest.Value = new Request($"{scheme}://{host}:{port}{pathAndQuery}", Stopwatch.StartNew());
Console.WriteLine($"GET {_currentRequest.Value.Url}");
}
else if (eventData.EventId == 2) // eventData.EventName == "RequestStop"
{
var currentRequest = _currentRequest.Value;
if (currentRequest != null)
{
Console.WriteLine($"OK {currentRequest.Url} executed in {currentRequest.ExecutionTime.ElapsedMilliseconds:F1}ms");
}
}
}
}
Các bạn có thể Monitoring các thông tin của request ở lớp này để show ra ứng dụng.
Thanks for watching!