IRequestLoggingFeature
This commit is contained in:
parent
31655bf890
commit
810aab081f
13
FastGithub.HttpServer/IRequestLoggingFeature.cs
Normal file
13
FastGithub.HttpServer/IRequestLoggingFeature.cs
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
namespace FastGithub.HttpServer
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 请求日志特性
|
||||||
|
/// </summary>
|
||||||
|
public interface IRequestLoggingFeature
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 是否启用
|
||||||
|
/// </summary>
|
||||||
|
bool Enable { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -32,6 +32,8 @@ namespace FastGithub.HttpServer
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task InvokeAsync(HttpContext context, RequestDelegate next)
|
public async Task InvokeAsync(HttpContext context, RequestDelegate next)
|
||||||
{
|
{
|
||||||
|
var feature = new RequestLoggingFeature();
|
||||||
|
context.Features.Set<IRequestLoggingFeature>(feature);
|
||||||
var stopwatch = Stopwatch.StartNew();
|
var stopwatch = Stopwatch.StartNew();
|
||||||
|
|
||||||
try
|
try
|
||||||
@ -43,6 +45,11 @@ namespace FastGithub.HttpServer
|
|||||||
stopwatch.Stop();
|
stopwatch.Stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (feature.Enable == false)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
var request = context.Request;
|
var request = context.Request;
|
||||||
var response = context.Response;
|
var response = context.Response;
|
||||||
var message = $"{request.Method} {request.Scheme}://{request.Host}{request.Path} responded {response.StatusCode} in {stopwatch.Elapsed.TotalMilliseconds} ms";
|
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();
|
return builder.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private class RequestLoggingFeature : IRequestLoggingFeature
|
||||||
|
{
|
||||||
|
public bool Enable { get; set; } = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
using FastGithub.Configuration;
|
using FastGithub.Configuration;
|
||||||
using FastGithub.FlowAnalyze;
|
using FastGithub.FlowAnalyze;
|
||||||
|
using FastGithub.HttpServer;
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
@ -70,6 +71,11 @@ namespace FastGithub
|
|||||||
{
|
{
|
||||||
endpoint.MapGet("/flowRates", context =>
|
endpoint.MapGet("/flowRates", context =>
|
||||||
{
|
{
|
||||||
|
var feature = context.Features.Get<IRequestLoggingFeature>();
|
||||||
|
if (feature != null)
|
||||||
|
{
|
||||||
|
feature.Enable = false;
|
||||||
|
}
|
||||||
var flowRate = context.RequestServices.GetRequiredService<IFlowAnalyzer>().GetFlowRate();
|
var flowRate = context.RequestServices.GetRequiredService<IFlowAnalyzer>().GetFlowRate();
|
||||||
return context.Response.WriteAsJsonAsync(flowRate);
|
return context.Response.WriteAsJsonAsync(flowRate);
|
||||||
});
|
});
|
||||||
|
|||||||
@ -20,7 +20,7 @@
|
|||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body>
|
<body onContextMenu="return false">
|
||||||
<div id="chart"></div>
|
<div id="chart"></div>
|
||||||
|
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user