1.1) حذف XRequestLogger Middleware

- حذف: Modules/xCommons/Middlewares/XRequestLogger.cs (144 خط)
- دلیل: منتقل شده به xLoggingService با بهبوری

1.2) تغییر Extensions
- فایل: XMiddlewareExtensions.cs
- تغییر: حذف متد UseXRequestLogger()
- نتیجه: کلاس اکنون خالی است

1.3) بهبود XLoggerModel
- اضافه: Exception property = null
- تنظیم: ترتیب Properties
This commit is contained in:
2026-11-09 12:49:36 +03:30
parent b9ba57dbfa
commit 0a8f9692dd
3 changed files with 5 additions and 159 deletions
+1 -12
View File
@@ -1,16 +1,5 @@
using xCommons.Middlewares;
using Microsoft.AspNetCore.Builder;
namespace xCommons.Extensions
{
public static class XMiddlewareExtensions
{
/// <summary>
/// Use XRequestLogger Middleware for log Requests ...
/// </summary>
/// <param name="app"></param>
public static void UseXRequestLogger(this IApplicationBuilder app) {
app.UseMiddleware<XRequestLogger>();
}
}
{ }
}
-144
View File
@@ -1,144 +0,0 @@
using System;
using System.IO;
using xCommons.Models;
using xCommons.Extensions;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
namespace xCommons.Middlewares {
public partial class XRequestLogger {
private readonly ILogger logger;
private readonly RequestDelegate next;
public XRequestLogger (
RequestDelegate next,
ILoggerFactory loggerFactory
) {
//
this.next = next;
this.logger = loggerFactory
.CreateLogger<XRequestLogger> ();
}
public async Task InvokeAsync (HttpContext context) {
//
var logModel = new XLoggerModel ();
//
logModel.Path = context.Request.Path;
logModel.Schema = context.Request.Scheme;
logModel.Method = context.Request.Method;
logModel.Headers = context.Request.Headers;
logModel.Host = context.Request.Host.ToString ();
logModel.QueryString = context.Request.QueryString.ToString ();
logModel.UserID = context.User.Identity.IsAuthenticated ? context.User.Identity.Name : "Not Authenticated";
//
logModel.Connection = new XLoggerHttpConnection {
Id = context.Connection.Id,
RemoteIpAddress = context.Connection.RemoteIpAddress.ToString (),
RemotePort = context.Connection.RemotePort.ToString (),
LocalIpAddress = context.Connection.LocalIpAddress.ToString (),
LocalPort = context.Connection.LocalPort.ToString (),
};
//
// Check if a request is a Post Call so read data from Body ...
if (context.Request.Method.ToNormalString () == "post") {
//
context.Request.EnableBuffering ();
var request = await new StreamReader (context.Request.Body)
.ReadToEndAsync ();
//
context.Request.Body.Position = 0;
logModel.Request = request;
}
//
logModel.RequestedOn = DateTime.UtcNow;
//
// Do Request ...
await next.Invoke (context);
//
using (Stream originalRequest = context.Response.Body) {
try {
using (var memStream = new MemoryStream ()) {
//
context.Response.Body = memStream;
//
// All the Request processing as described above
// happens from here.
// Response handling starts from here
// set the pointer to the beginning of the
// memory stream to read
memStream.Position = 0;
//
// read the memory stream till the end
var response = await new StreamReader (memStream)
.ReadToEndAsync ();
//
logModel.Response = response;
logModel.RespondedOn = DateTime.UtcNow;
logModel.StatusCode = context.Response.StatusCode;
logModel.IsSucceed = context.Response.StatusCode >= 200 && context.Response.StatusCode <= 299;
//
// since we have read till the end of the stream,
// reset it onto the first position
memStream.Position = 0;
//
// now copy the content of the temporary memory
// stream we have passed to the actual response body
// which will carry the response out.
await memStream.CopyToAsync (originalRequest);
}
} catch (Exception ex) {
//
// Log Exception ...
logger.LogError ($"Error on Logging Request/Response ... {ex.ToJSON()}");
} finally {
//
// assign the response body to the actual context
context.Response.Body = originalRequest;
}
}
//
// Log Response ...
logger.LogCritical ($"Http Response/Response Information:{Environment.NewLine}" +
$"Schema:{logModel.Schema} " + Environment.NewLine +
$"Host: {logModel.Host} " + Environment.NewLine +
$"Path: {logModel.Path} " + Environment.NewLine +
$"Method: {logModel.Method} " + Environment.NewLine +
$"UserID: {logModel.UserID} " + Environment.NewLine +
$"QueryString: {context.Request.QueryString} " + Environment.NewLine +
$"Headers: {logModel.Headers.ToJSON()}" + Environment.NewLine +
$"" + Environment.NewLine +
$"Http Connection: " + Environment.NewLine +
$"Id: {logModel.Connection.Id}" + Environment.NewLine +
$"RemoteIpAddress: {logModel.Connection.RemoteIpAddress}" + Environment.NewLine +
$"RemotePort: {logModel.Connection.RemotePort}" + Environment.NewLine +
$"LocalIpAddress: {logModel.Connection.LocalIpAddress}" + Environment.NewLine +
$"LocalPort: {logModel.Connection.LocalPort}" + Environment.NewLine +
$"" + Environment.NewLine +
$"Request Body: {logModel.Request}" + Environment.NewLine +
$"RequestedOn: {logModel.RequestedOn}" + Environment.NewLine +
$"" + Environment.NewLine +
$"IsSucceed: {logModel.IsSucceed}" + Environment.NewLine +
$"StatucCode: {logModel.StatusCode}" + Environment.NewLine +
$"Response Body: {logModel.Response}" + Environment.NewLine +
$"RespondedOn: {logModel.RespondedOn}" + Environment.NewLine +
$"" + Environment.NewLine
);
}
}
}
+4 -3
View File
@@ -3,19 +3,20 @@ using Microsoft.AspNetCore.Http;
namespace xCommons.Models {
public class XLoggerModel {
public string Schema { get; set; }
public string Host { get; set; }
public string Path { get; set; }
public string Schema { get; set; }
public string Method { get; set; }
public string UserID { get; set; }
public string QueryString { get; set; }
public IHeaderDictionary Headers { get; set; }
public string Request { get; set; }
public int StatusCode { get; set; }
public bool IsSucceed { get; set; }
public string Response { get; set; }
public string QueryString { get; set; }
public DateTime RequestedOn { get; set; }
public DateTime RespondedOn { get; set; }
public IHeaderDictionary Headers { get; set; }
public Exception Exception { get; set; } = null;
public XLoggerHttpConnection Connection { get; set; }
}