Skip to content

Commit

Permalink
Merge pull request serilog#977 from nblumhardt/output-improvements
Browse files Browse the repository at this point in the history
Output and rendering improvements
  • Loading branch information
nblumhardt authored Jun 13, 2017
2 parents e953401 + e205c07 commit e42245c
Show file tree
Hide file tree
Showing 58 changed files with 758 additions and 384 deletions.
13 changes: 12 additions & 1 deletion Serilog.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@


<s:Boolean x:Key="/Default/CodeEditing/Intellisense/CodeCompletion/AutoCompleteBasicCompletion/@EntryValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeEditing/Intellisense/CodeCompletion/IntelliSenseCompletingCharacters/CSharpCompletingCharacters/UpgradedFromVSSettings/@EntryValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeEditing/Intellisense/CodeCompletion/IntelliSenseCompletingCharacters/IntelliSenseCompletingCharactersSettingCSharp/UpgradedFromVSSettings/@EntryValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeEditing/Intellisense/LookupWindow/ShowSummary/@EntryValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeEditing/Localization/CSharpLocalizationOptions/DontAnalyseVerbatimStrings/@EntryValue">False</s:Boolean>
<s:String x:Key="/Default/CodeInspection/Highlighting/AnalysisEnabled/@EntryValue">SOLUTION</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=ArgumentsStyleLiteral/@EntryIndexedValue">DO_NOT_SHOW</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=ArgumentsStyleNamedExpression/@EntryIndexedValue">DO_NOT_SHOW</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=AssignToImplicitGlobalInFunctionScope/@EntryIndexedValue">DO_NOT_SHOW</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=BaseObjectEqualsIsObjectEquals/@EntryIndexedValue">ERROR</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=CheckNamespace/@EntryIndexedValue">DO_NOT_SHOW</s:String>
Expand All @@ -16,6 +19,7 @@
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=ConditionalTernaryEqualBranch/@EntryIndexedValue">ERROR</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=ConditionIsAlwaysTrueOrFalse/@EntryIndexedValue">WARNING</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=ConstantNullCoalescingCondition/@EntryIndexedValue">ERROR</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=ConvertIfStatementToConditionalTernaryExpression/@EntryIndexedValue">HINT</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=CSharpWarnings_003A_003ACS0109/@EntryIndexedValue">ERROR</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=DoubleNegationOperator/@EntryIndexedValue">ERROR</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=EmptyConstructor/@EntryIndexedValue">ERROR</s:String>
Expand All @@ -27,8 +31,9 @@
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=ExpressionIsAlwaysNull/@EntryIndexedValue">ERROR</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=FieldCanBeMadeReadOnly_002EGlobal/@EntryIndexedValue">DO_NOT_SHOW</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=FieldCanBeMadeReadOnly_002ELocal/@EntryIndexedValue">DO_NOT_SHOW</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=ForCanBeConvertedToForeach/@EntryIndexedValue">HINT</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=ForCanBeConvertedToForeach/@EntryIndexedValue">DO_NOT_SHOW</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=InconsistentNaming/@EntryIndexedValue">DO_NOT_SHOW</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=JoinNullCheckWithUsage/@EntryIndexedValue">HINT</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=LocalizableElement/@EntryIndexedValue">DO_NOT_SHOW</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=MemberCanBePrivate_002EGlobal/@EntryIndexedValue">HINT</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=MultipleOrderBy/@EntryIndexedValue">ERROR</s:String>
Expand Down Expand Up @@ -103,6 +108,7 @@
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=UnusedMember_002EGlobal/@EntryIndexedValue">DO_NOT_SHOW</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=UnusedMember_002ELocal/@EntryIndexedValue">SUGGESTION</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=UnusedVariable/@EntryIndexedValue">ERROR</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=UseNullPropagation/@EntryIndexedValue">HINT</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=UseObjectOrCollectionInitializer/@EntryIndexedValue">ERROR</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=ValueParameterNotUsed/@EntryIndexedValue">ERROR</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=VBPossibleMistakenCallToGetType_002E1/@EntryIndexedValue">ERROR</s:String>
Expand All @@ -111,6 +117,8 @@
<s:String x:Key="/Default/CodeStyle/CodeCleanup/Profiles/=Format_0020My_0020Code_0020Using_0020_0022Particular_0022_0020conventions/@EntryIndexedValue">&lt;?xml version="1.0" encoding="utf-16"?&gt;&lt;Profile name="Format My Code Using &amp;quot;Particular&amp;quot; conventions"&gt;&lt;CSMakeFieldReadonly&gt;True&lt;/CSMakeFieldReadonly&gt;&lt;CSUseVar&gt;&lt;BehavourStyle&gt;CAN_CHANGE_TO_IMPLICIT&lt;/BehavourStyle&gt;&lt;LocalVariableStyle&gt;ALWAYS_IMPLICIT&lt;/LocalVariableStyle&gt;&lt;ForeachVariableStyle&gt;ALWAYS_IMPLICIT&lt;/ForeachVariableStyle&gt;&lt;/CSUseVar&gt;&lt;CSOptimizeUsings&gt;&lt;OptimizeUsings&gt;True&lt;/OptimizeUsings&gt;&lt;EmbraceInRegion&gt;False&lt;/EmbraceInRegion&gt;&lt;RegionName&gt;&lt;/RegionName&gt;&lt;/CSOptimizeUsings&gt;&lt;CSReformatCode&gt;True&lt;/CSReformatCode&gt;&lt;CSReorderTypeMembers&gt;True&lt;/CSReorderTypeMembers&gt;&lt;JsInsertSemicolon&gt;True&lt;/JsInsertSemicolon&gt;&lt;JsReformatCode&gt;True&lt;/JsReformatCode&gt;&lt;CssReformatCode&gt;True&lt;/CssReformatCode&gt;&lt;CSArrangeThisQualifier&gt;True&lt;/CSArrangeThisQualifier&gt;&lt;RemoveCodeRedundancies&gt;True&lt;/RemoveCodeRedundancies&gt;&lt;CSUseAutoProperty&gt;True&lt;/CSUseAutoProperty&gt;&lt;HtmlReformatCode&gt;True&lt;/HtmlReformatCode&gt;&lt;CSShortenReferences&gt;True&lt;/CSShortenReferences&gt;&lt;CSharpFormatDocComments&gt;True&lt;/CSharpFormatDocComments&gt;&lt;CssAlphabetizeProperties&gt;True&lt;/CssAlphabetizeProperties&gt;&lt;/Profile&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/CodeCleanup/RecentlyUsedProfile/@EntryValue">Default: Reformat Code</s:String>
<s:String x:Key="/Default/CodeStyle/CodeCleanup/SilentCleanupProfile/@EntryValue">Format My Code Using "Particular" conventions</s:String>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpCodeStyle/DEFAULT_INTERNAL_MODIFIER/@EntryValue">Implicit</s:String>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpCodeStyle/DEFAULT_PRIVATE_MODIFIER/@EntryValue">Implicit</s:String>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/EXPLICIT_PRIVATE_MODIFIER/@EntryValue">False</s:Boolean>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/FORCE_FOR_BRACES_STYLE/@EntryValue">DO_NOT_CHANGE</s:String>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/FORCE_FOREACH_BRACES_STYLE/@EntryValue">DO_NOT_CHANGE</s:String>
Expand Down Expand Up @@ -477,10 +485,13 @@ II.2.12 &lt;HandlesEvent /&gt;&#xD;
<s:Boolean x:Key="/Default/CodeStyle/Generate/=Global/@KeyIndexDefined">True</s:Boolean>
<s:String x:Key="/Default/CodeStyle/Generate/=Global/Options/=PropertyBody/@EntryIndexedValue">Automatic property</s:String>
<s:Boolean x:Key="/Default/CodeStyle/Generate/=Implementations/@KeyIndexDefined">True</s:Boolean>
<s:String x:Key="/Default/CodeStyle/Generate/=Implementations/Options/=Async/@EntryIndexedValue">False</s:String>
<s:String x:Key="/Default/CodeStyle/Generate/=Implementations/Options/=WrapInRegion/@EntryIndexedValue">False</s:String>
<s:String x:Key="/Default/CodeStyle/Generate/=Implementations/Options/=XmlDocumentation/@EntryIndexedValue">False</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=AD/@EntryIndexedValue">AD</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=DB/@EntryIndexedValue">DB</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=DTC/@EntryIndexedValue">DTC</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=GT/@EntryIndexedValue">GT</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=ID/@EntryIndexedValue">ID</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=NSB/@EntryIndexedValue">NSB</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=SLA/@EntryIndexedValue">SLA</s:String>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
using Serilog.Events;
using Serilog.Parsing;

namespace Serilog.Parameters
namespace Serilog.Capturing
{
partial class PropertyValueConverter
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
using System.Linq;
using System.Reflection;

namespace Serilog.Parameters
namespace Serilog.Capturing
{
static class GetablePropertyFinder
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
using Serilog.Events;
using Serilog.Parsing;

namespace Serilog.Parameters
namespace Serilog.Capturing
{
class MessageTemplateProcessor : ILogEventPropertyFactory
{
readonly IMessageTemplateParser _parser = new MessageTemplateCache(new MessageTemplateParser());
readonly MessageTemplateCache _parser = new MessageTemplateCache(new MessageTemplateParser());
readonly PropertyBinder _propertyBinder;
readonly PropertyValueConverter _propertyValueConverter;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
using Serilog.Events;
using Serilog.Parsing;

namespace Serilog.Parameters
namespace Serilog.Capturing
{
// Performance relevant - on the hot path when creating log events from existing templates.
class PropertyBinder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
using Serilog.Policies;
using System.Runtime.CompilerServices;

namespace Serilog.Parameters
namespace Serilog.Capturing
{
// Values in Serilog are simplified down into a lowest-common-denominator internal
// type system so that there is a better chance of code written with one sink in
Expand Down Expand Up @@ -351,7 +351,7 @@ IEnumerable<LogEventProperty> GetProperties(object value)
if (_propagateExceptions)
throw;

propValue = "The property accessor threw an exception: " + ex.InnerException.GetType().Name;
propValue = "The property accessor threw an exception: " + ex.InnerException?.GetType().Name;
}
yield return new LogEventProperty(prop.Name, _depthLimiter.CreatePropertyValue(propValue, Destructuring.Destructure));
}
Expand Down
1 change: 0 additions & 1 deletion src/Serilog/Context/LogContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
using Serilog.Events;

#if ASYNCLOCAL
using System.Collections.Generic;
using System.Threading;
#elif REMOTING
using System.Runtime.Remoting;
Expand Down
2 changes: 1 addition & 1 deletion src/Serilog/Core/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@

using System;
using System.Collections.Generic;
using Serilog.Capturing;
using Serilog.Core.Enrichers;
using Serilog.Debugging;
using Serilog.Events;
using Serilog.Parameters;

#pragma warning disable Serilog004 // Constant MessageTemplate verifier

Expand Down
6 changes: 5 additions & 1 deletion src/Serilog/Core/Pipeline/MessageTemplateCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@
// limitations under the License.

using System;
using System.Collections.Generic;
using Serilog.Events;

#if HASHTABLE
using System.Collections;
#else
using System.Collections.Generic;
#endif

namespace Serilog.Core.Pipeline
{
Expand Down
4 changes: 2 additions & 2 deletions src/Serilog/Data/LogEventPropertyValueVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
using System;
using Serilog.Events;

// ReSharper disable VirtualMemberNeverOverridden.Global

namespace Serilog.Data
{
/// <summary>
Expand All @@ -40,7 +42,6 @@ public abstract class LogEventPropertyValueVisitor<TState, TResult>
/// <param name="state">Operation state.</param>
/// <param name="value">The value to visit.</param>
/// <returns>The result of visiting <paramref name="value"/>.</returns>
// ReSharper disable once VirtualMemberNeverOverriden.Global
protected virtual TResult Visit(TState state, LogEventPropertyValue value)
{
if (value == null) throw new ArgumentNullException(nameof(value));
Expand Down Expand Up @@ -103,7 +104,6 @@ protected virtual TResult Visit(TState state, LogEventPropertyValue value)
/// <param name="value">The value to visit.</param>
/// <returns>The result of visiting <paramref name="value"/>.</returns>
// ReSharper disable once UnusedParameter.Global
// ReSharper disable once VirtualMemberNeverOverriden.Global
protected virtual TResult VisitUnsupportedValue(TState state, LogEventPropertyValue value)
{
if (value == null) throw new ArgumentNullException(nameof(value));
Expand Down
17 changes: 9 additions & 8 deletions src/Serilog/Events/MessageTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using System.Linq;
using Serilog.Debugging;
using Serilog.Parsing;
using Serilog.Rendering;

namespace Serilog.Events
{
Expand All @@ -35,15 +36,14 @@ public class MessageTemplate

readonly MessageTemplateToken[] _tokens;

// Optimisation for when the template is bound to
// property values.

/// <summary>
/// Construct a message template using manually-defined text and property tokens.
/// </summary>
/// <param name="tokens">The text and property tokens defining the template.</param>
public MessageTemplate(IEnumerable<MessageTemplateToken> tokens)
// ReSharper disable PossibleMultipleEnumeration
: this(string.Join("", tokens), tokens)
// ReSharper enable PossibleMultipleEnumeration
{
}

Expand Down Expand Up @@ -91,7 +91,7 @@ public MessageTemplate(string text, IEnumerable<MessageTemplateToken> tokens)
/// <summary>
/// Similar to <see cref="Enumerable.OfType{TResult}"/>, but faster.
/// </summary>
static TResult[] GetElementsOfTypeToArray<TResult>(object[] tokens)
static TResult[] GetElementsOfTypeToArray<TResult>(MessageTemplateToken[] tokens)
where TResult: class
{
var result = new List<TResult>(tokens.Length / 2);
Expand Down Expand Up @@ -125,6 +125,8 @@ public override string ToString()
/// </summary>
public IEnumerable<MessageTemplateToken> Tokens => _tokens;

internal MessageTemplateToken[] TokenArray => _tokens;

internal PropertyToken[] NamedProperties { get; }

internal PropertyToken[] PositionalProperties { get; }
Expand Down Expand Up @@ -156,10 +158,9 @@ public string Render(IReadOnlyDictionary<string, LogEventPropertyValue> properti
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
public void Render(IReadOnlyDictionary<string, LogEventPropertyValue> properties, TextWriter output, IFormatProvider formatProvider = null)
{
foreach (var token in _tokens)
{
token.Render(properties, output, formatProvider);
}
if (properties == null) throw new ArgumentNullException(nameof(properties));
if (output == null) throw new ArgumentNullException(nameof(output));
MessageTemplateRenderer.Render(this, properties, output, null, formatProvider);
}
}
}
15 changes: 10 additions & 5 deletions src/Serilog/Events/ScalarValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,21 @@ public ScalarValue(object value)
/// <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 override void Render(TextWriter output, string format = null, IFormatProvider formatProvider = null)
{
Render(Value, output, format, formatProvider);
}

internal static void Render(object value, TextWriter output, string format = null, IFormatProvider formatProvider = null)
{
if (output == null) throw new ArgumentNullException(nameof(output));

if (Value == null)
if (value == null)
{
output.Write("null");
return;
}

var s = Value as string;
var s = value as string;
if (s != null)
{
if (format != "l")
Expand All @@ -76,19 +81,19 @@ public override void Render(TextWriter output, string format = null, IFormatProv
var custom = (ICustomFormatter)formatProvider.GetFormat(typeof(ICustomFormatter));
if (custom != null)
{
output.Write(custom.Format(format, Value, formatProvider));
output.Write(custom.Format(format, value, formatProvider));
return;
}
}

var f = Value as IFormattable;
var f = value as IFormattable;
if (f != null)
{
output.Write(f.ToString(format, formatProvider ?? CultureInfo.InvariantCulture));
}
else
{
output.Write(Value.ToString());
output.Write(value.ToString());
}
}

Expand Down
Loading

0 comments on commit e42245c

Please sign in to comment.