using System; using System.Text; using Kreta.Web.Logging.Logger; using Newtonsoft.Json; using Newtonsoft.Json.Linq; namespace Kreta.Web.Logger { /// /// Logging field renderer /// class LoginRequestBodyRenderer : LoggingFieldRenderer { /// /// Secret log value /// const string SecretLogValue = "***"; /// /// Password /// const string Password = nameof(Password); /// /// User /// const string UserName = nameof(UserName); /// /// Render /// /// Field /// Context public override void Render(string fieldName, LoggingFieldRendererContext context) { string contentBodyJsonText = context.GetFieldValue(fieldName) as string; if (contentBodyJsonText != null) { try { JObject contentBody = JsonConvert.DeserializeObject(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()); } } } } }