/// <summary> /// Exclude sensitive logs parameters from being logged into (long-term) files /// </summary> /// <param name="config">The logger config to apply the filter to.</param> /// <param name="applyDecider">Custom condition to decide whether to apply the sensitive log file to a log target.</param> private static void AddSensitiveFilter(LoggingConfiguration config, Func <Target, bool> applyDecider) { WhenMethodFilter sensitiveLogFilter = new WhenMethodFilter(context => { context.Properties.TryGetValue("sensitive", out object isSensitive); if (isSensitive != null && (bool)isSensitive) { for (int i = 0; i < context.MessageTemplateParameters.Count; i++) { MessageTemplateParameter template = context.MessageTemplateParameters[i]; if (sensitiveLogParameters.Contains(template.Name)) { context.Parameters.SetValue(new string('*', template.Value?.ToString().Length ?? 0), i); } } context.Parameters = context.Parameters; // Triggers NLog to format the message again } return(FilterResult.Log); }); foreach (LoggingRule rule in config.LoggingRules) { foreach (Target target in rule.Targets) { if (applyDecider(target)) { rule.Filters.Add(sensitiveLogFilter); } } } }
private static void AssertParameter(MessageTemplateParameter item, string expectedName, string expectedValue, CaptureType captureType) { Assert.Equal(expectedName, item.Name); Assert.Null(item.Format); Assert.Null(item.PositionalIndex); Assert.Equal(expectedValue, item.Value); Assert.Equal(captureType, item.CaptureType); }
public void IndexOf_ThrowsNotSupported() { // Arrange MessageTemplateParameter item = new MessageTemplateParameter(); // Act & Assert Assert.Throws <NotSupportedException>(() => _messageParameterList.IndexOf(item)); }
[InlineData("8a", null)] //condition branch 2b public void PositionalIndexTest(string name, int?expected) { // Arrange var parameter = new MessageTemplateParameter(name, null, null); // Act var positionalIndex = parameter.PositionalIndex; // Assert Assert.Equal(expected, positionalIndex); }
/// <summary> /// Injects the list of message-template-parameter into the IDictionary-interface /// </summary> /// <param name="parameterList">Message-template-parameters</param> public PropertiesDictionary(IList <MessageTemplateParameter> parameterList = null) { if (parameterList != null && parameterList.Count > 0) { var messageProperties = new MessageTemplateParameter[parameterList.Count]; for (int i = 0; i < parameterList.Count; ++i) { messageProperties[i] = parameterList[i]; } MessageProperties = messageProperties; } }
public void CopyTo_WithOffset_AllCopied() { // Arrange MessageTemplateParameter[] array = new MessageTemplateParameter[3]; int arrayIndex = 1; // Act _messageParameterList.CopyTo(array, arrayIndex); // Assert AssertParameters(array, 1); }
public void CopyTo_FullCopy_AllCopied() { // Arrange MessageTemplateParameter[] array = new MessageTemplateParameter[2]; int arrayIndex = 0; // Act _messageParameterList.CopyTo(array, arrayIndex); // Assert AssertParameters(array); }
/// <summary> /// Constructore for positional parameters /// </summary> public MessageTemplateParameters(object[] parameters) { if (parameters != null && parameters.Length > 0) { _parameters = new MessageTemplateParameter[parameters.Length]; for (int i = 0; i < parameters.Length; ++i) { string parameterName; switch (i) { //prevent creating a string (int.ToString()) case 0: parameterName = "0"; break; case 1: parameterName = "1"; break; case 2: parameterName = "2"; break; case 3: parameterName = "3"; break; case 4: parameterName = "4"; break; case 5: parameterName = "5"; break; case 6: parameterName = "6"; break; case 7: parameterName = "7"; break; case 8: parameterName = "8"; break; case 9: parameterName = "9"; break; default: parameterName = i.ToString(); break; } _parameters[i] = new MessageTemplateParameter(parameterName, parameters[i], null); } } else { _parameters = Internal.ArrayHelper.Empty <MessageTemplateParameter>(); } }
/// <summary> /// Add Property and init list if needed /// </summary> /// <param name="extraProperties"></param> /// <param name="item"></param> /// <returns>list with at least one item</returns> private static List <KeyValuePair <string, object> > AddExtraProperty(List <KeyValuePair <string, object> > extraProperties, MessageTemplateParameter item) { extraProperties = extraProperties ?? new List <KeyValuePair <string, object> >(); extraProperties.Add(new KeyValuePair <string, object>(item.Name, item.Value)); return(extraProperties); }
/// <summary> /// Add Property and init list if needed /// </summary> /// <param name="extraProperties"></param> /// <param name="item"></param> /// <returns>list with at least one item</returns> private static List <MessageTemplateParameter> AddExtraProperty(List <MessageTemplateParameter> extraProperties, MessageTemplateParameter item) { extraProperties = extraProperties ?? new List <MessageTemplateParameter>(); extraProperties.Add(item); return(extraProperties); }
/// <summary> /// Render a template to a string. /// </summary> /// <param name="template">The template.</param> /// <param name="formatProvider">Culture.</param> /// <param name="parameters">Parameters for the holes.</param> /// <param name="sb">The String Builder destination.</param> /// <param name="messageTemplateParameters">Parameters for the holes.</param> private void Render(string template, IFormatProvider formatProvider, object[] parameters, StringBuilder sb, out IList <MessageTemplateParameter> messageTemplateParameters) { int pos = 0; int holeIndex = 0; int holeStartPosition = 0; messageTemplateParameters = null; int originalLength = sb.Length; TemplateEnumerator templateEnumerator = new TemplateEnumerator(template); while (templateEnumerator.MoveNext()) { if (holeIndex == 0 && !_forceTemplateRenderer && templateEnumerator.Current.MaybePositionalTemplate && sb.Length == originalLength) { // Not a structured template sb.AppendFormat(formatProvider, template, parameters); return; } var literal = templateEnumerator.Current.Literal; sb.Append(template, pos, literal.Print); pos += literal.Print; if (literal.Skip == 0) { pos++; } else { pos += literal.Skip; var hole = templateEnumerator.Current.Hole; if (hole.Alignment != 0) { holeStartPosition = sb.Length; } if (hole.Index != -1 && messageTemplateParameters == null) { holeIndex++; RenderHole(sb, hole, formatProvider, parameters[hole.Index], true); } else { var holeParameter = parameters[holeIndex]; if (messageTemplateParameters == null) { messageTemplateParameters = new MessageTemplateParameter[parameters.Length]; if (holeIndex != 0) { // rewind and try again templateEnumerator = new TemplateEnumerator(template); sb.Length = originalLength; holeIndex = 0; pos = 0; continue; } } messageTemplateParameters[holeIndex++] = new MessageTemplateParameter(hole.Name, holeParameter, hole.Format, hole.CaptureType); RenderHole(sb, hole, formatProvider, holeParameter); } if (hole.Alignment != 0) { RenderPadding(sb, hole.Alignment, holeStartPosition); } } } if (messageTemplateParameters != null && holeIndex != messageTemplateParameters.Count) { var truncateParameters = new MessageTemplateParameter[holeIndex]; for (int i = 0; i < truncateParameters.Length; ++i) { truncateParameters[i] = messageTemplateParameters[i]; } messageTemplateParameters = truncateParameters; } }