Skip to content

Commit

Permalink
Merge branch 'master' into typeinfo
Browse files Browse the repository at this point in the history
  • Loading branch information
sungam3r committed Jan 24, 2024
2 parents 366f716 + 6adae75 commit 215d744
Show file tree
Hide file tree
Showing 15 changed files with 506 additions and 321 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/benchmarks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:
tool: 'benchmarkdotnet'
output-file-path: src/Benchmarks/BenchmarkDotNet.Artifacts/results/Combined.Benchmarks.json
github-token: ${{ secrets.GITHUB_TOKEN }}
alert-threshold: '101%'
alert-threshold: '110%'
comment-on-alert: true
- name: Push benchmarks results
if: github.event_name != 'pull_request'
Expand Down
32 changes: 15 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,14 @@ Apply the `LogWithName` attribute:

<!-- snippet: LogWithName -->
<a id='snippet-logwithname'></a>
```csharp
using Destructurama.Attributed;
...
```cs
public class PersonalData
{
[LogWithName("FullName")]
public string? Name { get; set; }
}
```
<sup><a href='/test/Destructurama.Attributed.Tests/LogWithNameAttributedTests.cs#L37-L43' title='Snippet source file'>snippet source</a> | <a href='#snippet-logwithname' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Destructurama.Attributed.Tests/LogWithNameAttributedTests.cs#L36-L42' title='Snippet source file'>snippet source</a> | <a href='#snippet-logwithname' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

## 2. Ignoring a property
Expand All @@ -66,9 +64,7 @@ Apply the `NotLogged` attribute:

<!-- snippet: LoginCommand -->
<a id='snippet-logincommand'></a>
```csharp
using Destructurama.Attributed;
...
```cs
public class LoginCommand
{
public string? Username { get; set; }
Expand All @@ -77,18 +73,18 @@ public class LoginCommand
public string? Password { get; set; }
}
```
<sup><a href='/test/Destructurama.Attributed.Tests/Snippets.cs#L29-L37' title='Snippet source file'>snippet source</a> | <a href='#snippet-logincommand' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Destructurama.Attributed.Tests/Snippets.cs#L29-L37' title='Snippet source file'>snippet source</a> | <a href='#snippet-logincommand' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

When the object is passed using `{@...}` syntax the attributes will be consulted.

<!-- snippet: LogCommand -->
<a id='snippet-logcommand'></a>
```csharp
```cs
var command = new LoginCommand { Username = "logged", Password = "not logged" };
log.Information("Logging in {@Command}", command);
_log.Information("Logging in {@Command}", command);
```
<sup><a href='/test/Destructurama.Attributed.Tests/Snippets.cs#L44-L47' title='Snippet source file'>snippet source</a> | <a href='#snippet-logcommand' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Destructurama.Attributed.Tests/Snippets.cs#L44-L47' title='Snippet source file'>snippet source</a> | <a href='#snippet-logcommand' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

## 3. Ignoring a property if it has default value
Expand Down Expand Up @@ -164,9 +160,7 @@ Note that masking also works for properties of type `IEnumerable<string>` or der

<!-- snippet: CustomizedMaskedLogs -->
<a id='snippet-customizedmaskedlogs'></a>
```csharp
using Destructurama.Attributed;
...
```cs
public class CustomizedMaskedLogs
{
/// <summary>
Expand Down Expand Up @@ -284,7 +278,7 @@ public class CustomizedMaskedLogs
public string? ShowFirstAndLastThreeAndCustomMaskInTheMiddlePreservedLengthIgnored { get; set; }
}
```
<sup><a href='/test/Destructurama.Attributed.Tests/MaskedAttributeTests.cs#L10-L129' title='Snippet source file'>snippet source</a> | <a href='#snippet-customizedmaskedlogs' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Destructurama.Attributed.Tests/MaskedAttributeTests.cs#L9-L128' title='Snippet source file'>snippet source</a> | <a href='#snippet-customizedmaskedlogs' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

## 7. Masking a string property with regular expressions
Expand Down Expand Up @@ -319,7 +313,7 @@ __Available properties__:

<!-- snippet: WithRegex -->
<a id='snippet-withregex'></a>
```csharp
```cs
public class WithRegex
{
private const string REGEX_WITH_VERTICAL_BARS = @"([a-zA-Z0-9]+)\|([a-zA-Z0-9]+)\|([a-zA-Z0-9]+)";
Expand All @@ -337,5 +331,9 @@ public class WithRegex
public string? RegexReplaceSecond { get; set; }
}
```
<sup><a href='/test/Destructurama.Attributed.Tests/Snippets.cs#L6-L25' title='Snippet source file'>snippet source</a> | <a href='#snippet-withregex' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Destructurama.Attributed.Tests/Snippets.cs#L6-L25' title='Snippet source file'>snippet source</a> | <a href='#snippet-withregex' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

# Benchmarks

The results are available [here](https://destructurama.github.io/attributed/dev/bench/).
4 changes: 2 additions & 2 deletions mdsnippets.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
{
"$schema": "https://raw.githubusercontent.com/SimonCropp/MarkdownSnippets/master/schema.json",
"MaxWidth": 100,
"MaxWidth": 110,
"Convention": "InPlaceOverwrite"
}
177 changes: 174 additions & 3 deletions src/Benchmarks/AttributedBenchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,190 @@
// limitations under the License.

using BenchmarkDotNet.Attributes;
using Destructurama;
using Destructurama.Attributed;
using Serilog;
using Serilog.Core;

namespace Benchmarks;

public class AttributedBenchmarks
{
private class LogAsScalarClass
{
[LogAsScalar]
public string? Name { get; set; }

[LogAsScalar]
public LogAsScalarClass? Inner { get; set; }

[LogAsScalar(isMutable: true)]
public LogAsScalarClass? Inner2 { get; set; }
}

private class LogMaskedClass
{
[LogMasked]
public string? Password1 { get; set; }

[LogMasked(ShowFirst = 3)]
public string? Password2 { get; set; }

[LogMasked(ShowLast = 3)]
public string? Password3 { get; set; }

[LogMasked(ShowFirst = 3, ShowLast = 3)]
public string? Password4 { get; set; }
}

private class LogReplacedClass
{
private const string REGEX_WITH_VERTICAL_BARS = @"([a-zA-Z0-9]+)\|([a-zA-Z0-9]+)\|([a-zA-Z0-9]+)";

/// <summary>
/// 123|456|789 results in "***|456|789"
/// </summary>
[LogReplaced(REGEX_WITH_VERTICAL_BARS, "***|$2|$3")]
public string? RegexReplaceFirst { get; set; }
}

private class LogWithNameClass
{
[LogWithName("OtherName1")]
public string? Name { get; set; }

[LogWithName("OtherName2")]
public LogWithNameClass? Inner { get; set; }
}

private class NotLoggedClass
{
[NotLogged]
public string? Name { get; set; }

[NotLogged]
public NotLoggedClass? Inner { get; set; }
}

private class NotLoggedIfDefaultClass
{
[NotLoggedIfDefault]
public string? Name { get; set; }

[NotLoggedIfDefault]
public int Age { get; set; }
}

private class NotLoggedIfNullClass
{
[NotLoggedIfNull]
public string? Name { get; set; }

[NotLoggedIfNull]
public int Age { get; set; }
}

private readonly LogAsScalarClass _logAsScalar = new()
{
Name = "Tom",
Inner = new LogAsScalarClass(),
Inner2 = new LogAsScalarClass(),
};

private readonly LogMaskedClass _logMasked = new()
{
Password1 = "abcdef123456",
Password2 = "abcdef123456",
Password3 = "abcdef123456",
Password4 = "abcdef123456",
};

private readonly LogReplacedClass _logReplaced = new()
{
RegexReplaceFirst = "123|456|789",
};

private readonly LogWithNameClass _logWithName = new()
{
Name = "Tome",
Inner = new LogWithNameClass(),
};

private readonly NotLoggedClass _notLogged = new()
{
Name = "Tom",
Inner = new NotLoggedClass(),
};

private readonly NotLoggedIfDefaultClass _notLoggedIfDefault = new()
{
};

private readonly NotLoggedIfNullClass _notLoggedIfNull = new()
{
};

private ILogEventPropertyValueFactory _factory = null!;
private IDestructuringPolicy _policy = null!;

[GlobalSetup]
public void Setup()
{
(_policy, _factory) = Build(c => c.Destructure.UsingAttributes());
}

private static (IDestructuringPolicy, ILogEventPropertyValueFactory) Build(Func<LoggerConfiguration, LoggerConfiguration> configure)
{
var configuration = new LoggerConfiguration();
var logger = configure(configuration).CreateLogger();

var processor = logger.GetType().GetField("_messageTemplateProcessor", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)!.GetValue(logger)!;
var converter = processor.GetType().GetField("_propertyValueConverter", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)!.GetValue(processor)!;
var factory = (ILogEventPropertyValueFactory)converter;
var policies = (IDestructuringPolicy[])converter.GetType().GetField("_destructuringPolicies", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)!.GetValue(converter)!;
var policy = policies.First(p => p is AttributedDestructuringPolicy);
return (policy, factory);
}

[Benchmark]
public void LogAsScalar()
{
_policy.TryDestructure(_logAsScalar, _factory, out _);
}

[Benchmark]
public void LogMasked()
{
_policy.TryDestructure(_logMasked, _factory, out _);
}

[Benchmark]
public void LogReplaced()
{
_policy.TryDestructure(_logReplaced, _factory, out _);
}

[Benchmark]
public void LogWithName()
{
_policy.TryDestructure(_logWithName, _factory, out _);
}

[Benchmark]
public void NotLogged()
{
_policy.TryDestructure(_notLogged, _factory, out _);
}

[Benchmark]
public void NotLoggedIfDefault()
{
_policy.TryDestructure(_notLoggedIfDefault, _factory, out _);
}

//[Benchmark]
public void Execute()
[Benchmark]
public void NotLoggedIfNull()
{
//TODO: implement
_policy.TryDestructure(_notLoggedIfNull, _factory, out _);
}
}
2 changes: 0 additions & 2 deletions src/Benchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Diagnosers;
using BenchmarkDotNet.Running;
using Benchmarks;

new AttributedBenchmarks().Setup();
var config = ManualConfig
.Create(DefaultConfig.Instance)
.AddDiagnoser(MemoryDiagnoser.Default);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Destructurama.Attributed
{
Serilog.Events.LogEventPropertyValue CreateLogEventPropertyValue(object? value, Serilog.Core.ILogEventPropertyValueFactory propertyValueFactory);
}
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Property)]
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Struct | System.AttributeTargets.Property)]
public class LogAsScalarAttribute : System.Attribute, Destructurama.Attributed.IPropertyDestructuringAttribute, Destructurama.Attributed.ITypeDestructuringAttribute
{
public LogAsScalarAttribute(bool isMutable = false) { }
Expand Down
Loading

0 comments on commit 215d744

Please sign in to comment.