ASP.NET Core Web APIでロギング目的でHTTPリクエストを取得するのが難しいです。私は助けているここでは例ASP.Net Core Web APIロギング用HTTPリクエストの取得
http://dotnetliberty.com/index.php/2016/01/07/logging-asp-net-5-requests-using-middleware/
を見つけることができました。基本的には、ミドルウェア機能を使用してHTTPリクエストパイプラインに追加されるロギングクラスです。問題は、クラスメソッドがアプリケーションの起動時にのみ呼び出されることです。私はhttpリクエストを取得したり投稿したりするときに呼び出すことができません。私がデバッグすることができ、コンソールアプリケーション内でFiddlerとhttpクライアントを使って試した応答がOKであるため、httpリクエストの取得またはポストが機能しています。私が使用してHTTP要求にピギーバックところ、最後の行である。ここ
は私のロギングクラスは
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
namespace CoreWebAPI.Models
{
public class RequestLoggingMiddleware
{
private readonly RequestDelegate _next;
private readonly ILogger<RequestLoggingMiddleware> _logger;
public RequestLoggingMiddleware(RequestDelegate next, ILogger<RequestLoggingMiddleware> logger)
{
_next = next;
_logger = logger;
}
public async Task Invoke(HttpContext context)
{
var startTime = DateTime.UtcNow;
var watch = Stopwatch.StartNew();
await _next.Invoke(context);
watch.Stop();
var logTemplate = @"
Client IP: {clientIP}
Request path: {requestPath}
Request content type: {requestContentType}
Request content length: {requestContentLength}
Start time: {startTime}
Duration: {duration}";
_logger.LogInformation(logTemplate,
context.Connection.RemoteIpAddress.ToString(),
context.Request.Path,
context.Request.ContentType,
context.Request.ContentLength,
startTime,
watch.ElapsedMilliseconds);
}
}
}
されており、ここで私のStartup.Configure方法は、私のStartup.csクラスで
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseApplicationInsightsRequestTelemetry();
app.UseApplicationInsightsExceptionTelemetry();
app.UseMvc();
app.UseCors(builder => builder.AllowAnyOrigin());
app.UseMiddleware<RequestLoggingMiddleware>();
}
ですapp.UseMiddleware。私はこれがうまくいくはずだと思うが、なぜ誰かがそれがなぜそうでないか、あるいはそれが偉大になる代わりのものであるかという手がかりを持っている。追加情報を提供する必要がある場合はお知らせください。
の初めにミドルウェアを追加する必要がスタートアップで
-for-asp-net-core / – IbrarMumtaz