IRequestLoggingFeature

This commit is contained in:
陈国伟 2021-10-29 09:40:30 +08:00
parent 31655bf890
commit 810aab081f
5 changed files with 33 additions and 2 deletions

View File

@ -0,0 +1,13 @@
namespace FastGithub.HttpServer
{
/// <summary>
/// 请求日志特性
/// </summary>
public interface IRequestLoggingFeature
{
/// <summary>
/// 是否启用
/// </summary>
bool Enable { get; set; }
}
}

View File

@ -32,6 +32,8 @@ namespace FastGithub.HttpServer
/// <returns></returns>
public async Task InvokeAsync(HttpContext context, RequestDelegate next)
{
var feature = new RequestLoggingFeature();
context.Features.Set<IRequestLoggingFeature>(feature);
var stopwatch = Stopwatch.StartNew();
try
@ -43,6 +45,11 @@ namespace FastGithub.HttpServer
stopwatch.Stop();
}
if (feature.Enable == false)
{
return;
}
var request = context.Request;
var response = context.Response;
var message = $"{request.Method} {request.Scheme}://{request.Host}{request.Path} responded {response.StatusCode} in {stopwatch.Elapsed.TotalMilliseconds} ms";
@ -119,5 +126,10 @@ namespace FastGithub.HttpServer
}
return builder.ToString();
}
private class RequestLoggingFeature : IRequestLoggingFeature
{
public bool Enable { get; set; } = true;
}
}
}

View File

@ -146,7 +146,7 @@
<TabControl Style="{StaticResource TabControlWithUnderLineStyle}" Foreground="Black" Background="Transparent" BorderBrush="Transparent" BorderThickness="0">
<TabItem Style="{StaticResource TabItemExWithUnderLineStyle}" Cursor="Hand" Header="流量监控" Height="40" Width="100" Margin="5 0" FontSize="18">
<Grid Background="#ddd">
<WebBrowser Source="http://127.0.0.1/flow.html" />
<WebBrowser Source="http://127.0.0.1/flow.html"/>
</Grid>
</TabItem>

View File

@ -1,5 +1,6 @@
using FastGithub.Configuration;
using FastGithub.FlowAnalyze;
using FastGithub.HttpServer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
@ -70,6 +71,11 @@ namespace FastGithub
{
endpoint.MapGet("/flowRates", context =>
{
var feature = context.Features.Get<IRequestLoggingFeature>();
if (feature != null)
{
feature.Enable = false;
}
var flowRate = context.RequestServices.GetRequiredService<IFlowAnalyzer>().GetFlowRate();
return context.Response.WriteAsJsonAsync(flowRate);
});

View File

@ -20,7 +20,7 @@
</style>
</head>
<body>
<body onContextMenu="return false">
<div id="chart"></div>
<script type="text/javascript">