using System; using System.Collections.Generic; using System.Text; namespace WmsLite.Api.Infrastructure.HttpLogging; internal static class HttpAccessLogFormatter { private const string Bar = "────────────────────────────────────────"; private const string Thick = "════════════════════════════════════════"; public static string Build( string method, string? path, int statusCode, string requestContent, string responseContent, IReadOnlyList sqlStatements) { var sb = new StringBuilder(512 + requestContent.Length + responseContent.Length); sb.AppendLine(Thick); sb.AppendLine(" HTTP ACCESS"); sb.AppendLine(Thick); sb.Append("Time : ").AppendLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")); sb.Append("Method : ").AppendLine(method); sb.Append("Path : ").AppendLine(path ?? string.Empty); sb.Append("Status : ").AppendLine(statusCode.ToString()); sb.AppendLine(Bar); sb.AppendLine("Request"); sb.AppendLine(Bar); sb.AppendLine(string.IsNullOrEmpty(requestContent) ? "(empty)" : requestContent); sb.AppendLine(); sb.AppendLine(Bar); sb.AppendLine("Response"); sb.AppendLine(Bar); sb.AppendLine(string.IsNullOrEmpty(responseContent) ? "(empty)" : responseContent); sb.AppendLine(); sb.AppendLine(Bar); sb.Append("SQL (").Append(sqlStatements.Count).AppendLine(")"); sb.AppendLine(Bar); if (sqlStatements.Count == 0) { sb.AppendLine("(no database commands)"); } else { for (var i = 0; i < sqlStatements.Count; i++) { sb.Append('[').Append(i + 1).AppendLine("]"); sb.AppendLine(sqlStatements[i]); if (i < sqlStatements.Count - 1) { sb.AppendLine(); } } } sb.AppendLine(Thick); return sb.ToString(); } }