Skip to content

Commit

Permalink
file scoped namespaces (serilog#1734)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp authored and Twinki14 committed Dec 30, 2023
1 parent 9c3c71e commit c99a97b
Show file tree
Hide file tree
Showing 145 changed files with 15,526 additions and 15,670 deletions.
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);
}
}

0 comments on commit c99a97b

Please sign in to comment.