RDOSRequestCounter 1.0.5

There is a newer version of this package available.
See the version list below for details.
dotnet add package RDOSRequestCounter --version 1.0.5
                    
NuGet\Install-Package RDOSRequestCounter -Version 1.0.5
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="RDOSRequestCounter" Version="1.0.5" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="RDOSRequestCounter" Version="1.0.5" />
                    
Directory.Packages.props
<PackageReference Include="RDOSRequestCounter" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add RDOSRequestCounter --version 1.0.5
                    
#r "nuget: RDOSRequestCounter, 1.0.5"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package RDOSRequestCounter@1.0.5
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=RDOSRequestCounter&version=1.0.5
                    
Install as a Cake Addin
#tool nuget:?package=RDOSRequestCounter&version=1.0.5
                    
Install as a Cake Tool

RDOS.RequestCounter

Library .NET 5.0 để đếm request và lưu trữ vào PostgreSQL một cách bất đồng bộ, không làm chậm middleware pipeline.

Tính năng chính

  • Non-blocking Middleware: Middleware không đợi database insert
  • Async Background Processing: Xử lý request data trong background service
  • PostgreSQL Storage: Lưu trữ vào PostgreSQL với Entity Framework Core
  • Batch Processing: Insert nhiều records cùng lúc để tối ưu performance
  • Channel-based Queue: Sử dụng System.Threading.Channels cho async processing
  • Configurable: Nhiều tùy chọn cấu hình linh hoạt
  • Health Checks: Tích hợp health checks cho database
  • Error Handling: Retry logic và error logging
  • Metrics Endpoint: API để xem real-time metrics

Cài đặt

1. Thêm Package Reference

<PackageReference Include="RDOS.RequestCounter" Version="1.0.0" />

Lưu ý: Library được tối ưu cho .NET 5.0 và tương thích với .NET 5.0+.

2. Cấu hình Connection String

Trong appsettings.json:

{
  "ConnectionStrings": {
    "RequestCounter": "Host=localhost;Database=requestdb;Username=your_user;Password=your_password"
  }
}

3. Đăng ký Services

Trong Program.cs:

using RDOS.RequestCounter.Extensions;
using RDOS.RequestCounter.Configuration;

var builder = WebApplication.CreateBuilder(args);

// Thiết lập connection string
RequestCounterConfig.SetConnectionString("Host=localhost;Database=requestdb;Username=your_user;Password=your_password");

// Thêm RequestCounter
builder.Services.AddRequestCounter();

var app = builder.Build();

// Sử dụng middleware
app.UseRequestCounter();

// Đảm bảo database được tạo
await app.EnsureRequestCounterDatabaseAsync();

app.Run();

Cấu hình nâng cao

Cách 1: Sử dụng static configuration

// Thiết lập connection string
RequestCounterConfig.SetConnectionString("your_connection_string");

// Cấu hình default options
RequestCounterConfig.ConfigureDefaultOptions(options =>
{
    options.ChannelCapacity = 10000;           // Kích thước buffer
    options.BatchSize = 100;                   // Số records per batch
    options.BatchTimeout = TimeSpan.FromSeconds(5); // Timeout cho batch
    options.EnableResponseTimeTracking = true; // Theo dõi response time
    options.EnableIpTracking = true;           // Theo dõi IP address
    options.EnableUserAgentTracking = true;    // Theo dõi User Agent
    options.IgnoredPaths = new[] { "/health", "/metrics" }; // Paths bỏ qua
    options.MaxRetryAttempts = 3;              // Số lần retry
});

// Đăng ký services
builder.Services.AddRequestCounter();

Cách 2: Cấu hình trực tiếp

builder.Services.AddRequestCounter(options =>
{
    options.ConnectionString = "your_connection_string";
    options.ChannelCapacity = 10000;
    options.BatchSize = 100;
    options.BatchTimeout = TimeSpan.FromSeconds(5);
    options.EnableResponseTimeTracking = true;
    options.EnableIpTracking = true;
    options.EnableUserAgentTracking = true;
    options.IgnoredPaths = new[] { "/health", "/metrics" };
    options.MaxRetryAttempts = 3;
});

Database Schema

Library sẽ tự động tạo bảng monitoring_request_metrics với các cột:

  • id: Primary key
  • timestamp: Thời gian request
  • method: HTTP method (GET, POST, etc.)
  • path: Request path
  • status_code: HTTP status code
  • response_time_ms: Thời gian response (milliseconds)
  • ip_address: IP address của client
  • user_agent: User agent string
  • content_length: Content length
  • query_string: Query string (nếu enable)
  • referer: Referer header (nếu enable)
  • created_at: Thời gian tạo record

Metrics API

GET /metrics/request-counter

Xem real-time metrics với các query parameters:

  • from: Thời gian bắt đầu (ISO 8601)
  • to: Thời gian kết thúc (ISO 8601)
  • method: Filter theo HTTP method
  • path: Filter theo path pattern

Ví dụ:

GET /metrics/request-counter?from=2024-01-01T00:00:00Z&method=GET

Response:

{
  "totalRequests": 1500,
  "averageResponseTime": 125.5,
  "statusCodes": [
    { "statusCode": 200, "count": 1200 },
    { "statusCode": 404, "count": 200 },
    { "statusCode": 500, "count": 100 }
  ],
  "methods": [
    { "method": "GET", "count": 800 },
    { "method": "POST", "count": 700 }
  ],
  "period": {
    "from": "2024-01-01T00:00:00Z",
    "to": null
  }
}

Performance Considerations

Tối ưu Database

  1. Indexes: Library tự động tạo indexes cho các cột thường query
  2. Batch Processing: Insert nhiều records cùng lúc
  3. Connection Pooling: Sử dụng EF Core connection pooling
  4. Async Operations: Tất cả database operations đều async

Tối ưu Application

  1. Channel Capacity: Điều chỉnh ChannelCapacity phù hợp với load
  2. Batch Size: Tăng BatchSize cho high-throughput applications
  3. Ignored Paths: Bỏ qua các path không cần thiết (health checks, etc.)

Health Checks

Library tự động đăng ký health check cho database:

// Kiểm tra health
app.MapHealthChecks("/health");

Error Handling

  • Retry Logic: Tự động retry khi database insert thất bại
  • Circuit Breaker: Tránh cascade failures
  • Logging: Log errors với configurable level
  • Graceful Degradation: Application vẫn hoạt động khi database down

Monitoring

Logs

Library sử dụng structured logging với các log levels:

  • Information: Service start/stop
  • Debug: Batch processing details
  • Warning: Retry attempts
  • Error: Database failures

Metrics

Sử dụng metrics endpoint để monitor:

  • Request volume
  • Response times
  • Error rates
  • Status code distribution

Troubleshooting

Database Connection Issues

  1. Kiểm tra connection string
  2. Đảm bảo PostgreSQL đang chạy
  3. Kiểm tra network connectivity
  4. Verify user permissions

Performance Issues

  1. Tăng BatchSizeChannelCapacity
  2. Optimize database indexes
  3. Monitor memory usage
  4. Check background service logs

Missing Data

  1. Kiểm tra IgnoredPaths configuration
  2. Verify database permissions
  3. Check background service status
  4. Review error logs

License

MIT License

Support

Để được hỗ trợ, vui lòng tạo issue trên GitHub repository.

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.6 0 9/29/2025
1.0.5 171 9/22/2025
1.0.4 155 9/22/2025