LogExtensions
Defines extension methods for ILog.
using Relativity.DataExchange.Helpers;
using Relativity.Logging;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Net;
using System.Reflection;
namespace Relativity.DataExchange.Logging
{
internal static class LogExtensions
{
private const string DataExchangeLogPrefix = "DataExchange";
private const string ImportLogPrefix = "Import";
[Obsolete("Please use ILog instance directly. In this method, it is impossible to hash sensitive data")]
public static void LogObjectAsDictionary(this ILog logger, string messageTemplate, object value)
{
logger.LogObjectAsDictionary(messageTemplate, value, null);
}
[Obsolete("Please use ILog instance directly. In this method, it is impossible to hash sensitive data")]
public static void LogObjectAsDictionary(this ILog logger, string messageTemplate, object value, Func<PropertyInfo, bool> filter)
{
logger.ThrowIfNull<ILog>("logger");
if (value != null)
try {
BindingFlags bindingAttr = BindingFlags.Instance | BindingFlags.Public;
Dictionary<string, string> dictionary = value.GetType().GetProperties(bindingAttr).Where(delegate(PropertyInfo info) {
if (filter != null)
return filter(info);
return true;
})
.ToList()
.ToDictionary((PropertyInfo info) => info.Name, (PropertyInfo info) => Convert.ToString(info.GetValue(value, null)));
logger.LogInformation(messageTemplate, new object[1] {
dictionary
});
} catch (Exception ex) {
if (ExceptionHelper.IsFatalException(ex))
throw;
logger.LogWarning(ex, "Failed to log {SettingType} as a dictionary.", new object[1] {
value.GetType()
});
}
}
public static void LogUserContextInformation(this ILog logger, string messageTemplate, ICredentials credentials)
{
NetworkCredential networkCredential = credentials as NetworkCredential;
if (networkCredential != null) {
if (networkCredential.UserName == "XxX_BearerTokenCredentials_XxX")
LogInformationForAuthToken(logger, messageTemplate, networkCredential);
else
logger.LogInformation(messageTemplate + " with User Name: {userName}", new object[1] {
HashingHelper.CalculateSHA256Hash(networkCredential.UserName)
});
}
}
[SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "It wraps LogContextPushProperty ILog method execution so the calling code is responsible for disposing the returned object.")]
public static IDisposable LogImportContextPushProperties(this ILog logger, object value)
{
logger.ThrowIfNull<ILog>("logger");
value.ThrowIfNull("value");
StackOfDisposables stackOfDisposables = new StackOfDisposables();
PropertyInfo[] properties = value.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
foreach (PropertyInfo propertyInfo in properties) {
string name = propertyInfo.Name;
string text = propertyInfo.GetValue(value, null)?.ToString() ?? string.Empty;
IDisposable disposable = logger.LogContextPushProperty("DataExchange.Import." + name, (object)text);
stackOfDisposables.Push(disposable);
}
return stackOfDisposables;
}
private static void LogInformationForAuthToken(ILog logger, string messageTemplate, NetworkCredential networkCredentials)
{
if (JwtTokenHelper.TryParse(networkCredentials.Password, out JwtAuthToken jwtAuthToken))
logger.LogInformation(messageTemplate + " with {@jwtAuthToken}", new object[1] {
jwtAuthToken
});
else
logger.LogWarning("Can't parse bearer token!", Array.Empty<object>());
}
}
}