Skip to content

Commit

Permalink
Layout.Render-method never returns null (#5138)
Browse files Browse the repository at this point in the history
  • Loading branch information
snakefoot authored Dec 30, 2022
1 parent b1175f1 commit 6219de4
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 8 deletions.
3 changes: 1 addition & 2 deletions src/NLog/Config/LoggingConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ public LoggingConfiguration()
public LoggingConfiguration(LogFactory logFactory)
{
LogFactory = logFactory ?? LogManager.LogFactory;
LoggingRules = new List<LoggingRule>();
_variables = new ConfigVariablesDictionary(this);
DefaultCultureInfo = LogFactory._defaultCultureInfo;
}
Expand Down Expand Up @@ -110,7 +109,7 @@ public LoggingConfiguration(LogFactory logFactory)
/// <summary>
/// Gets the collection of logging rules.
/// </summary>
public IList<LoggingRule> LoggingRules { get; private set; }
public IList<LoggingRule> LoggingRules { get; } = new List<LoggingRule>();

internal List<LoggingRule> GetLoggingRulesThreadSafe() { lock (LoggingRules) return LoggingRules.ToList(); }
private void AddLoggingRulesThreadSafe(LoggingRule rule) { lock (LoggingRules) LoggingRules.Add(rule); }
Expand Down
3 changes: 1 addition & 2 deletions src/NLog/Config/LoggingRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ public LoggingRule(string ruleName)
{
RuleName = ruleName;
Filters = new List<Filter>();
ChildRules = new List<LoggingRule>();
}

/// <summary>
Expand Down Expand Up @@ -125,7 +124,7 @@ public LoggingRule(string loggerNamePattern, Target target)
/// <summary>
/// Gets a collection of child rules to be evaluated when this rule matches.
/// </summary>
public IList<LoggingRule> ChildRules { get; }
public IList<LoggingRule> ChildRules { get; } = new List<LoggingRule>();

internal List<LoggingRule> GetChildRulesThreadSafe() { lock (ChildRules) return ChildRules.ToList(); }
internal Target[] GetTargetsThreadSafe() { lock (_targets) return _targets.Count == 0 ? NLog.Internal.ArrayHelper.Empty<Target>() : _targets.ToArray(); }
Expand Down
2 changes: 1 addition & 1 deletion src/NLog/Filters/WhenRepeatedFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ private FilterInfoKey RenderFilterInfoKey(LogEventInfo logEvent, StringBuilder t
targetBuilder.Length = MaxLength;
return new FilterInfoKey(targetBuilder, null);
}
string value = Layout.Render(logEvent) ?? string.Empty;
string value = Layout.Render(logEvent);
if (value.Length > MaxLength)
value = value.Substring(0, MaxLength);
return new FilterInfoKey(null, value);
Expand Down
6 changes: 3 additions & 3 deletions src/NLog/Layouts/Typed/ValueTypeLayoutInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public object RenderValue(LogEventInfo logEvent)
return rawValue;
}

var stringValue = layout.Render(logEvent) ?? string.Empty;
var stringValue = layout.Render(logEvent);
if (string.IsNullOrEmpty(stringValue))
{
return GetTypedDefaultValue();
Expand Down Expand Up @@ -257,12 +257,12 @@ private object CreateTypedDefaultValue()
return string.Empty;

if (_typedLayout is null)
return _defaultValue.Render(LogEventInfo.CreateNullEvent()) ?? string.Empty;
return _defaultValue.Render(LogEventInfo.CreateNullEvent());

if (_typedLayout.IsFixed)
return string.Empty;

var defaultStringValue = _defaultValue.Render(LogEventInfo.CreateNullEvent()) ?? string.Empty;
var defaultStringValue = _defaultValue.Render(LogEventInfo.CreateNullEvent());
if (string.IsNullOrEmpty(defaultStringValue))
return string.Empty;

Expand Down

0 comments on commit 6219de4

Please sign in to comment.