response elapsed time logging
This commit is contained in:
parent
a4c2ae3e28
commit
448161239e
@ -2,7 +2,7 @@
|
|||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<!--package info-->
|
<!--package info-->
|
||||||
<PackageId>DTLib.Web</PackageId>
|
<PackageId>DTLib.Web</PackageId>
|
||||||
<Version>1.2.1</Version>
|
<Version>1.2.2</Version>
|
||||||
<Authors>Timerix</Authors>
|
<Authors>Timerix</Authors>
|
||||||
<Description>HTTP Server with simple routing</Description>
|
<Description>HTTP Server with simple routing</Description>
|
||||||
<RepositoryType>GIT</RepositoryType>
|
<RepositoryType>GIT</RepositoryType>
|
||||||
|
|||||||
@ -6,6 +6,7 @@ global using System.Threading.Tasks;
|
|||||||
global using DTLib.Filesystem;
|
global using DTLib.Filesystem;
|
||||||
global using DTLib.Logging;
|
global using DTLib.Logging;
|
||||||
global using System.Net;
|
global using System.Net;
|
||||||
|
using System.Diagnostics;
|
||||||
using DTLib.Extensions;
|
using DTLib.Extensions;
|
||||||
using DTLib.Web.Routes;
|
using DTLib.Web.Routes;
|
||||||
|
|
||||||
@ -28,20 +29,24 @@ public class WebApp
|
|||||||
|
|
||||||
public async Task Run()
|
public async Task Run()
|
||||||
{
|
{
|
||||||
_logger.LogInfo($"starting webserver at {_baseUrl} ...");
|
_logger.LogInfo($"starting server at '{_baseUrl}'...");
|
||||||
HttpListener server = new HttpListener();
|
HttpListener server = new HttpListener();
|
||||||
server.Prefixes.Add(_baseUrl);
|
server.Prefixes.Add(_baseUrl);
|
||||||
server.Start();
|
server.Start();
|
||||||
_logger.LogInfo("server started");
|
_logger.LogInfo("server started");
|
||||||
long requestId = 1;
|
long requestId = 1;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
while (!_stopToken.IsCancellationRequested)
|
while (!_stopToken.IsCancellationRequested)
|
||||||
{
|
{
|
||||||
var ctx = await server.GetContextAsync().AsCancellable(_stopToken);
|
var ctx = await server.GetContextAsync().AsCancellable(_stopToken);
|
||||||
HandleRequestAsync(ctx, requestId);
|
HandleRequestAsync(ctx, requestId++);
|
||||||
requestId++;
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
catch (OperationCanceledException)
|
||||||
|
{}
|
||||||
|
|
||||||
// stop
|
|
||||||
server.Stop();
|
server.Stop();
|
||||||
_logger.LogInfo("server stopped");
|
_logger.LogInfo("server stopped");
|
||||||
}
|
}
|
||||||
@ -49,12 +54,15 @@ public class WebApp
|
|||||||
// ReSharper disable once AsyncVoidMethod
|
// ReSharper disable once AsyncVoidMethod
|
||||||
private async void HandleRequestAsync(HttpListenerContext ctx, long requestId)
|
private async void HandleRequestAsync(HttpListenerContext ctx, long requestId)
|
||||||
{
|
{
|
||||||
string logContext = $"Request {requestId}";
|
string logContext = $"Request-{requestId}";
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_logger.LogInfo(logContext, $"[{ctx.Request.HttpMethod}] {ctx.Request.RawUrl} from {ctx.Request.RemoteEndPoint}...");
|
_logger.LogInfo(logContext, $"[{ctx.Request.HttpMethod}] {ctx.Request.RawUrl} from {ctx.Request.RemoteEndPoint}...");
|
||||||
|
var stopwatch = new Stopwatch();
|
||||||
|
stopwatch.Start();
|
||||||
var status = await _router.Resolve(ctx);
|
var status = await _router.Resolve(ctx);
|
||||||
_logger.LogInfo(logContext, $"{(int)status} ({status})");
|
stopwatch.Stop();
|
||||||
|
_logger.LogInfo(logContext, $"responded {(int)status} ({status}) in {stopwatch.ElapsedMilliseconds}ms");
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user