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

Add support C# 8 nullable reference types #98

Merged
merged 17 commits into from
Jul 12, 2021
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
version: '{build}'
skip_tags: true
image: Visual Studio 2017
image: Visual Studio 2019
configuration: Release
test: off
build_script:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ public static LoggerConfiguration Console(
this LoggerSinkConfiguration sinkConfiguration,
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
string outputTemplate = DefaultConsoleOutputTemplate,
IFormatProvider formatProvider = null,
LoggingLevelSwitch levelSwitch = null,
IFormatProvider? formatProvider = null,
LoggingLevelSwitch? levelSwitch = null,
LogEventLevel? standardErrorFromLevel = null,
ConsoleTheme theme = null,
ConsoleTheme? theme = null,
bool applyThemeToRedirectedOutput = false,
object syncRoot = null)
object? syncRoot = null)
{
if (sinkConfiguration == null) throw new ArgumentNullException(nameof(sinkConfiguration));
if (outputTemplate == null) throw new ArgumentNullException(nameof(outputTemplate));
Expand All @@ -68,7 +68,7 @@ public static LoggerConfiguration Console(
ConsoleTheme.None :
theme ?? SystemConsoleThemes.Literate;

syncRoot = syncRoot ?? DefaultSyncRoot;
syncRoot ??= DefaultSyncRoot;

var formatter = new OutputTemplateRenderer(appliedTheme, outputTemplate, formatProvider);
return sinkConfiguration.Sink(new ConsoleSink(appliedTheme, formatter, standardErrorFromLevel, syncRoot), restrictedToMinimumLevel, levelSwitch);
Expand All @@ -93,14 +93,15 @@ public static LoggerConfiguration Console(
this LoggerSinkConfiguration sinkConfiguration,
ITextFormatter formatter,
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
LoggingLevelSwitch levelSwitch = null,
LoggingLevelSwitch? levelSwitch = null,
LogEventLevel? standardErrorFromLevel = null,
object syncRoot = null)
object? syncRoot = null)
{
if (sinkConfiguration == null) throw new ArgumentNullException(nameof(sinkConfiguration));
if (formatter == null) throw new ArgumentNullException(nameof(formatter));

syncRoot = syncRoot ?? DefaultSyncRoot;
syncRoot ??= DefaultSyncRoot;

return sinkConfiguration.Sink(new ConsoleSink(ConsoleTheme.None, formatter, standardErrorFromLevel, syncRoot), restrictedToMinimumLevel, levelSwitch);
}
}
Expand Down
20 changes: 12 additions & 8 deletions src/Serilog.Sinks.Console/Serilog.Sinks.Console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<Description>A Serilog sink that writes log events to the console/terminal.</Description>
<VersionPrefix>4.0.0</VersionPrefix>
<Authors>Serilog Contributors</Authors>
<TargetFrameworks>net45;netstandard1.3;netstandard2.0;netcoreapp1.1;netcoreapp2.0</TargetFrameworks>
<TargetFrameworks>net45;netstandard1.3;netstandard2.0;netstandard2.1;netcoreapp1.1;netcoreapp2.0;netcoreapp3.0;netcoreapp3.1;</TargetFrameworks>
rafaelsc marked this conversation as resolved.
Show resolved Hide resolved
<AssemblyName>Serilog.Sinks.Console</AssemblyName>
<AssemblyOriginatorKeyFile>../../assets/Serilog.snk</AssemblyOriginatorKeyFile>
<SignAssembly>true</SignAssembly>
Expand All @@ -20,17 +20,22 @@
<GenerateAssemblyFileVersionAttribute>true</GenerateAssemblyFileVersionAttribute>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<!-- Don't reference the full NETStandard.Library -->
<DisableImplicitFrameworkReferences Condition=" '$(TargetFramework)' != 'netstandard2.0' And '$(TargetFramework)' != 'netcoreapp2.0' ">true</DisableImplicitFrameworkReferences>
<DisableImplicitFrameworkReferences Condition=" '$(TargetFramework)' != 'netstandard2.0' And '$(TargetFramework)' != 'netstandard2.1' And '$(TargetFramework)' != 'netcoreapp2.0' And '$(TargetFramework)' != 'netcoreapp3.0' And '$(TargetFramework)' != 'netcoreapp3.1' ">true</DisableImplicitFrameworkReferences>
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
<TreatSpecificWarningsAsErrors />
<RootNamespace>Serilog</RootNamespace>
</PropertyGroup>

<PropertyGroup>
rafaelsc marked this conversation as resolved.
Show resolved Hide resolved
<LangVersion>8.0</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'net45' ">
<DefineConstants>$(DefineConstants);PINVOKE</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'netcoreapp1.1' ">
<PropertyGroup Condition=" '$(TargetFramework)' == 'netcoreapp1.1' or '$(TargetFramework)' == 'netcoreapp2.0' or '$(TargetFramework)' == 'netcoreapp3.0' ">
<DefineConstants>$(DefineConstants);PINVOKE;RUNTIME_INFORMATION</DefineConstants>
</PropertyGroup>

Expand All @@ -44,15 +49,14 @@
<Reference Include="Microsoft.CSharp" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.3' ">
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.3' or '$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'netstandard2.1' ">
<PackageReference Include="System.Console" Version="4.3.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp1.1' ">
<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp1.1' or '$(TargetFramework)' == 'netcoreapp2.0' or '$(TargetFramework)' == 'netcoreapp3.0' or '$(TargetFramework)' == 'netcoreapp3.1' ">
<PackageReference Include="System.Console" Version="4.3.0" />
<PackageReference Include="System.Runtime.InteropServices" Version="4.3.0" />
<PackageReference Include="System.Runtime.InteropServices.RuntimeInformation" Version="4.3.0" />
</ItemGroup>


</Project>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ namespace Serilog.Sinks.SystemConsole.Formatting
{
class ThemedDisplayValueFormatter : ThemedValueFormatter
{
readonly IFormatProvider _formatProvider;
readonly IFormatProvider? _formatProvider;

public ThemedDisplayValueFormatter(ConsoleTheme theme, IFormatProvider formatProvider)
public ThemedDisplayValueFormatter(ConsoleTheme theme, IFormatProvider? formatProvider)
: base(theme)
{
_formatProvider = formatProvider;
Expand Down Expand Up @@ -150,7 +150,7 @@ protected override int VisitDictionaryValue(ThemedValueFormatterState state, Dic
return count;
}

public int FormatLiteralValue(ScalarValue scalar, TextWriter output, string format)
public int FormatLiteralValue(ScalarValue scalar, TextWriter output, string? format)
{
var value = scalar.Value;
var count = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ namespace Serilog.Sinks.SystemConsole.Formatting
class ThemedJsonValueFormatter : ThemedValueFormatter
{
readonly ThemedDisplayValueFormatter _displayFormatter;
readonly IFormatProvider _formatProvider;
readonly IFormatProvider? _formatProvider;

public ThemedJsonValueFormatter(ConsoleTheme theme, IFormatProvider formatProvider)
public ThemedJsonValueFormatter(ConsoleTheme theme, IFormatProvider? formatProvider)
: base(theme)
{
_displayFormatter = new ThemedDisplayValueFormatter(theme, formatProvider);
Expand Down Expand Up @@ -154,7 +154,7 @@ protected override int VisitDictionaryValue(ThemedValueFormatterState state, Dic
: ConsoleThemeStyle.Scalar;

using (ApplyStyle(state.Output, style, ref count))
JsonValueFormatter.WriteQuotedJsonString((element.Key.Value ?? "null").ToString(), state.Output);
JsonValueFormatter.WriteQuotedJsonString((element.Key.Value?.ToString() ?? "null"), state.Output);
rafaelsc marked this conversation as resolved.
Show resolved Hide resolved

using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count))
state.Output.Write(": ");
Expand Down Expand Up @@ -248,7 +248,7 @@ int FormatLiteralValue(ScalarValue scalar, TextWriter output)
}

using (ApplyStyle(output, ConsoleThemeStyle.Scalar, ref count))
JsonValueFormatter.WriteQuotedJsonString(value.ToString(), output);
JsonValueFormatter.WriteQuotedJsonString(value.ToString() ?? "null", output);
rafaelsc marked this conversation as resolved.
Show resolved Hide resolved

return count;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected StyleReset ApplyStyle(TextWriter output, ConsoleThemeStyle style, ref
return _theme.Apply(output, style, ref invisibleCharacterCount);
}

public int Format(LogEventPropertyValue value, TextWriter output, string format, bool literalTopLevel = false)
public int Format(LogEventPropertyValue value, TextWriter output, string? format, bool literalTopLevel = false)
{
return Visit(new ThemedValueFormatterState { Output = output, Format = format, IsTopLevel = literalTopLevel }, value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace Serilog.Sinks.SystemConsole.Formatting
struct ThemedValueFormatterState
{
public TextWriter Output;
public string Format;
public string? Format;
public bool IsTopLevel;

public ThemedValueFormatterState Nest() => new ThemedValueFormatterState { Output = Output };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ class EventPropertyTokenRenderer : OutputTemplateTokenRenderer
{
readonly ConsoleTheme _theme;
readonly PropertyToken _token;
readonly IFormatProvider _formatProvider;
readonly IFormatProvider? _formatProvider;

public EventPropertyTokenRenderer(ConsoleTheme theme, PropertyToken token, IFormatProvider formatProvider)
public EventPropertyTokenRenderer(ConsoleTheme theme, PropertyToken token, IFormatProvider? formatProvider)
{
_theme = theme;
_token = token;
Expand Down Expand Up @@ -62,7 +62,7 @@ public override void Render(LogEvent logEvent, TextWriter output)

if (_token.Alignment.HasValue)
{
var str = writer.ToString();
var str = writer.ToString() ?? throw new InvalidOperationException("The output TextWriter, return a invalid state.");
rafaelsc marked this conversation as resolved.
Show resolved Hide resolved
Padding.Apply(output, str, _token.Alignment);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public override void Render(LogEvent logEvent, TextWriter output)
return;

var lines = new StringReader(logEvent.Exception.ToString());
string nextLine;
string? nextLine;
while ((nextLine = lines.ReadLine()) != null)
{
var style = nextLine.StartsWith(StackFrameLinePrefix) ? ConsoleThemeStyle.SecondaryText : ConsoleThemeStyle.Text;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ static class LevelOutputFormat
new[] { "F", "FA", "FTL", "FATL" },
};

public static string GetLevelMoniker(LogEventLevel value, string format = null)
public static string GetLevelMoniker(LogEventLevel value, string? format = null)
{
if (format == null || format.Length != 2 && format.Length != 3)
return Casing.Format(value.ToString(), format);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ public LevelTokenRenderer(ConsoleTheme theme, PropertyToken levelToken)
_levelToken = levelToken;
}

protected LevelTokenRenderer()
{
}

public override void Render(LogEvent logEvent, TextWriter output)
{
var moniker = LevelOutputFormat.GetLevelMoniker(logEvent.Level, _levelToken.Format);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ class MessageTemplateOutputTokenRenderer : OutputTemplateTokenRenderer
readonly PropertyToken _token;
readonly ThemedMessageTemplateRenderer _renderer;

public MessageTemplateOutputTokenRenderer(ConsoleTheme theme, PropertyToken token, IFormatProvider formatProvider)
public MessageTemplateOutputTokenRenderer(ConsoleTheme theme, PropertyToken token, IFormatProvider? formatProvider)
{
_theme = theme ?? throw new ArgumentNullException(nameof(theme));
_token = token ?? throw new ArgumentNullException(nameof(token));

bool isLiteral = false, isJson = false;

if (token.Format != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ class OutputTemplateRenderer : ITextFormatter
{
readonly OutputTemplateTokenRenderer[] _renderers;

public OutputTemplateRenderer(ConsoleTheme theme, string outputTemplate, IFormatProvider formatProvider)
public OutputTemplateRenderer(ConsoleTheme theme, string outputTemplate, IFormatProvider? formatProvider)
{
if (outputTemplate == null) throw new ArgumentNullException(nameof(outputTemplate));

var template = new MessageTemplateParser().Parse(outputTemplate);

var renderers = new List<OutputTemplateTokenRenderer>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ class PropertiesTokenRenderer : OutputTemplateTokenRenderer
readonly PropertyToken _token;
readonly ThemedValueFormatter _valueFormatter;

public PropertiesTokenRenderer(ConsoleTheme theme, PropertyToken token, MessageTemplate outputTemplate, IFormatProvider formatProvider)
public PropertiesTokenRenderer(ConsoleTheme theme, PropertyToken token, MessageTemplate outputTemplate, IFormatProvider? formatProvider)
{
_outputTemplate = outputTemplate;
_theme = theme ?? throw new ArgumentNullException(nameof(theme));
_token = token ?? throw new ArgumentNullException(nameof(token));

var isJson = false;

if (token.Format != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ class TimestampTokenRenderer : OutputTemplateTokenRenderer
{
readonly ConsoleTheme _theme;
readonly PropertyToken _token;
readonly IFormatProvider _formatProvider;
readonly IFormatProvider? _formatProvider;

public TimestampTokenRenderer(ConsoleTheme theme, PropertyToken token, IFormatProvider formatProvider)
public TimestampTokenRenderer(ConsoleTheme theme, PropertyToken token, IFormatProvider? formatProvider)
{
_theme = theme;
_token = token;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ static class Casing
/// <param name="value">Provided string for formatting.</param>
/// <param name="format">Format string.</param>
/// <returns>The provided <paramref name="value"/> with formatting applied.</returns>
public static string Format(string value, string format = null)
public static string Format(string value, string? format = null)
{
switch (format)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,12 @@ int RenderPropertyToken(PropertyToken pt, IReadOnlyDictionary<string, LogEventPr

int RenderAlignedPropertyTokenUnbuffered(PropertyToken pt, TextWriter output, LogEventPropertyValue propertyValue)
{
if(pt.Alignment == null) throw new ArgumentException("Invalid PropertyToken State. The PropertyToken should have a Alignment.", nameof(pt));
rafaelsc marked this conversation as resolved.
Show resolved Hide resolved

var valueOutput = new StringWriter();
RenderValue(NoTheme, _unthemedValueFormatter, propertyValue, valueOutput, pt.Format);

var valueLength = valueOutput.ToString().Length;
// ReSharper disable once PossibleInvalidOperationException
if (valueLength >= pt.Alignment.Value.Width)
{
return RenderValue(_theme, _valueFormatter, propertyValue, output, pt.Format);
Expand All @@ -123,7 +124,7 @@ int RenderAlignedPropertyTokenUnbuffered(PropertyToken pt, TextWriter output, Lo
return RenderValue(_theme, _valueFormatter, propertyValue, output, pt.Format);
}

int RenderValue(ConsoleTheme theme, ThemedValueFormatter valueFormatter, LogEventPropertyValue propertyValue, TextWriter output, string format)
int RenderValue(ConsoleTheme theme, ThemedValueFormatter valueFormatter, LogEventPropertyValue propertyValue, TextWriter output, string? format)
{
if (_isLiteral && propertyValue is ScalarValue sv && sv.Value is string)
{
Expand Down
Loading