Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

format and formatProvider can be null #1715

Merged
merged 3 commits into from Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 3 additions & 5 deletions src/Serilog/Capturing/PropertyBinder.cs
Expand Up @@ -51,15 +51,13 @@ public EventProperty[] ConstructProperties(MessageTemplate messageTemplate, obje
}

if (messageTemplate.PositionalProperties != null)
return ConstructPositionalProperties(messageTemplate, messageTemplateParameters);
return ConstructPositionalProperties(messageTemplate, messageTemplateParameters, messageTemplate.PositionalProperties);

return ConstructNamedProperties(messageTemplate, messageTemplateParameters!);
}

EventProperty[] ConstructPositionalProperties(MessageTemplate template, object?[] messageTemplateParameters)
EventProperty[] ConstructPositionalProperties(MessageTemplate template, object?[] messageTemplateParameters, PropertyToken[] positionalProperties)
{
var positionalProperties = template.PositionalProperties;

if (positionalProperties.Length != messageTemplateParameters.Length)
SelfLog.WriteLine("Positional property count does not match parameter count: {0}", template);

Expand Down Expand Up @@ -107,7 +105,7 @@ EventProperty[] ConstructNamedProperties(MessageTemplate template, object[] mess
var result = new EventProperty[messageTemplateParameters.Length];
for (var i = 0; i < matchedRun; ++i)
{
var property = template.NamedProperties[i];
var property = namedProperties[i];
var value = messageTemplateParameters[i];
result[i] = ConstructProperty(property, value);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Serilog/Events/DictionaryValue.cs
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#nullable enable
using System;
using System.Collections.Generic;
using System.IO;
Expand Down Expand Up @@ -49,7 +50,7 @@ public DictionaryValue(IEnumerable<KeyValuePair<ScalarValue, LogEventPropertyVal
/// <param name="formatProvider">A format provider to apply to the value, or null to use the default.</param>
/// <seealso cref="LogEventPropertyValue.ToString(string, IFormatProvider)"/>.
/// <exception cref="ArgumentNullException">When <paramref name="output"/> is <code>null</code></exception>
public override void Render(TextWriter output, string format = null, IFormatProvider formatProvider = null)
public override void Render(TextWriter output, string? format = null, IFormatProvider? formatProvider = null)
{
if (output == null) throw new ArgumentNullException(nameof(output));

Expand Down
13 changes: 7 additions & 6 deletions src/Serilog/Events/LogEvent.cs
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#nullable enable
using System;
using System.Collections.Generic;
using System.IO;
Expand All @@ -25,7 +26,7 @@ public class LogEvent
{
readonly Dictionary<string, LogEventPropertyValue> _properties;

LogEvent(DateTimeOffset timestamp, LogEventLevel level, Exception exception, MessageTemplate messageTemplate, Dictionary<string, LogEventPropertyValue> properties)
LogEvent(DateTimeOffset timestamp, LogEventLevel level, Exception? exception, MessageTemplate messageTemplate, Dictionary<string, LogEventPropertyValue> properties)
{
Timestamp = timestamp;
Level = level;
Expand All @@ -44,7 +45,7 @@ public class LogEvent
/// <param name="properties">Properties associated with the event, including those presented in <paramref name="messageTemplate"/>.</param>
/// <exception cref="ArgumentNullException">When <paramref name="messageTemplate"/> is <code>null</code></exception>
/// <exception cref="ArgumentNullException">When <paramref name="properties"/> is <code>null</code></exception>
public LogEvent(DateTimeOffset timestamp, LogEventLevel level, Exception exception, MessageTemplate messageTemplate, IEnumerable<LogEventProperty> properties)
public LogEvent(DateTimeOffset timestamp, LogEventLevel level, Exception? exception, MessageTemplate messageTemplate, IEnumerable<LogEventProperty> properties)
: this(timestamp, level, exception, messageTemplate, new Dictionary<string, LogEventPropertyValue>())
{
if (properties == null) throw new ArgumentNullException(nameof(properties));
Expand All @@ -63,7 +64,7 @@ public LogEvent(DateTimeOffset timestamp, LogEventLevel level, Exception excepti
/// <param name="properties">Properties associated with the event, including those presented in <paramref name="messageTemplate"/>.</param>
/// <exception cref="ArgumentNullException">When <paramref name="messageTemplate"/> is <code>null</code></exception>
/// <exception cref="ArgumentNullException">When <paramref name="properties"/> is <code>null</code></exception>
internal LogEvent(DateTimeOffset timestamp, LogEventLevel level, Exception exception, MessageTemplate messageTemplate, EventProperty[] properties)
internal LogEvent(DateTimeOffset timestamp, LogEventLevel level, Exception? exception, MessageTemplate messageTemplate, EventProperty[] properties)
: this(timestamp, level, exception, messageTemplate, new Dictionary<string, LogEventPropertyValue>(properties?.Length ?? throw new ArgumentNullException(nameof(properties))))
{
for (var i = 0; i < properties.Length; ++i)
Expand Down Expand Up @@ -91,7 +92,7 @@ internal LogEvent(DateTimeOffset timestamp, LogEventLevel level, Exception excep
/// </summary>
/// <param name="output">The output.</param>
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
public void RenderMessage(TextWriter output, IFormatProvider formatProvider = null)
public void RenderMessage(TextWriter output, IFormatProvider? formatProvider = null)
{
MessageTemplate.Render(Properties, output, formatProvider);
}
Expand All @@ -101,7 +102,7 @@ public void RenderMessage(TextWriter output, IFormatProvider formatProvider = nu
/// with the event, and return the result.
/// </summary>
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
public string RenderMessage(IFormatProvider formatProvider = null)
public string RenderMessage(IFormatProvider? formatProvider = null)
{
return MessageTemplate.Render(Properties, formatProvider);
}
Expand All @@ -114,7 +115,7 @@ public string RenderMessage(IFormatProvider formatProvider = null)
/// <summary>
/// An exception associated with the event, or null.
/// </summary>
public Exception Exception { get; }
public Exception? Exception { get; }

/// <summary>
/// Add a property to the event if not already present, otherwise, update its value.
Expand Down
5 changes: 3 additions & 2 deletions src/Serilog/Events/LogEventPropertyValue.cs
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#nullable enable
using System;
using System.IO;

Expand All @@ -30,7 +31,7 @@ public abstract class LogEventPropertyValue : IFormattable
/// <param name="format">A format string applied to the value, or null.</param>
/// <param name="formatProvider">A format provider to apply to the value, or null to use the default.</param>
/// <seealso cref="LogEventPropertyValue.ToString(string, IFormatProvider)"/>.
public abstract void Render(TextWriter output, string format = null, IFormatProvider formatProvider = null);
public abstract void Render(TextWriter output, string? format = null, IFormatProvider? formatProvider = null);

/// <summary>
/// Returns a string that represents the current object.
Expand All @@ -52,7 +53,7 @@ public abstract class LogEventPropertyValue : IFormattable
/// <param name="formatProvider">The provider to use to format the value.-or- A null reference
/// (Nothing in Visual Basic) to obtain the numeric format information from the current locale
/// setting of the operating system. </param><filterpriority>2</filterpriority>
public string ToString(string format, IFormatProvider formatProvider)
public string ToString(string? format, IFormatProvider? formatProvider)
{
var output = new StringWriter();
Render(output, format, formatProvider);
Expand Down
9 changes: 5 additions & 4 deletions src/Serilog/Events/MessageTemplate.cs
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#nullable enable
using System;
using System.Collections.Generic;
using System.IO;
Expand Down Expand Up @@ -122,9 +123,9 @@ static TResult[] GetElementsOfTypeToArray<TResult>(MessageTemplateToken[] tokens

internal MessageTemplateToken[] TokenArray => _tokens;

internal PropertyToken[] NamedProperties { get; }
internal PropertyToken[]? NamedProperties { get; }

internal PropertyToken[] PositionalProperties { get; }
internal PropertyToken[]? PositionalProperties { get; }

/// <summary>
/// Convert the message template into a textual message, given the
Expand All @@ -136,7 +137,7 @@ static TResult[] GetElementsOfTypeToArray<TResult>(MessageTemplateToken[] tokens
/// properties are mismatched with the template, the template will be
/// returned with incomplete substitution.</returns>
/// <exception cref="ArgumentNullException">When <paramref name="properties"/> is <code>null</code></exception>
public string Render(IReadOnlyDictionary<string, LogEventPropertyValue> properties, IFormatProvider formatProvider = null)
public string Render(IReadOnlyDictionary<string, LogEventPropertyValue> properties, IFormatProvider? formatProvider = null)
{
var writer = new StringWriter(formatProvider);
Render(properties, writer, formatProvider);
Expand All @@ -154,7 +155,7 @@ public string Render(IReadOnlyDictionary<string, LogEventPropertyValue> properti
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
/// <exception cref="ArgumentNullException">When <paramref name="properties"/> is <code>null</code></exception>
/// <exception cref="ArgumentNullException">When <paramref name="output"/> is <code>null</code></exception>
public void Render(IReadOnlyDictionary<string, LogEventPropertyValue> properties, TextWriter output, IFormatProvider formatProvider = null)
public void Render(IReadOnlyDictionary<string, LogEventPropertyValue> properties, TextWriter output, IFormatProvider? formatProvider = null)
{
if (properties == null) throw new ArgumentNullException(nameof(properties));
if (output == null) throw new ArgumentNullException(nameof(output));
Expand Down
5 changes: 3 additions & 2 deletions src/Serilog/Formatting/Display/Obsolete/LiteralStringValue.cs
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#nullable enable
using System;
using System.IO;
using Serilog.Events;
Expand All @@ -31,12 +32,12 @@ public LiteralStringValue(string value)
_value = value ?? throw new ArgumentNullException(nameof(value));
}

public override void Render(TextWriter output, string format = null, IFormatProvider formatProvider = null)
public override void Render(TextWriter output, string? format = null, IFormatProvider? formatProvider = null)
{
output.Write(Casing.Format(_value, format));
}

public override bool Equals(object obj)
public override bool Equals(object? obj)
{
return obj is LiteralStringValue sv && Equals(_value, sv._value);
}
Expand Down
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#nullable enable
using System;
using System.IO;

Expand All @@ -32,7 +33,7 @@ public LogEventLevelValue(LogEventLevel value)
/// <summary>
/// This method will apply only upper or lower case formatting, not fixed width
/// </summary>
public override void Render(TextWriter output, string format = null, IFormatProvider formatProvider = null)
public override void Render(TextWriter output, string? format = null, IFormatProvider? formatProvider = null)
{
output.Write(LevelOutputFormat.GetLevelMoniker(_value, format));
}
Expand Down
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#nullable enable
using System;
using System.Collections.Generic;
using System.IO;
Expand All @@ -33,7 +34,7 @@ public LogEventPropertiesValue(MessageTemplate template, IReadOnlyDictionary<str
_outputTemplate = outputTemplate;
}

public override void Render(TextWriter output, string format = null, IFormatProvider formatProvider = null)
public override void Render(TextWriter output, string? format = null, IFormatProvider? formatProvider = null)
{
PropertiesOutputFormat.Render(_template, _properties, _outputTemplate, output, format, formatProvider);
}
Expand Down
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#nullable enable
using System;
using System.Collections.Generic;
using System.IO;
Expand All @@ -31,7 +32,7 @@ public LogEventPropertyMessageValue(MessageTemplate template, IReadOnlyDictionar
_properties = properties;
}

public override void Render(TextWriter output, string format = null, IFormatProvider formatProvider = null)
public override void Render(TextWriter output, string? format = null, IFormatProvider? formatProvider = null)
{
_template.Render(_properties, output, formatProvider);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Serilog/Formatting/Display/PropertiesOutputFormat.cs
Expand Up @@ -26,7 +26,7 @@ static class PropertiesOutputFormat
{
static readonly JsonValueFormatter JsonValueFormatter = new("$type");

public static void Render(MessageTemplate template, IReadOnlyDictionary<string, LogEventPropertyValue> properties, MessageTemplate outputTemplate, TextWriter output, string format, IFormatProvider? formatProvider = null)
public static void Render(MessageTemplate template, IReadOnlyDictionary<string, LogEventPropertyValue> properties, MessageTemplate outputTemplate, TextWriter output, string? format, IFormatProvider? formatProvider = null)
{
if (format?.Contains("j") == true)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Serilog/Formatting/Json/JsonFormatter.cs
Expand Up @@ -354,7 +354,7 @@ protected virtual void WriteDictionary(IReadOnlyDictionary<ScalarValue, LogEvent
/// Writes out a json property with the specified value on output writer
/// </summary>
[Obsolete(ExtensionPointObsoletionMessage)]
protected virtual void WriteJsonProperty(string name, object value, ref string precedingDelimiter, TextWriter output)
protected virtual void WriteJsonProperty(string name, object? value, ref string precedingDelimiter, TextWriter output)
{
output.Write(precedingDelimiter);
output.Write("\"");
Expand Down
5 changes: 3 additions & 2 deletions src/Serilog/Parsing/MessageTemplateToken.cs
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#nullable enable
using System;
using System.Collections.Generic;
using System.IO;
Expand Down Expand Up @@ -51,6 +52,6 @@ protected MessageTemplateToken(int startIndex)
/// <param name="output">Output for the rendered string.</param>
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
// ReSharper disable once UnusedMemberInSuper.Global
public abstract void Render(IReadOnlyDictionary<string, LogEventPropertyValue> properties, TextWriter output, IFormatProvider formatProvider = null);
public abstract void Render(IReadOnlyDictionary<string, LogEventPropertyValue> properties, TextWriter output, IFormatProvider? formatProvider = null);
}
}
}
9 changes: 5 additions & 4 deletions src/Serilog/Parsing/PropertyToken.cs
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#nullable enable
using System;
using System.Collections.Generic;
using System.ComponentModel;
Expand Down Expand Up @@ -55,7 +56,7 @@ public PropertyToken(string propertyName, string rawText, string formatObsolete,
/// <param name="startIndex">The token's start index in the template.</param>
/// <exception cref="ArgumentNullException">When <paramref name="propertyName"/> is <code>null</code></exception>
/// <exception cref="ArgumentNullException">When <paramref name="rawText"/> is <code>null</code></exception>
public PropertyToken(string propertyName, string rawText, string format = null, Alignment? alignment = null, Destructuring destructuring = Destructuring.Default, int startIndex = -1)
public PropertyToken(string propertyName, string rawText, string? format = null, Alignment? alignment = null, Destructuring destructuring = Destructuring.Default, int startIndex = -1)
: base(startIndex)
{
PropertyName = propertyName ?? throw new ArgumentNullException(nameof(propertyName));
Expand Down Expand Up @@ -84,7 +85,7 @@ public PropertyToken(string propertyName, string rawText, string format = null,
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
/// <exception cref="ArgumentNullException">When <paramref name="properties"/> is <code>null</code></exception>
/// <exception cref="ArgumentNullException">When <paramref name="output"/> is <code>null</code></exception>
public override void Render(IReadOnlyDictionary<string, LogEventPropertyValue> properties, TextWriter output, IFormatProvider formatProvider = null)
public override void Render(IReadOnlyDictionary<string, LogEventPropertyValue> properties, TextWriter output, IFormatProvider? formatProvider = null)
{
if (properties == null) throw new ArgumentNullException(nameof(properties));
if (output == null) throw new ArgumentNullException(nameof(output));
Expand All @@ -105,7 +106,7 @@ public override void Render(IReadOnlyDictionary<string, LogEventPropertyValue> p
/// <summary>
/// Format applied to the property.
/// </summary>
public string Format { get; }
public string? Format { get; }

/// <summary>
/// Alignment applied to the property.
Expand Down Expand Up @@ -143,7 +144,7 @@ public bool TryGetPositionalValue(out int position)
/// true if the specified object is equal to the current object; otherwise, false.
/// </returns>
/// <param name="obj">The object to compare with the current object. </param><filterpriority>2</filterpriority>
public override bool Equals(object obj)
public override bool Equals(object? obj)
{
return obj is PropertyToken pt &&
pt.Destructuring == Destructuring &&
Expand Down