Rin 可以捕获对 ASP.NET Core 应用程序的 HTTP 请求,并为捕获的数据提供查看器。它是调试 Web 应用程序(例如,网站、API 应用)的有用工具。
先看如下代码:
var builder = WebApplication.CreateBuilder(args);
builder.Logging.AddRinLogger();
builder.Services.AddRin();
builder.Services.AddHttpClient();
var app = builder.Build();
app.UseRin();
app.UseRinDiagnosticsHandler();
var summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
app.MapGet("/weatherforecast", () =>
{
var forecast = Enumerable.Range(1, 5).Select(index =>
new WeatherForecast
(
DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
Random.Shared.Next(-20, 55),
summaries[Random.Shared.Next(summaries.Length)]
))
.ToArray();
return forecast;
});
app.MapPost("/weatherforecast", (WeatherForecast weather) =>
{
return weather;
});
app.MapPut("/weatherforecast", (WeatherForecast weather) =>
{
return weather;
});
app.MapDelete("/weatherforecast", (int id) =>
{
return true;
});
app.MapGet("/weather", async (IHttpClientFactory clientFactory) =>
{
var lat = "35.71459525981295";
var lon = "139.7989249730396";
var apiKey = "6d43c019aa2aeb0e7dd5ce070f174248";
var client = clientFactory.CreateClient();
var url = $"https://api.openweathermap.org/data/2.5/forecast?lat={lat}&lon={lon}&appid={apiKey}";
Console.WriteLine(url);
var response = await client.GetAsync(url);
return response.Content.ReadAsStringAsync();
});
app.Run();
internal record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
{
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}
Rin是内置的,运行起程序后,只需要换成/rin,就可以访问具体请求信息,这点非常方便,轻巧。如下图,是get请求的相关信息。
Request基本信息:
Response基本信息:
Timleline:这是区分了Processing和Transferring两个事件。
上面是get请求的三个Tab信息。post,put,delete等其他类型请求类似,下图是post的请求,增加了请求body信息。
可能你觉得,如果只有这么简单,那直接用Postman等客户端工具也是可以的。当然,是可以的,但Rin不仅如此,还可以对Web内部对第三方的请求也给记录下来。
下图是在Timeline,在Proccessing和Transferring增加Trace,来清晰地表示客户端一个请求,后端的调用链路。
具体的Trace可以参看Trace的Tab,这里清晰地说明了4个Trace分别对应的Start processing HTTP Request,Sending HTTP request,Received HTTP response,End procession HTTP request。
Rin很轻量,建议在调试时用,在生产环境上,有专业的日志,监控和链路跟踪系统,并不需要。虽然Rin已有两年没有更新,但它的轻量,容用,基本能满足我们的需求了。
声明:文中观点不代表本站立场。本文传送门:https://eyangzhen.com/392764.html