-
Notifications
You must be signed in to change notification settings - Fork 36
Add NotLoggedIfNull attribute #49
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
6fda17f
Add NotLoggedIfDefault attribute
ydemetriades 98d11a7
Update README.md
ydemetriades 7d44003
Update NotLoggedIfDefaultAttribute
ydemetriades cc0fd2b
Resolve Pull Request comments
ydemetriades 54aae43
Add NotLoggedIfNull attribute and tests
avtc eae6345
Removed breaking change introduced during adding NotLoggedIfDefault a…
avtc c1fc680
review changes
avtc 36abdcc
fix test
avtc ed09e43
Updated readme and added tests for LogMasked on non-string types
avtc bdace93
Revert unnecessary changes
avtc 31358c5
Update AttributedDestructuringPolicyOptions.cs
avtc 1d4b525
Merge remote-tracking branch 'origin/dev' into add-notloggedifnull
avtc 041c659
fix errors and warnings
avtc 149af65
fix release build
avtc 461417c
removed unsupported version compiler directives
avtc 1797bf1
Update src/Destructurama.Attributed/Attributed/LogMaskedAttribute.cs
sungam3r a7044e7
Update src/Destructurama.Attributed/Attributed/AttributedDestructurin…
sungam3r 93633e6
Update test/Destructurama.Attributed.Tests/MaskedAttributeTests.cs
sungam3r 86f4c1a
Update src/Destructurama.Attributed/Attributed/AttributedDestructurin…
sungam3r 1c74da4
Update README.md
sungam3r File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/Destructurama.Attributed/Attributed/IPropertyOptionalIgnoreAttribute.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| // Copyright 2020 Destructurama Contributors, Serilog Contributors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| using System; | ||
|
|
||
| namespace Destructurama.Attributed | ||
| { | ||
| public interface IPropertyOptionalIgnoreAttribute | ||
| { | ||
| bool ShouldPropertyBeIgnored(string name, object value, Type type); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
src/Destructurama.Attributed/Attributed/NotLoggedIfDefaultAttribute.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| // Copyright 2020 Destructurama Contributors, Serilog Contributors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| using System; | ||
| using System.Collections.Concurrent; | ||
| #if NETSTANDARD1_1 | ||
| using System.Reflection; | ||
| #endif | ||
|
|
||
| namespace Destructurama.Attributed | ||
| { | ||
| abstract class CachedValue | ||
| { | ||
| public abstract bool IsDefaultValue(object value); | ||
| } | ||
|
|
||
| class CachedValue<T> : CachedValue | ||
| { | ||
| T Value { get; set; } | ||
|
|
||
| public CachedValue(T value) | ||
| { | ||
| Value = value; | ||
| } | ||
|
|
||
| public override bool IsDefaultValue(object value) | ||
| { | ||
| return Value.Equals(value); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Specified that a property with default value for its type should not be included when destructuring an object for logging. | ||
| /// </summary> | ||
| [AttributeUsage(AttributeTargets.Property)] | ||
| public class NotLoggedIfDefaultAttribute : Attribute, IPropertyOptionalIgnoreAttribute | ||
| { | ||
| readonly static ConcurrentDictionary<Type, CachedValue> _cache = new ConcurrentDictionary<Type, CachedValue>(); | ||
|
|
||
| public bool ShouldPropertyBeIgnored(string name, object value, Type type) | ||
| { | ||
| if (value != null) | ||
| { | ||
|
|
||
| #if NETSTANDARD1_1 | ||
| if (type.GetTypeInfo().IsValueType) | ||
| #else | ||
| if (type.IsValueType) | ||
| #endif | ||
| { | ||
| CachedValue cachedValue; | ||
| if (!_cache.TryGetValue(type, out cachedValue)) | ||
| { | ||
| var cachedValueType = typeof(CachedValue<>).MakeGenericType(type); | ||
| var defaultValue = Activator.CreateInstance(type); | ||
| cachedValue = (CachedValue)Activator.CreateInstance(cachedValueType, defaultValue); | ||
|
|
||
| _cache.TryAdd(type, cachedValue); | ||
| } | ||
|
|
||
| if (cachedValue.IsDefaultValue(value)) | ||
| { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
| } | ||
| } |
30 changes: 30 additions & 0 deletions
30
src/Destructurama.Attributed/Attributed/NotLoggedIfNullAttribute.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| // Copyright 2020 Destructurama Contributors, Serilog Contributors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| using System; | ||
|
|
||
| namespace Destructurama.Attributed | ||
| { | ||
| /// <summary> | ||
| /// Specified that a property with default value for its type should not be included when destructuring an object for logging. | ||
| /// </summary> | ||
| [AttributeUsage(AttributeTargets.Property)] | ||
| public class NotLoggedIfNullAttribute : Attribute, IPropertyOptionalIgnoreAttribute | ||
| { | ||
| public static readonly NotLoggedIfNullAttribute Instance = new NotLoggedIfNullAttribute(); | ||
|
|
||
| public bool ShouldPropertyBeIgnored(string name, object value, Type type) | ||
| => value == null; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.