NEWS

[C#] Theo dõi Monitoring all tất cả các Request Http từ ứng dụng .NET

[C#] Theo dõi Monitoring all tất cả các Request Http từ ứng dụng .NET
Đăng bởi: Thảo Meo - Lượt xem: 2448 11:52:21, 16/09/2022C#   In bài viết

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

WrappingHttpRequest

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.

ObserveWrapHttp

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!

 

DOWNLOAD SOURCE

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Theo dõi Monitoring all tất cả các Request Http từ ứng dụng .NET
Đăng bởi: Thảo Meo - Lượt xem: 2448 11:52:21, 16/09/2022C#   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.