This commit is contained in:
skidoodle 2024-03-13 00:33:46 +01:00
commit e124a47765
19374 changed files with 9806149 additions and 0 deletions

View file

@ -0,0 +1,86 @@
using System;
using System.Text;
using Kreta.Web.Logging.Logger;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Kreta.Web.Logger
{
/// <summary>
/// Logging field renderer
/// </summary>
class LoginRequestBodyRenderer : LoggingFieldRenderer
{
/// <summary>
/// Secret log value
/// </summary>
const string SecretLogValue = "***";
/// <summary>
/// Password
/// </summary>
const string Password = nameof(Password);
/// <summary>
/// User
/// </summary>
const string UserName = nameof(UserName);
/// <summary>
/// Render
/// </summary>
/// <param name="fieldName">Field</param>
/// <param name="context">Context</param>
public override void Render(string fieldName, LoggingFieldRendererContext context)
{
string contentBodyJsonText = context.GetFieldValue(fieldName) as string;
if (contentBodyJsonText != null)
{
try
{
JObject contentBody = JsonConvert.DeserializeObject<JObject>(contentBodyJsonText);
if (!string.IsNullOrWhiteSpace(contentBody.GetValue(Password, StringComparison.OrdinalIgnoreCase)?.ToString()))
{
contentBody[Password] = SecretLogValue;
}
string userLoginName = contentBody.GetValue(UserName, StringComparison.OrdinalIgnoreCase)?.ToString();
StringBuilder requestUserIdentifier = new StringBuilder();
if (userLoginName != null)
{
requestUserIdentifier.Append(userLoginName.ToUpper());
string requestUri = context.GetFieldValue(RequestResponseLoggingFields.Request.Uri) as string;
if (!string.IsNullOrWhiteSpace(requestUri))
{
requestUserIdentifier.Append('/');
try
{
Uri uri = new Uri(requestUri);
requestUserIdentifier.Append(uri.Authority.Split('.')[0]);
}
catch (Exception ex)
{
requestUserIdentifier.Append($"[{ex.Message}]");
}
}
context.SetFieldValue(RequestResponseLoggingFields.Request.User, requestUserIdentifier.ToString());
}
context.SetFieldValue(fieldName, contentBody.ToString());
}
catch (Exception ex)
{
context.SetFieldValue(fieldName, ex.ToString());
}
}
}
}
}