Added Viewer and
change to App Setting File from Constants
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using OI.Metrology.Archive.Models;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
@ -12,13 +12,13 @@ namespace OI.Metrology.Archive;
|
||||
// You may need to install the Microsoft.AspNetCore.Http.Abstractions package into your project
|
||||
public class ApiLoggingMiddleware
|
||||
{
|
||||
private IConfiguration Config { get; }
|
||||
private readonly RequestDelegate _Next;
|
||||
private readonly AppSettings _AppSettings;
|
||||
|
||||
public ApiLoggingMiddleware(RequestDelegate next, IConfiguration config)
|
||||
public ApiLoggingMiddleware(RequestDelegate next, AppSettings appSettings)
|
||||
{
|
||||
Config = config;
|
||||
_Next = next;
|
||||
_AppSettings = appSettings;
|
||||
}
|
||||
|
||||
// this is the method called in ASP.NET Core middleware to handle an HTTP request
|
||||
@ -29,25 +29,22 @@ public class ApiLoggingMiddleware
|
||||
try
|
||||
{
|
||||
bool doLogging = false;
|
||||
string pathsToLog = Config[Constants.ApiLoggingPathPrefixes];
|
||||
string contentTypesToLog = Config[Constants.ApiLoggingContentTypes];
|
||||
|
||||
// check to see if this is a request path that is enabled for logging
|
||||
if (!string.IsNullOrWhiteSpace(pathsToLog))
|
||||
if (!string.IsNullOrWhiteSpace(_AppSettings.ApiLoggingPathPrefixes))
|
||||
{
|
||||
// check if the request path begins with any part of pathsToLog
|
||||
if (pathsToLog.Split(';').Any(p => httpContext.Request.Path.StartsWithSegments(new PathString(p))))
|
||||
if (_AppSettings.ApiLoggingPathPrefixes.Split(';').Any(p => httpContext.Request.Path.StartsWithSegments(new PathString(p))))
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(contentTypesToLog))
|
||||
{
|
||||
// if there are content type filters configured, only log is the request begins with one of them
|
||||
doLogging = contentTypesToLog.Split(';').Any(ct => httpContext.Request.ContentType.StartsWith(ct));
|
||||
}
|
||||
else
|
||||
if (string.IsNullOrWhiteSpace(_AppSettings.ApiLoggingContentTypes))
|
||||
{
|
||||
// if no content type filter is defined, log all content types
|
||||
doLogging = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
// if there are content type filters configured, only log is the request begins with one of them
|
||||
doLogging = _AppSettings.ApiLoggingContentTypes.Split(';').Any(ct => httpContext.Request.ContentType.StartsWith(ct));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -107,15 +104,14 @@ public class ApiLoggingMiddleware
|
||||
{
|
||||
int threadId = Environment.CurrentManagedThreadId;
|
||||
|
||||
string logPath = Config[Constants.ApiLogPath];
|
||||
string fileName = $"ApiLog{requestTime:yyyyMMdd_hhmmssttt}_{threadId}.txt";
|
||||
|
||||
HttpContext context = request.HttpContext;
|
||||
|
||||
if (!Directory.Exists(logPath))
|
||||
_ = Directory.CreateDirectory(logPath);
|
||||
if (!Directory.Exists(_AppSettings.ApiLogPath))
|
||||
_ = Directory.CreateDirectory(_AppSettings.ApiLogPath);
|
||||
|
||||
using StreamWriter sw = new(Path.Join(logPath, fileName), true);
|
||||
using StreamWriter sw = new(Path.Join(_AppSettings.ApiLogPath, fileName), true);
|
||||
sw.WriteLine($"Request at {requestTime:yyyy/MM/dd hh:mm:ss.ttt} from {context.Connection.RemoteIpAddress}");
|
||||
sw.WriteLine($"{request.Method} {request.Path} {request.QueryString}");
|
||||
sw.WriteLine("Request body:");
|
||||
|
Reference in New Issue
Block a user