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

file scoped namespaces #1734

Merged
merged 1 commit into from Aug 25, 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
81 changes: 40 additions & 41 deletions src/Serilog/Capturing/DepthLimiter.cs
Expand Up @@ -19,63 +19,62 @@
using Serilog.Events;
using Serilog.Parsing;

namespace Serilog.Capturing
namespace Serilog.Capturing;

partial class PropertyValueConverter
{
partial class PropertyValueConverter
class DepthLimiter : ILogEventPropertyValueFactory
{
class DepthLimiter : ILogEventPropertyValueFactory
{
[ThreadStatic]
static int _currentDepth;
[ThreadStatic]
static int _currentDepth;

readonly int _maximumDestructuringDepth;
readonly PropertyValueConverter _propertyValueConverter;
readonly int _maximumDestructuringDepth;
readonly PropertyValueConverter _propertyValueConverter;

public DepthLimiter(int maximumDepth, PropertyValueConverter propertyValueConverter)
{
_maximumDestructuringDepth = maximumDepth;
_propertyValueConverter = propertyValueConverter;
}
public DepthLimiter(int maximumDepth, PropertyValueConverter propertyValueConverter)
{
_maximumDestructuringDepth = maximumDepth;
_propertyValueConverter = propertyValueConverter;
}

public static void SetCurrentDepth(int depth)
{
_currentDepth = depth;
}
public static void SetCurrentDepth(int depth)
{
_currentDepth = depth;
}

public LogEventPropertyValue CreatePropertyValue(object? value, Destructuring destructuring)
{
var storedDepth = _currentDepth;
public LogEventPropertyValue CreatePropertyValue(object? value, Destructuring destructuring)
{
var storedDepth = _currentDepth;

var result = DefaultIfMaximumDepth(storedDepth) ??
_propertyValueConverter.CreatePropertyValue(value, destructuring, storedDepth + 1);
var result = DefaultIfMaximumDepth(storedDepth) ??
_propertyValueConverter.CreatePropertyValue(value, destructuring, storedDepth + 1);

_currentDepth = storedDepth;
_currentDepth = storedDepth;

return result;
}
return result;
}

LogEventPropertyValue ILogEventPropertyValueFactory.CreatePropertyValue(object value, bool destructureObjects)
{
var storedDepth = _currentDepth;
LogEventPropertyValue ILogEventPropertyValueFactory.CreatePropertyValue(object value, bool destructureObjects)
{
var storedDepth = _currentDepth;

var result = DefaultIfMaximumDepth(storedDepth) ??
_propertyValueConverter.CreatePropertyValue(value, destructureObjects, storedDepth + 1);
var result = DefaultIfMaximumDepth(storedDepth) ??
_propertyValueConverter.CreatePropertyValue(value, destructureObjects, storedDepth + 1);

_currentDepth = storedDepth;
_currentDepth = storedDepth;

return result;
}
return result;
}

LogEventPropertyValue? DefaultIfMaximumDepth(int depth)
LogEventPropertyValue? DefaultIfMaximumDepth(int depth)
{
if (depth == _maximumDestructuringDepth)
{
if (depth == _maximumDestructuringDepth)
{
SelfLog.WriteLine("Maximum destructuring depth reached.");
return new ScalarValue(null);
}

return null;
SelfLog.WriteLine("Maximum destructuring depth reached.");
return new ScalarValue(null);
}

return null;
}
}
}
45 changes: 22 additions & 23 deletions src/Serilog/Capturing/GetablePropertyFinder.cs
Expand Up @@ -17,36 +17,35 @@
using System.Linq;
using System.Reflection;

namespace Serilog.Capturing
namespace Serilog.Capturing;

static class GetablePropertyFinder
{
static class GetablePropertyFinder
internal static IEnumerable<PropertyInfo> GetPropertiesRecursive(this Type type)
{
internal static IEnumerable<PropertyInfo> GetPropertiesRecursive(this Type type)
var seenNames = new HashSet<string>();

var currentTypeInfo = type.GetTypeInfo();

while (currentTypeInfo.AsType() != typeof(object))
{
var seenNames = new HashSet<string>();
var unseenProperties = currentTypeInfo.DeclaredProperties.Where(p => p.CanRead &&
p.GetMethod!.IsPublic && !p.GetMethod.IsStatic &&
(p.Name != "Item" || p.GetIndexParameters().Length == 0) && !seenNames.Contains(p.Name));

var currentTypeInfo = type.GetTypeInfo();
foreach (var propertyInfo in unseenProperties)
{
seenNames.Add(propertyInfo.Name);
yield return propertyInfo;
}

while (currentTypeInfo.AsType() != typeof(object))
var baseType = currentTypeInfo.BaseType;
if (baseType == null)
{
var unseenProperties = currentTypeInfo.DeclaredProperties.Where(p => p.CanRead &&
p.GetMethod!.IsPublic && !p.GetMethod.IsStatic &&
(p.Name != "Item" || p.GetIndexParameters().Length == 0) && !seenNames.Contains(p.Name));

foreach (var propertyInfo in unseenProperties)
{
seenNames.Add(propertyInfo.Name);
yield return propertyInfo;
}

var baseType = currentTypeInfo.BaseType;
if (baseType == null)
{
yield break;
}

currentTypeInfo = baseType.GetTypeInfo();
yield break;
}

currentTypeInfo = baseType.GetTypeInfo();
}
}
}
47 changes: 23 additions & 24 deletions src/Serilog/Capturing/MessageTemplateProcessor.cs
Expand Up @@ -17,34 +17,33 @@
using Serilog.Events;
using Serilog.Parsing;

namespace Serilog.Capturing
namespace Serilog.Capturing;

class MessageTemplateProcessor : ILogEventPropertyFactory
{
class MessageTemplateProcessor : ILogEventPropertyFactory
{
readonly MessageTemplateCache _parser = new(new MessageTemplateParser());
readonly PropertyBinder _propertyBinder;
readonly PropertyValueConverter _propertyValueConverter;
readonly MessageTemplateCache _parser = new(new MessageTemplateParser());
readonly PropertyBinder _propertyBinder;
readonly PropertyValueConverter _propertyValueConverter;

public MessageTemplateProcessor(PropertyValueConverter propertyValueConverter)
{
_propertyValueConverter = propertyValueConverter;
_propertyBinder = new(_propertyValueConverter);
}
public MessageTemplateProcessor(PropertyValueConverter propertyValueConverter)
{
_propertyValueConverter = propertyValueConverter;
_propertyBinder = new(_propertyValueConverter);
}

public void Process(string messageTemplate, object?[]? messageTemplateParameters, out MessageTemplate parsedTemplate, out EventProperty[] properties)
{
parsedTemplate = _parser.Parse(messageTemplate);
properties = _propertyBinder.ConstructProperties(parsedTemplate, messageTemplateParameters);
}
public void Process(string messageTemplate, object?[]? messageTemplateParameters, out MessageTemplate parsedTemplate, out EventProperty[] properties)
{
parsedTemplate = _parser.Parse(messageTemplate);
properties = _propertyBinder.ConstructProperties(parsedTemplate, messageTemplateParameters);
}

public LogEventProperty CreateProperty(string name, object? value, bool destructureObjects = false)
{
return _propertyValueConverter.CreateProperty(name, value, destructureObjects);
}
public LogEventProperty CreateProperty(string name, object? value, bool destructureObjects = false)
{
return _propertyValueConverter.CreateProperty(name, value, destructureObjects);
}

public LogEventPropertyValue CreatePropertyValue(object? value, bool destructureObjects = false)
{
return _propertyValueConverter.CreatePropertyValue(value, destructureObjects);
}
public LogEventPropertyValue CreatePropertyValue(object? value, bool destructureObjects = false)
{
return _propertyValueConverter.CreatePropertyValue(value, destructureObjects);
}
}