diff --git a/README.md b/README.md index 404ebef..1fad7dd 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ Apply the `LogWithName` attribute: public class PersonalData { [LogWithName("FullName")] - public string Name { get; set; } + public string? Name { get; set; } } ``` snippet source | anchor @@ -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; } } ``` snippet source | anchor @@ -98,106 +98,112 @@ public class CustomizedMaskedLogs /// 123456789 results in "***" /// [LogMasked] - public string DefaultMasked { get; set; } + public string? DefaultMasked { get; set; } /// /// [123456789,123456789,123456789] results in [***,***,***] /// [LogMasked] - public string[] DefaultMaskedArray { get; set; } + public string[]? DefaultMaskedArray { get; set; } /// /// 123456789 results in "*********" /// [LogMasked(PreserveLength = true)] - public string DefaultMaskedPreserved { get; set; } + public string? DefaultMaskedPreserved { get; set; } /// /// 123456789 results in "#" /// [LogMasked(Text = "_REMOVED_")] - public string CustomMasked { get; set; } + public string? CustomMasked { get; set; } + + /// + /// 123456789 results in "#" + /// + [LogMasked(Text = "")] + public string? CustomMaskedWithEmptyString { get; set; } /// /// 123456789 results in "#########" /// [LogMasked(Text = "#", PreserveLength = true)] - public string CustomMaskedPreservedLength { get; set; } + public string? CustomMaskedPreservedLength { get; set; } /// /// 123456789 results in "123******" /// [LogMasked(ShowFirst = 3)] - public string ShowFirstThreeThenDefaultMasked { get; set; } + public string? ShowFirstThreeThenDefaultMasked { get; set; } /// /// 123456789 results in "123******" /// [LogMasked(ShowFirst = 3, PreserveLength = true)] - public string ShowFirstThreeThenDefaultMaskedPreservedLength { get; set; } + public string? ShowFirstThreeThenDefaultMaskedPreservedLength { get; set; } /// /// 123456789 results in "***789" /// [LogMasked(ShowLast = 3)] - public string ShowLastThreeThenDefaultMasked { get; set; } + public string? ShowLastThreeThenDefaultMasked { get; set; } /// /// 123456789 results in "******789" /// [LogMasked(ShowLast = 3, PreserveLength = true)] - public string ShowLastThreeThenDefaultMaskedPreservedLength { get; set; } + public string? ShowLastThreeThenDefaultMaskedPreservedLength { get; set; } /// /// 123456789 results in "123REMOVED" /// [LogMasked(Text = "_REMOVED_", ShowFirst = 3)] - public string ShowFirstThreeThenCustomMask { get; set; } + public string? ShowFirstThreeThenCustomMask { get; set; } /// /// 123456789 results in "123_REMOVED_" /// [LogMasked(Text = "_REMOVED_", ShowFirst = 3, PreserveLength = true)] - public string ShowFirstThreeThenCustomMaskPreservedLengthIgnored { get; set; } + public string? ShowFirstThreeThenCustomMaskPreservedLengthIgnored { get; set; } /// /// 123456789 results in "_REMOVED_789" /// [LogMasked(Text = "_REMOVED_", ShowLast = 3)] - public string ShowLastThreeThenCustomMask { get; set; } + public string? ShowLastThreeThenCustomMask { get; set; } /// /// 123456789 results in "_REMOVED_789" /// [LogMasked(Text = "_REMOVED_", ShowLast = 3, PreserveLength = true)] - public string ShowLastThreeThenCustomMaskPreservedLengthIgnored { get; set; } + public string? ShowLastThreeThenCustomMaskPreservedLengthIgnored { get; set; } /// /// 123456789 results in "123***789" /// [LogMasked(ShowFirst = 3, ShowLast = 3)] - public string ShowFirstAndLastThreeAndDefaultMaskInTheMiddle { get; set; } + public string? ShowFirstAndLastThreeAndDefaultMaskInTheMiddle { get; set; } /// /// 123456789 results in "123***789" /// [LogMasked(ShowFirst = 3, ShowLast = 3, PreserveLength = true)] - public string ShowFirstAndLastThreeAndDefaultMaskInTheMiddlePreservedLength { get; set; } + public string? ShowFirstAndLastThreeAndDefaultMaskInTheMiddlePreservedLength { get; set; } /// /// 123456789 results in "123_REMOVED_789" /// [LogMasked(Text = "_REMOVED_", ShowFirst = 3, ShowLast = 3)] - public string ShowFirstAndLastThreeAndCustomMaskInTheMiddle { get; set; } + public string? ShowFirstAndLastThreeAndCustomMaskInTheMiddle { get; set; } /// /// 123456789 results in "123_REMOVED_789". PreserveLength is ignored" /// [LogMasked(Text = "_REMOVED_", ShowFirst = 3, ShowLast = 3, PreserveLength = true)] - public string ShowFirstAndLastThreeAndCustomMaskInTheMiddlePreservedLengthIgnored { get; set; } + public string? ShowFirstAndLastThreeAndCustomMaskInTheMiddlePreservedLengthIgnored { get; set; } } ``` -snippet source | anchor +snippet source | anchor @@ -242,13 +248,13 @@ public class WithRegex /// 123|456|789 results in "***|456|789" /// [LogReplaced(RegexWithVerticalBars, "***|$2|$3")] - public string RegexReplaceFirst { get; set; } + public string? RegexReplaceFirst { get; set; } /// /// 123|456|789 results in "123|***|789" /// [LogReplaced(RegexWithVerticalBars, "$1|***|$3")] - public string RegexReplaceSecond { get; set; } + public string? RegexReplaceSecond { get; set; } } ``` snippet source | anchor diff --git a/appveyor.yml b/appveyor.yml index 7d469e2..9f41cd6 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,6 +1,6 @@ version: '{build}' skip_tags: true -image: Visual Studio 2019 +image: Visual Studio 2022 configuration: Release test: off build_script: diff --git a/global.json b/global.json new file mode 100644 index 0000000..9acdfa6 --- /dev/null +++ b/global.json @@ -0,0 +1,7 @@ +{ + "sdk": { + "version": "7.0.102", + "allowPrerelease": false, + "rollForward": "latestFeature" + } +} diff --git a/src/Destructurama.Attributed/Attributed/AttributedDestructuringPolicy.cs b/src/Destructurama.Attributed/Attributed/AttributedDestructuringPolicy.cs index f76acd1..6fa89d0 100644 --- a/src/Destructurama.Attributed/Attributed/AttributedDestructuringPolicy.cs +++ b/src/Destructurama.Attributed/Attributed/AttributedDestructuringPolicy.cs @@ -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; @@ -28,7 +29,7 @@ class AttributedDestructuringPolicy : IDestructuringPolicy { readonly static ConcurrentDictionary _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); @@ -63,7 +64,7 @@ static LogEventPropertyValue MakeStructure(object o, IEnumerable 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 { diff --git a/src/Destructurama.Attributed/Attributed/IPropertyDestructuringAttribute.cs b/src/Destructurama.Attributed/Attributed/IPropertyDestructuringAttribute.cs index 1f99e38..1302bc5 100644 --- a/src/Destructurama.Attributed/Attributed/IPropertyDestructuringAttribute.cs +++ b/src/Destructurama.Attributed/Attributed/IPropertyDestructuringAttribute.cs @@ -13,6 +13,7 @@ // limitations under the License. using System; +using System.Diagnostics.CodeAnalysis; using Serilog.Core; using Serilog.Events; @@ -31,6 +32,6 @@ public interface IPropertyDestructuringAttribute /// The current . /// The to use as a replacement. /// trueIf a replacement has been derived. - bool TryCreateLogEventProperty(string name, object? value, ILogEventPropertyValueFactory propertyValueFactory, out LogEventProperty? property); + bool TryCreateLogEventProperty(string name, object? value, ILogEventPropertyValueFactory propertyValueFactory, [NotNullWhen(true)] out LogEventProperty? property); } } \ No newline at end of file diff --git a/src/Destructurama.Attributed/Attributed/LogAsScalarAttribute.cs b/src/Destructurama.Attributed/Attributed/LogAsScalarAttribute.cs index 3ed149d..745d64f 100644 --- a/src/Destructurama.Attributed/Attributed/LogAsScalarAttribute.cs +++ b/src/Destructurama.Attributed/Attributed/LogAsScalarAttribute.cs @@ -45,7 +45,7 @@ public LogEventPropertyValue CreateLogEventPropertyValue(object? value, ILogEven } /// - 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; diff --git a/src/Destructurama.Attributed/Attributed/LogMaskedAttribute.cs b/src/Destructurama.Attributed/Attributed/LogMaskedAttribute.cs index 45c4517..a9af047 100644 --- a/src/Destructurama.Attributed/Attributed/LogMaskedAttribute.cs +++ b/src/Destructurama.Attributed/Attributed/LogMaskedAttribute.cs @@ -112,7 +112,7 @@ private object FormatMaskedValue(string val) } /// - 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; diff --git a/src/Destructurama.Attributed/Attributed/LogReplacedAttribute.cs b/src/Destructurama.Attributed/Attributed/LogReplacedAttribute.cs index 0c339a0..645a03f 100644 --- a/src/Destructurama.Attributed/Attributed/LogReplacedAttribute.cs +++ b/src/Destructurama.Attributed/Attributed/LogReplacedAttribute.cs @@ -13,6 +13,7 @@ // limitations under the License. using System; +using System.Diagnostics.CodeAnalysis; using System.Text.RegularExpressions; using Serilog.Core; using Serilog.Events; @@ -50,7 +51,7 @@ public LogReplacedAttribute(string pattern, string replacement) } /// - 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) { diff --git a/src/Destructurama.Attributed/Attributed/LogWithNameAttribute.cs b/src/Destructurama.Attributed/Attributed/LogWithNameAttribute.cs index cf30a83..85fabf0 100644 --- a/src/Destructurama.Attributed/Attributed/LogWithNameAttribute.cs +++ b/src/Destructurama.Attributed/Attributed/LogWithNameAttribute.cs @@ -15,6 +15,7 @@ using Serilog.Core; using Serilog.Events; using System; +using System.Diagnostics.CodeAnalysis; namespace Destructurama.Attributed { @@ -36,7 +37,7 @@ public LogWithNameAttribute(string newName) } /// - 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); @@ -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; } diff --git a/src/Destructurama.Attributed/Attributed/NotLoggedAttribute.cs b/src/Destructurama.Attributed/Attributed/NotLoggedAttribute.cs index c015ae3..d0439b2 100644 --- a/src/Destructurama.Attributed/Attributed/NotLoggedAttribute.cs +++ b/src/Destructurama.Attributed/Attributed/NotLoggedAttribute.cs @@ -13,6 +13,7 @@ // limitations under the License. using System; +using System.Diagnostics.CodeAnalysis; using Serilog.Core; using Serilog.Events; @@ -25,7 +26,7 @@ namespace Destructurama.Attributed public class NotLoggedAttribute : Attribute, IPropertyDestructuringAttribute { /// - 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; diff --git a/src/Destructurama.Attributed/Destructurama.Attributed.csproj b/src/Destructurama.Attributed/Destructurama.Attributed.csproj index 9ef5555..0eb5fcf 100644 --- a/src/Destructurama.Attributed/Destructurama.Attributed.csproj +++ b/src/Destructurama.Attributed/Destructurama.Attributed.csproj @@ -1,9 +1,8 @@  - netstandard2.0;netstandard1.1 - 3.0.0 - $(PackageTargetFallback);dnxcore50 + netstandard2.0 + 3.1.0 Destructurama True Serilog Contributors @@ -13,16 +12,14 @@ icon.png serilog;attributed true - - true true - - + + diff --git a/test/Destructurama.Attributed.Tests/Destructurama.Attributed.Tests.csproj b/test/Destructurama.Attributed.Tests/Destructurama.Attributed.Tests.csproj index 4b2cb65..cfea821 100644 --- a/test/Destructurama.Attributed.Tests/Destructurama.Attributed.Tests.csproj +++ b/test/Destructurama.Attributed.Tests/Destructurama.Attributed.Tests.csproj @@ -8,7 +8,7 @@ - + diff --git a/test/Destructurama.Attributed.Tests/Support/Extensions.cs b/test/Destructurama.Attributed.Tests/Support/Extensions.cs index 1f5db9c..9cb112a 100644 --- a/test/Destructurama.Attributed.Tests/Support/Extensions.cs +++ b/test/Destructurama.Attributed.Tests/Support/Extensions.cs @@ -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; }