Skip to content

Commit

Permalink
Replaced private setter with property initializer (#5139)
Browse files Browse the repository at this point in the history
  • Loading branch information
snakefoot authored Dec 31, 2022
1 parent 6219de4 commit 9476449
Show file tree
Hide file tree
Showing 13 changed files with 22 additions and 51 deletions.
10 changes: 1 addition & 9 deletions src/NLog.Database/DatabaseCommandInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,6 @@ namespace NLog.Targets
[NLogConfigurationItem]
public class DatabaseCommandInfo
{
/// <summary>
/// Initializes a new instance of the <see cref="DatabaseCommandInfo"/> class.
/// </summary>
public DatabaseCommandInfo()
{
Parameters = new List<DatabaseParameterInfo>();
}

/// <summary>
/// Gets or sets the type of the command.
/// </summary>
Expand Down Expand Up @@ -85,6 +77,6 @@ public DatabaseCommandInfo()
/// </summary>
/// <docgen category='Command Options' order='10' />
[ArrayParameter(typeof(DatabaseParameterInfo), "parameter")]
public IList<DatabaseParameterInfo> Parameters { get; private set; }
public IList<DatabaseParameterInfo> Parameters { get; } = new List<DatabaseParameterInfo>();
}
}
6 changes: 2 additions & 4 deletions src/NLog.Database/DatabaseTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,6 @@ public class DatabaseTarget : Target, IInstallable
/// </summary>
public DatabaseTarget()
{
InstallDdlCommands = new List<DatabaseCommandInfo>();
UninstallDdlCommands = new List<DatabaseCommandInfo>();
DBProvider = "sqlserver";
DBHost = ".";
#if !NETSTANDARD
Expand Down Expand Up @@ -168,14 +166,14 @@ public DatabaseTarget(string name) : this()
/// </summary>
/// <docgen category='Installation Options' order='100' />
[ArrayParameter(typeof(DatabaseCommandInfo), "install-command")]
public IList<DatabaseCommandInfo> InstallDdlCommands { get; private set; }
public IList<DatabaseCommandInfo> InstallDdlCommands { get; } = new List<DatabaseCommandInfo>();

/// <summary>
/// Gets the uninstallation DDL commands.
/// </summary>
/// <docgen category='Installation Options' order='100' />
[ArrayParameter(typeof(DatabaseCommandInfo), "uninstall-command")]
public IList<DatabaseCommandInfo> UninstallDdlCommands { get; private set; }
public IList<DatabaseCommandInfo> UninstallDdlCommands { get; } = new List<DatabaseCommandInfo>();

/// <summary>
/// Gets or sets a value indicating whether to keep the
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 @@ -67,7 +67,6 @@ public LoggingRule()
public LoggingRule(string ruleName)
{
RuleName = ruleName;
Filters = new List<Filter>();
}

/// <summary>
Expand Down Expand Up @@ -133,7 +132,7 @@ public LoggingRule(string loggerNamePattern, Target target)
/// <summary>
/// Gets a collection of filters to be checked before writing to targets.
/// </summary>
public IList<Filter> Filters { get; }
public IList<Filter> Filters { get; } = new List<Filter>();

/// <summary>
/// Gets or sets a value indicating whether to quit processing any following rules when this one matches.
Expand Down
3 changes: 1 addition & 2 deletions src/NLog/Layouts/CSV/CsvLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ public class CsvLayout : LayoutWithHeaderAndFooter
/// </summary>
public CsvLayout()
{
Columns = new List<CsvColumn>();
Layout = this;
Header = new CsvHeaderLayout(this);
Footer = null;
Expand All @@ -74,7 +73,7 @@ public CsvLayout()
/// </summary>
/// <docgen category='Layout Options' order='10' />
[ArrayParameter(typeof(CsvColumn), "column")]
public IList<CsvColumn> Columns { get; private set; }
public IList<CsvColumn> Columns { get; } = new List<CsvColumn>();

/// <summary>
/// Gets or sets a value indicating whether CVS should include header.
Expand Down
3 changes: 1 addition & 2 deletions src/NLog/Layouts/JSON/JsonLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ public bool SerializeObjectNoLimit(object value, StringBuilder builder)
/// </summary>
public JsonLayout()
{
Attributes = new List<JsonAttribute>();
ExcludeProperties = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
}

Expand All @@ -104,7 +103,7 @@ public JsonLayout()
/// </summary>
/// <docgen category='Layout Options' order='10' />
[ArrayParameter(typeof(JsonAttribute), "attribute")]
public IList<JsonAttribute> Attributes { get; private set; }
public IList<JsonAttribute> Attributes { get; } = new List<JsonAttribute>();

/// <summary>
/// Gets or sets the option to suppress the extra spaces in the output json
Expand Down
6 changes: 2 additions & 4 deletions src/NLog/Layouts/XML/XmlElementBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ protected XmlElementBase(string elementName, Layout elementValue)
{
ElementNameInternal = elementName;
LayoutWrapper.Inner = elementValue;
Attributes = new List<XmlAttribute>();
Elements = new List<XmlElement>();
ExcludeProperties = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
}

Expand Down Expand Up @@ -88,14 +86,14 @@ protected XmlElementBase(string elementName, Layout elementValue)
/// </summary>
/// <docgen category='Layout Options' order='10' />
[ArrayParameter(typeof(XmlElement), "element")]
public IList<XmlElement> Elements { get; private set; }
public IList<XmlElement> Elements { get; } = new List<XmlElement>();

/// <summary>
/// Gets the array of 'attributes' configurations for the element
/// </summary>
/// <docgen category='Layout Options' order='10' />
[ArrayParameter(typeof(XmlAttribute), "attribute")]
public IList<XmlAttribute> Attributes { get; private set; }
public IList<XmlAttribute> Attributes { get; } = new List<XmlAttribute>();

/// <summary>
/// Gets or sets whether a ElementValue with empty value should be included in the output
Expand Down
6 changes: 2 additions & 4 deletions src/NLog/Targets/ColoredConsoleTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,6 @@ public sealed class ColoredConsoleTarget : TargetWithLayoutHeaderAndFooter
/// </remarks>
public ColoredConsoleTarget()
{
WordHighlightingRules = new List<ConsoleWordHighlightingRule>();
RowHighlightingRules = new List<ConsoleRowHighlightingRule>();
_consolePrinter = CreateConsolePrinter(EnableAnsiOutput);
}

Expand Down Expand Up @@ -221,14 +219,14 @@ public Encoding Encoding
/// </summary>
/// <docgen category='Highlighting Rules' order='10' />
[ArrayParameter(typeof(ConsoleRowHighlightingRule), "highlight-row")]
public IList<ConsoleRowHighlightingRule> RowHighlightingRules { get; private set; }
public IList<ConsoleRowHighlightingRule> RowHighlightingRules { get; } = new List<ConsoleRowHighlightingRule>();

/// <summary>
/// Gets the word highlighting rules.
/// </summary>
/// <docgen category='Highlighting Rules' order='11' />
[ArrayParameter(typeof(ConsoleWordHighlightingRule), "highlight-word")]
public IList<ConsoleWordHighlightingRule> WordHighlightingRules { get; private set; }
public IList<ConsoleWordHighlightingRule> WordHighlightingRules { get; } = new List<ConsoleWordHighlightingRule>();

/// <inheritdoc/>
protected override void InitializeTarget()
Expand Down
10 changes: 1 addition & 9 deletions src/NLog/Targets/ConsoleRowHighlightingRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,6 @@ namespace NLog.Targets
[NLogConfigurationItem]
public class ConsoleRowHighlightingRule
{
/// <summary>
/// Initializes static members of the ConsoleRowHighlightingRule class.
/// </summary>
static ConsoleRowHighlightingRule()
{
Default = new ConsoleRowHighlightingRule(null, ConsoleOutputColor.NoChange, ConsoleOutputColor.NoChange);
}

/// <summary>
/// Initializes a new instance of the <see cref="ConsoleRowHighlightingRule" /> class.
/// </summary>
Expand All @@ -74,7 +66,7 @@ public ConsoleRowHighlightingRule(ConditionExpression condition, ConsoleOutputCo
/// <summary>
/// Gets the default highlighting rule. Doesn't change the color.
/// </summary>
public static ConsoleRowHighlightingRule Default { get; private set; }
public static ConsoleRowHighlightingRule Default { get; } = new ConsoleRowHighlightingRule(null, ConsoleOutputColor.NoChange, ConsoleOutputColor.NoChange);

/// <summary>
/// Gets or sets the condition that must be met in order to set the specified foreground and background color.
Expand Down
6 changes: 3 additions & 3 deletions src/NLog/Targets/DateAndSequenceArchive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,17 @@ internal class DateAndSequenceArchive
/// <summary>
/// The full name of the archive file.
/// </summary>
public string FileName { get; private set; }
public string FileName { get; }

/// <summary>
/// The parsed date contained in the file name.
/// </summary>
public DateTime Date { get; private set; }
public DateTime Date { get; }

/// <summary>
/// The parsed sequence number contained in the file name.
/// </summary>
public int Sequence { get; private set; }
public int Sequence { get; }

/// <summary>
/// Determines whether <paramref name="date"/> produces the same string as the current instance's date once formatted with the current instance's date format.
Expand Down
9 changes: 4 additions & 5 deletions src/NLog/Targets/FileArchiveModes/FileArchiveModeBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,30 +220,29 @@ internal sealed class FileNameTemplate
/// File name which is used as template for matching and replacements.
/// It is expected to contain a pattern to match.
/// </summary>
public string Template { get; private set; }
public string Template { get; }

/// <summary>
/// The beginning position of the <see cref="FileNameTemplate.PatternStartCharacters"/>
/// within the <see cref="FileNameTemplate.Template"/>. -1 is returned
/// when no pattern can be found.
/// </summary>
public int BeginAt { get; private set; }
public int BeginAt { get; }

/// <summary>
/// The ending position of the <see cref="FileNameTemplate.PatternEndCharacters"/>
/// within the <see cref="FileNameTemplate.Template"/>. -1 is returned
/// when no pattern can be found.
/// </summary>
public int EndAt { get; private set; }
public int EndAt { get; }

private bool FoundPattern => BeginAt != -1 && EndAt != -1;

public FileNameTemplate(string template)
{
Template = template;
BeginAt = template.IndexOf(PatternStartCharacters, StringComparison.Ordinal);
if (BeginAt != -1)
EndAt = template.IndexOf(PatternEndCharacters, StringComparison.Ordinal) + PatternEndCharacters.Length;
EndAt = BeginAt != -1 ? (template.IndexOf(PatternEndCharacters, StringComparison.Ordinal) + PatternEndCharacters.Length) : -1;
}

/// <summary>
Expand Down
6 changes: 2 additions & 4 deletions src/NLog/Targets/WebServiceTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,6 @@ public WebServiceTarget()
Encoding = new UTF8Encoding(writeBOM);
IncludeBOM = writeBOM;

Headers = new List<MethodCallParameter>();

#if NETSTANDARD1_3 || NETSTANDARD1_5
// NetCore1 throws PlatformNotSupportedException on WebRequest.GetSystemWebProxy, when using DefaultWebProxy
// Net5 (or newer) will turn off Http-connection-pooling if not using DefaultWebProxy
Expand Down Expand Up @@ -223,7 +221,7 @@ public WebServiceProxyType ProxyType
/// </summary>
/// <docgen category='Web Service Options' order='10' />
[ArrayParameter(typeof(MethodCallParameter), "header")]
public IList<MethodCallParameter> Headers { get; private set; }
public IList<MethodCallParameter> Headers { get; } = new List<MethodCallParameter>();

/// <summary>
/// Indicates whether to pre-authenticate the HttpWebRequest (Requires 'Authorization' in <see cref="Headers"/> parameters)
Expand Down Expand Up @@ -574,7 +572,7 @@ protected HttpPostFormatterBase(WebServiceTarget target)
protected string ContentType => _contentType ?? (_contentType = GetContentType(Target));
private string _contentType;

protected WebServiceTarget Target { get; private set; }
protected WebServiceTarget Target { get; }

protected virtual string GetContentType(WebServiceTarget target)
{
Expand Down
2 changes: 1 addition & 1 deletion src/NLog/Targets/Wrappers/CompoundTargetBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ protected CompoundTargetBase(params Target[] targets)
/// <summary>
/// Gets the collection of targets managed by this compound target.
/// </summary>
public IList<Target> Targets { get; private set; }
public IList<Target> Targets { get; }

/// <inheritdoc/>
public override string ToString()
Expand Down
3 changes: 1 addition & 2 deletions src/NLog/Targets/Wrappers/PostFilteringTargetWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ public PostFilteringTargetWrapper(string name, Target wrappedTarget)
{
Name = name;
WrappedTarget = wrappedTarget;
Rules = new List<FilteringRule>();
}

/// <summary>
Expand All @@ -113,7 +112,7 @@ public PostFilteringTargetWrapper(string name, Target wrappedTarget)
/// </summary>
/// <docgen category='Filtering Rules' order='10' />
[ArrayParameter(typeof(FilteringRule), "when")]
public IList<FilteringRule> Rules { get; private set; }
public IList<FilteringRule> Rules { get; } = new List<FilteringRule>();

/// <inheritdoc/>
protected override void Write(AsyncLogEventInfo logEvent)
Expand Down

0 comments on commit 9476449

Please sign in to comment.