Skip to content

Commit

Permalink
Merge pull request #55 from sungam3r/code-cleanup
Browse files Browse the repository at this point in the history
Spell check and cleanup
  • Loading branch information
nblumhardt authored Mar 14, 2019
2 parents 75e1dd4 + aff642d commit 95d1184
Show file tree
Hide file tree
Showing 26 changed files with 122 additions and 113 deletions.
2 changes: 1 addition & 1 deletion sample/ConsoleDemo/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Serilog;
using Serilog.Sinks.SystemConsole.Themes;
using System;
using System.Threading;
using Serilog.Sinks.SystemConsole.Themes;

namespace ConsoleDemo
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using Serilog.Configuration;
using Serilog.Core;
using Serilog.Events;
using Serilog.Formatting;
using Serilog.Sinks.SystemConsole;
using Serilog.Sinks.SystemConsole.Themes;
using Serilog.Sinks.SystemConsole.Output;
using Serilog.Sinks.SystemConsole.Themes;
using System;

namespace Serilog
{
Expand All @@ -36,11 +36,11 @@ public static class ConsoleLoggerConfigurationExtensions
/// <param name="sinkConfiguration">Logger sink configuration.</param>
/// <param name="restrictedToMinimumLevel">The minimum level for
/// events passed through the sink. Ignored when <paramref name="levelSwitch"/> is specified.</param>
/// <param name="outputTemplate">A message template describing the format used to write to the sink.
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
/// <param name="levelSwitch">A switch allowing the pass-through minimum level
/// to be changed at runtime.</param>
/// <param name="outputTemplate">A message template describing the format used to write to the sink.
/// the default is <code>"[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}"</code>.</param>
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
/// <param name="standardErrorFromLevel">Specifies the level at which events will be written to standard error.</param>
/// <param name="theme">The theme to apply to the styled output. If not specified,
/// uses <see cref="SystemConsoleTheme.Literate"/>.</param>
Expand Down
14 changes: 7 additions & 7 deletions src/Serilog.Sinks.Console/Sinks/SystemConsole/ConsoleSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.IO;
using System.Text;
using Serilog.Core;
using Serilog.Events;
using Serilog.Sinks.SystemConsole.Themes;
using Serilog.Formatting;
using Serilog.Sinks.SystemConsole.Platform;
using Serilog.Sinks.SystemConsole.Themes;
using System;
using System.IO;
using System.Text;

namespace Serilog.Sinks.SystemConsole
{
Expand All @@ -30,7 +30,7 @@ class ConsoleSink : ILogEventSink
readonly ITextFormatter _formatter;
readonly object _syncRoot = new object();

const int DefaultWriteBuffer = 256;
const int DefaultWriteBufferCapacity = 256;

static ConsoleSink()
{
Expand All @@ -56,7 +56,7 @@ public void Emit(LogEvent logEvent)
// buffered write here and have no effect when the line is actually written out.
if (_theme.CanBuffer)
{
var buffer = new StringWriter(new StringBuilder(DefaultWriteBuffer));
var buffer = new StringWriter(new StringBuilder(DefaultWriteBufferCapacity));
_formatter.Format(logEvent, buffer);
lock (_syncRoot)
{
Expand All @@ -76,7 +76,7 @@ public void Emit(LogEvent logEvent)

TextWriter SelectOutputStream(LogEventLevel logEventLevel)
{
if (!_standardErrorFromLevel.HasValue)
if (_standardErrorFromLevel == null)
return Console.Out;

return logEventLevel < _standardErrorFromLevel ? Console.Out : Console.Error;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,14 @@ protected override int VisitSequenceValue(ThemedValueFormatterState state, Seque
using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count))
state.Output.Write('[');

var delim = "";
var delim = string.Empty;
for (var index = 0; index < sequence.Elements.Count; ++index)
{
if (delim.Length != 0)
{
using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count))
state.Output.Write(delim);
}

delim = ", ";
Visit(state, sequence.Elements[index]);
Expand All @@ -84,12 +86,14 @@ protected override int VisitStructureValue(ThemedValueFormatterState state, Stru
using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count))
state.Output.Write('{');

var delim = "";
var delim = string.Empty;
for (var index = 0; index < structure.Properties.Count; ++index)
{
if (delim.Length != 0)
{
using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count))
state.Output.Write(delim);
}

delim = ", ";

Expand Down Expand Up @@ -117,12 +121,14 @@ protected override int VisitDictionaryValue(ThemedValueFormatterState state, Dic
using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count))
state.Output.Write('{');

var delim = "";
var delim = string.Empty;
foreach (var element in dictionary.Elements)
{
if (delim.Length != 0)
{
using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count))
state.Output.Write(delim);
}

delim = ", ";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,14 @@ protected override int VisitSequenceValue(ThemedValueFormatterState state, Seque
using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count))
state.Output.Write('[');

var delim = "";
var delim = string.Empty;
for (var index = 0; index < sequence.Elements.Count; ++index)
{
if (delim.Length != 0)
{
using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count))
state.Output.Write(delim);
}

delim = ", ";
Visit(state.Nest(), sequence.Elements[index]);
Expand All @@ -84,12 +86,14 @@ protected override int VisitStructureValue(ThemedValueFormatterState state, Stru
using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count))
state.Output.Write('{');

var delim = "";
var delim = string.Empty;
for (var index = 0; index < structure.Properties.Count; ++index)
{
if (delim.Length != 0)
{
using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count))
state.Output.Write(delim);
}

delim = ", ";

Expand Down Expand Up @@ -132,12 +136,14 @@ protected override int VisitDictionaryValue(ThemedValueFormatterState state, Dic
using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count))
state.Output.Write('{');

var delim = "";
var delim = string.Empty;
foreach (var element in dictionary.Elements)
{
if (delim.Length != 0)
{
using (ApplyStyle(state.Output, ConsoleThemeStyle.TertiaryText, ref count))
state.Output.Write(delim);
}

delim = ", ";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ struct ThemedValueFormatterState
public string Format;
public bool IsTopLevel;

public ThemedValueFormatterState Nest()
{
return new ThemedValueFormatterState {Output = Output};
}
public ThemedValueFormatterState Nest() => new ThemedValueFormatterState { Output = Output };
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,9 @@ public EventPropertyTokenRenderer(ConsoleTheme theme, PropertyToken token, IForm
public override void Render(LogEvent logEvent, TextWriter output)
{
// If a property is missing, don't render anything (message templates render the raw token here).
LogEventPropertyValue propertyValue;
if (!logEvent.Properties.TryGetValue(_token.PropertyName, out propertyValue))
if (!logEvent.Properties.TryGetValue(_token.PropertyName, out var propertyValue))
{
Padding.Apply(output, "", _token.Alignment);
Padding.Apply(output, string.Empty, _token.Alignment);
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,34 @@ namespace Serilog.Sinks.SystemConsole.Output
/// </summary>
static class LevelOutputFormat
{
static readonly string[][] TitleCaseLevelMap = {
new []{ "V", "Vb", "Vrb", "Verb" },
new []{ "D", "De", "Dbg", "Dbug" },
new []{ "I", "In", "Inf", "Info" },
new []{ "W", "Wn", "Wrn", "Warn" },
new []{ "E", "Er", "Err", "Eror" },
new []{ "F", "Fa", "Ftl", "Fatl" }
static readonly string[][] TitleCaseLevelMap =
{
new[] { "V", "Vb", "Vrb", "Verb" },
new[] { "D", "De", "Dbg", "Dbug" },
new[] { "I", "In", "Inf", "Info" },
new[] { "W", "Wn", "Wrn", "Warn" },
new[] { "E", "Er", "Err", "Eror" },
new[] { "F", "Fa", "Ftl", "Fatl" },
};

static readonly string[][] LowercaseLevelMap = {
new []{ "v", "vb", "vrb", "verb" },
new []{ "d", "de", "dbg", "dbug" },
new []{ "i", "in", "inf", "info" },
new []{ "w", "wn", "wrn", "warn" },
new []{ "e", "er", "err", "eror" },
new []{ "f", "fa", "ftl", "fatl" }
static readonly string[][] LowercaseLevelMap =
{
new[] { "v", "vb", "vrb", "verb" },
new[] { "d", "de", "dbg", "dbug" },
new[] { "i", "in", "inf", "info" },
new[] { "w", "wn", "wrn", "warn" },
new[] { "e", "er", "err", "eror" },
new[] { "f", "fa", "ftl", "fatl" },
};

static readonly string[][] UppercaseLevelMap = {
new []{ "V", "VB", "VRB", "VERB" },
new []{ "D", "DE", "DBG", "DBUG" },
new []{ "I", "IN", "INF", "INFO" },
new []{ "W", "WN", "WRN", "WARN" },
new []{ "E", "ER", "ERR", "EROR" },
new []{ "F", "FA", "FTL", "FATL" }
static readonly string[][] UppercaseLevelMap =
{
new[] { "V", "VB", "VRB", "VERB" },
new[] { "D", "DE", "DBG", "DBUG" },
new[] { "I", "IN", "INF", "INFO" },
new[] { "W", "WN", "WRN", "WARN" },
new[] { "E", "ER", "ERR", "EROR" },
new[] { "F", "FA", "FTL", "FATL" },
};

public static string GetLevelMoniker(LogEventLevel value, string format = null)
Expand Down Expand Up @@ -78,7 +81,7 @@ public static string GetLevelMoniker(LogEventLevel value, string format = null)
}

var index = (int)value;
if (index >= 0 && index <= (int) LogEventLevel.Fatal)
if (index >= 0 && index <= (int)LogEventLevel.Fatal)
{
switch (format[0])
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ class LevelTokenRenderer : OutputTemplateTokenRenderer
{
readonly ConsoleTheme _theme;
readonly PropertyToken _levelToken;

static readonly Dictionary<LogEventLevel, ConsoleThemeStyle> Levels = new Dictionary<LogEventLevel, ConsoleThemeStyle>
{
{ LogEventLevel.Verbose, ConsoleThemeStyle.LevelVerbose },
{ LogEventLevel.Debug, ConsoleThemeStyle.LevelDebug },
{ LogEventLevel.Information, ConsoleThemeStyle.LevelInformation },
{ LogEventLevel.Warning, ConsoleThemeStyle.LevelWarning },
{ LogEventLevel.Error, ConsoleThemeStyle.LevelError },
{ LogEventLevel.Fatal, ConsoleThemeStyle.LevelFatal }
{ LogEventLevel.Fatal, ConsoleThemeStyle.LevelFatal },
};

public LevelTokenRenderer(ConsoleTheme theme, PropertyToken levelToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ public override void Render(LogEvent logEvent, TextWriter output)
using (_theme.Apply(output, ConsoleThemeStyle.SecondaryText, ref _))
{
if (_token.Alignment == null)
{
sv.Render(output, _token.Format, _formatProvider);
}
else
{
var buffer = new StringWriter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ static class WindowsConsole
{
#if PINVOKE
public static void EnableVirtualTerminalProcessing()
{
{
#if RUNTIME_INFORMATION
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
return;
Expand All @@ -49,10 +49,10 @@ public static void EnableVirtualTerminalProcessing()
static extern bool GetConsoleMode(IntPtr handle, out uint mode);

[DllImport("kernel32.dll", SetLastError = true)]
static extern bool SetConsoleMode(IntPtr handle, uint mode);
static extern bool SetConsoleMode(IntPtr handle, uint mode);
#else
public static void EnableVirtualTerminalProcessing()
{
{
}
#endif
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ static class Casing
{
/// <summary>
/// Apply upper or lower casing to <paramref name="value"/> when <paramref name="format"/> is provided.
/// Returns <paramref name="value"/> when no or invalid format provided
/// Returns <paramref name="value"/> when no or invalid format provided.
/// </summary>
/// <returns>The provided <paramref name="value"/> with formatting applied</returns>
/// <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)
{
switch (format)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ static class Padding
/// <summary>
/// Writes the provided value to the output, applying direction-based padding when <paramref name="alignment"/> is provided.
/// </summary>
/// <param name="output">Output object to write result.</param>
/// <param name="value">Provided value.</param>
/// <param name="alignment">The alignment settings to apply when rendering <paramref name="value"/>.</param>
public static void Apply(TextWriter output, string value, Alignment? alignment)
{
if (!alignment.HasValue || value.Length >= alignment.Value.Width)
if (alignment == null || value.Length >= alignment.Value.Width)
{
output.Write(value);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ int RenderTextToken(TextToken tt, TextWriter output)

int RenderPropertyToken(PropertyToken pt, IReadOnlyDictionary<string, LogEventPropertyValue> properties, TextWriter output)
{
LogEventPropertyValue propertyValue;
if (!properties.TryGetValue(pt.PropertyName, out propertyValue))
if (!properties.TryGetValue(pt.PropertyName, out var propertyValue))
{
var count = 0;
using (_theme.Apply(output, ConsoleThemeStyle.Invalid, ref count))
Expand Down Expand Up @@ -116,11 +115,11 @@ int RenderAlignedPropertyTokenUnbuffered(PropertyToken pt, TextWriter output, Lo
if (pt.Alignment.Value.Direction == AlignmentDirection.Left)
{
var invisible = RenderValue(_theme, _valueFormatter, propertyValue, output, pt.Format);
Padding.Apply(output, "", pt.Alignment.Value.Widen(-valueLength));
Padding.Apply(output, string.Empty, pt.Alignment.Value.Widen(-valueLength));
return invisible;
}

Padding.Apply(output, "", pt.Alignment.Value.Widen(-valueLength));
Padding.Apply(output, string.Empty, pt.Alignment.Value.Widen(-valueLength));
return RenderValue(_theme, _valueFormatter, propertyValue, output, pt.Format);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class AnsiConsoleTheme : ConsoleTheme
public static AnsiConsoleTheme Grayscale { get; } = AnsiConsoleThemes.Grayscale;

/// <summary>
/// A theme in the syle of the original <i>Serilog.Sinks.Literate</i>.
/// A theme in the style of the original <i>Serilog.Sinks.Literate</i>.
/// </summary>
public static AnsiConsoleTheme Literate { get; } = AnsiConsoleThemes.Literate;

Expand Down
Loading

0 comments on commit 95d1184

Please sign in to comment.