Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 29 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Apply the `LogWithName` attribute:
public class PersonalData
{
[LogWithName("FullName")]
public string Name { get; set; }
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>
Expand All @@ -49,10 +49,10 @@ Apply the `NotLogged` attribute:
```cs
public class LoginCommand
{
public string Username { get; set; }
public string? Username { get; set; }

[NotLogged]
public string Password { get; set; }
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>
Expand Down Expand Up @@ -98,106 +98,112 @@ public class CustomizedMaskedLogs
/// 123456789 results in "***"
/// </summary>
[LogMasked]
public string DefaultMasked { get; set; }
public string? DefaultMasked { get; set; }

/// <summary>
/// [123456789,123456789,123456789] results in [***,***,***]
/// </summary>
[LogMasked]
public string[] DefaultMaskedArray { get; set; }
public string[]? DefaultMaskedArray { get; set; }

/// <summary>
/// 123456789 results in "*********"
/// </summary>
[LogMasked(PreserveLength = true)]
public string DefaultMaskedPreserved { get; set; }
public string? DefaultMaskedPreserved { get; set; }

/// <summary>
/// 123456789 results in "#"
/// </summary>
[LogMasked(Text = "_REMOVED_")]
public string CustomMasked { get; set; }
public string? CustomMasked { get; set; }

/// <summary>
/// 123456789 results in "#"
/// </summary>
[LogMasked(Text = "")]
public string? CustomMaskedWithEmptyString { get; set; }

/// <summary>
/// 123456789 results in "#########"
/// </summary>
[LogMasked(Text = "#", PreserveLength = true)]
public string CustomMaskedPreservedLength { get; set; }
public string? CustomMaskedPreservedLength { get; set; }

/// <summary>
/// 123456789 results in "123******"
/// </summary>
[LogMasked(ShowFirst = 3)]
public string ShowFirstThreeThenDefaultMasked { get; set; }
public string? ShowFirstThreeThenDefaultMasked { get; set; }

/// <summary>
/// 123456789 results in "123******"
/// </summary>
[LogMasked(ShowFirst = 3, PreserveLength = true)]
public string ShowFirstThreeThenDefaultMaskedPreservedLength { get; set; }
public string? ShowFirstThreeThenDefaultMaskedPreservedLength { get; set; }

/// <summary>
/// 123456789 results in "***789"
/// </summary>
[LogMasked(ShowLast = 3)]
public string ShowLastThreeThenDefaultMasked { get; set; }
public string? ShowLastThreeThenDefaultMasked { get; set; }

/// <summary>
/// 123456789 results in "******789"
/// </summary>
[LogMasked(ShowLast = 3, PreserveLength = true)]
public string ShowLastThreeThenDefaultMaskedPreservedLength { get; set; }
public string? ShowLastThreeThenDefaultMaskedPreservedLength { get; set; }

/// <summary>
/// 123456789 results in "123REMOVED"
/// </summary>
[LogMasked(Text = "_REMOVED_", ShowFirst = 3)]
public string ShowFirstThreeThenCustomMask { get; set; }
public string? ShowFirstThreeThenCustomMask { get; set; }

/// <summary>
/// 123456789 results in "123_REMOVED_"
/// </summary>
[LogMasked(Text = "_REMOVED_", ShowFirst = 3, PreserveLength = true)]
public string ShowFirstThreeThenCustomMaskPreservedLengthIgnored { get; set; }
public string? ShowFirstThreeThenCustomMaskPreservedLengthIgnored { get; set; }

/// <summary>
/// 123456789 results in "_REMOVED_789"
/// </summary>
[LogMasked(Text = "_REMOVED_", ShowLast = 3)]
public string ShowLastThreeThenCustomMask { get; set; }
public string? ShowLastThreeThenCustomMask { get; set; }

/// <summary>
/// 123456789 results in "_REMOVED_789"
/// </summary>
[LogMasked(Text = "_REMOVED_", ShowLast = 3, PreserveLength = true)]
public string ShowLastThreeThenCustomMaskPreservedLengthIgnored { get; set; }
public string? ShowLastThreeThenCustomMaskPreservedLengthIgnored { get; set; }

/// <summary>
/// 123456789 results in "123***789"
/// </summary>
[LogMasked(ShowFirst = 3, ShowLast = 3)]
public string ShowFirstAndLastThreeAndDefaultMaskInTheMiddle { get; set; }
public string? ShowFirstAndLastThreeAndDefaultMaskInTheMiddle { get; set; }

/// <summary>
/// 123456789 results in "123***789"
/// </summary>
[LogMasked(ShowFirst = 3, ShowLast = 3, PreserveLength = true)]
public string ShowFirstAndLastThreeAndDefaultMaskInTheMiddlePreservedLength { get; set; }
public string? ShowFirstAndLastThreeAndDefaultMaskInTheMiddlePreservedLength { get; set; }

/// <summary>
/// 123456789 results in "123_REMOVED_789"
/// </summary>
[LogMasked(Text = "_REMOVED_", ShowFirst = 3, ShowLast = 3)]
public string ShowFirstAndLastThreeAndCustomMaskInTheMiddle { get; set; }
public string? ShowFirstAndLastThreeAndCustomMaskInTheMiddle { get; set; }

/// <summary>
/// 123456789 results in "123_REMOVED_789". PreserveLength is ignored"
/// </summary>
[LogMasked(Text = "_REMOVED_", ShowFirst = 3, ShowLast = 3, PreserveLength = true)]
public string ShowFirstAndLastThreeAndCustomMaskInTheMiddlePreservedLengthIgnored { get; set; }
public string? ShowFirstAndLastThreeAndCustomMaskInTheMiddlePreservedLengthIgnored { get; set; }
}
```
<sup><a href='/test/Destructurama.Attributed.Tests/MaskedAttributeTests.cs#L9-L116' title='Snippet source file'>snippet source</a> | <a href='#snippet-customizedmaskedlogs' title='Start of snippet'>anchor</a></sup>
<sup><a href='/test/Destructurama.Attributed.Tests/MaskedAttributeTests.cs#L9-L122' title='Snippet source file'>snippet source</a> | <a href='#snippet-customizedmaskedlogs' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand Down Expand Up @@ -242,13 +248,13 @@ public class WithRegex
/// 123|456|789 results in "***|456|789"
/// </summary>
[LogReplaced(RegexWithVerticalBars, "***|$2|$3")]
public string RegexReplaceFirst { get; set; }
public string? RegexReplaceFirst { get; set; }

/// <summary>
/// 123|456|789 results in "123|***|789"
/// </summary>
[LogReplaced(RegexWithVerticalBars, "$1|***|$3")]
public string RegexReplaceSecond { get; set; }
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>
Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
version: '{build}'
skip_tags: true
image: Visual Studio 2019
image: Visual Studio 2022
configuration: Release
test: off
build_script:
Expand Down
7 changes: 7 additions & 0 deletions global.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"sdk": {
"version": "7.0.102",
"allowPrerelease": false,
"rollForward": "latestFeature"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using Destructurama.Util;
Expand All @@ -28,7 +29,7 @@ class AttributedDestructuringPolicy : IDestructuringPolicy
{
readonly static ConcurrentDictionary<Type, CacheEntry> _cache = new();

public bool TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory, out LogEventPropertyValue? result)
public bool TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory, [NotNullWhen(true)] out LogEventPropertyValue? result)
{
var cached = _cache.GetOrAdd(value.GetType(), CreateCacheEntry);
result = cached.DestructureFunc(value, propertyValueFactory);
Expand Down Expand Up @@ -63,7 +64,7 @@ static LogEventPropertyValue MakeStructure(object o, IEnumerable<PropertyInfo> l
if (destructuringAttributes.TryGetValue(pi, out var destructuringAttribute))
{
if (destructuringAttribute.TryCreateLogEventProperty(pi.Name, propValue, propertyValueFactory, out var property))
structureProperties.Add(property!);
structureProperties.Add(property);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

using System;
using System.Diagnostics.CodeAnalysis;
using Serilog.Core;
using Serilog.Events;

Expand All @@ -31,6 +32,6 @@ public interface IPropertyDestructuringAttribute
/// <param name="propertyValueFactory">The current <see cref="ILogEventPropertyValueFactory"/>.</param>
/// <param name="property">The <see cref="LogEventProperty"/> to use as a replacement.</param>
/// <returns><code>true</code>If a replacement <see cref="LogEventProperty"/> has been derived.</returns>
bool TryCreateLogEventProperty(string name, object? value, ILogEventPropertyValueFactory propertyValueFactory, out LogEventProperty? property);
bool TryCreateLogEventProperty(string name, object? value, ILogEventPropertyValueFactory propertyValueFactory, [NotNullWhen(true)] out LogEventProperty? property);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public LogEventPropertyValue CreateLogEventPropertyValue(object? value, ILogEven
}

/// <inheritdoc/>
public bool TryCreateLogEventProperty(string name, object? value, ILogEventPropertyValueFactory propertyValueFactory, out LogEventProperty? property)
public bool TryCreateLogEventProperty(string name, object? value, ILogEventPropertyValueFactory propertyValueFactory, out LogEventProperty property)
{
property = new(name, CreateLogEventPropertyValue(value, propertyValueFactory));
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ private object FormatMaskedValue(string val)
}

/// <inheritdoc/>
public bool TryCreateLogEventProperty(string name, object? value, ILogEventPropertyValueFactory propertyValueFactory, out LogEventProperty? property)
public bool TryCreateLogEventProperty(string name, object? value, ILogEventPropertyValueFactory propertyValueFactory, out LogEventProperty property)
{
property = new LogEventProperty(name, CreateValue(value));
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

using System;
using System.Diagnostics.CodeAnalysis;
using System.Text.RegularExpressions;
using Serilog.Core;
using Serilog.Events;
Expand Down Expand Up @@ -50,7 +51,7 @@ public LogReplacedAttribute(string pattern, string replacement)
}

/// <inheritdoc/>
public bool TryCreateLogEventProperty(string name, object? value, ILogEventPropertyValueFactory propertyValueFactory, out LogEventProperty? property)
public bool TryCreateLogEventProperty(string name, object? value, ILogEventPropertyValueFactory propertyValueFactory, [NotNullWhen(true)] out LogEventProperty? property)
{
if (value == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Serilog.Core;
using Serilog.Events;
using System;
using System.Diagnostics.CodeAnalysis;

namespace Destructurama.Attributed
{
Expand All @@ -36,7 +37,7 @@ public LogWithNameAttribute(string newName)
}

/// <inheritdoc/>
public bool TryCreateLogEventProperty(string name, object? value, ILogEventPropertyValueFactory propertyValueFactory, out LogEventProperty? property)
public bool TryCreateLogEventProperty(string name, object? value, ILogEventPropertyValueFactory propertyValueFactory, [NotNullWhen(true)] out LogEventProperty? property)
{
var propValue = propertyValueFactory.CreatePropertyValue(value);

Expand All @@ -49,6 +50,12 @@ public bool TryCreateLogEventProperty(string name, object? value, ILogEventPrope
_ => null
};

if (logEventPropVal is null)
{
property = null;
return false;
}

property = new LogEventProperty(_newName, logEventPropVal);
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

using System;
using System.Diagnostics.CodeAnalysis;
using Serilog.Core;
using Serilog.Events;

Expand All @@ -25,7 +26,7 @@ namespace Destructurama.Attributed
public class NotLoggedAttribute : Attribute, IPropertyDestructuringAttribute
{
/// <inheritdoc/>
public bool TryCreateLogEventProperty(string name, object? value, ILogEventPropertyValueFactory propertyValueFactory, out LogEventProperty? property)
public bool TryCreateLogEventProperty(string name, object? value, ILogEventPropertyValueFactory propertyValueFactory, [NotNullWhen(true)] out LogEventProperty? property)
{
property = null;
return false;
Expand Down
11 changes: 4 additions & 7 deletions src/Destructurama.Attributed/Destructurama.Attributed.csproj
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard1.1</TargetFrameworks>
<VersionPrefix>3.0.0</VersionPrefix>
<PackageTargetFallback Condition="'$(TargetFramework)' == 'netstandard1.1'">$(PackageTargetFallback);dnxcore50</PackageTargetFallback>
<TargetFramework>netstandard2.0</TargetFramework>
<VersionPrefix>3.1.0</VersionPrefix>
<RootNamespace>Destructurama</RootNamespace>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Authors>Serilog Contributors</Authors>
Expand All @@ -13,16 +12,14 @@
<PackageIcon>icon.png</PackageIcon>
<PackageTags>serilog;attributed</PackageTags>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<!-- Don't reference the full NETStandard.Library -->
<DisableImplicitFrameworkReferences Condition="'$(TargetFramework)' == 'netstandard1.1'">true</DisableImplicitFrameworkReferences>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" Condition="$(Configuration) == 'Release'" />
<PackageReference Condition="'$(TargetFramework)' == 'netstandard1.1'" Include="Serilog" Version="2.*" />
<PackageReference Condition="'$(TargetFramework)' == 'netstandard2.0'" Include="Serilog" Version="[2.10.0,3.0.0)" />
<PackageReference Include="Serilog" Version="[2.10.0,4.0.0)" />
<None Include="..\..\assets\icon.png" Pack="true" PackagePath="" />
<PackageReference Include="Polyfill" Version="1.23.0" PrivateAssets="all" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="NUnit" Version="3.13.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.0.0" />
<PackageReference Include="Serilog" Version="2.10.0" />
<PackageReference Include="Serilog" Version="3.0.1" />
<ProjectReference Include="..\..\src\Destructurama.Attributed\Destructurama.Attributed.csproj" />
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion test/Destructurama.Attributed.Tests/Support/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Destructurama.Attributed.Tests.Support
{
public static class Extensions
{
public static object LiteralValue(this LogEventPropertyValue @this)
public static object? LiteralValue(this LogEventPropertyValue @this)
{
return ((ScalarValue)@this).Value;
}
Expand Down