86 lines
2.9 KiB
C#
86 lines
2.9 KiB
C#
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());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|