-
-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathDataDictionaryExtensions.cs
More file actions
144 lines (123 loc) · 6.44 KB
/
Copy pathDataDictionaryExtensions.cs
File metadata and controls
144 lines (123 loc) · 6.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using System;
using System.Collections.Generic;
using System.Linq;
using Exceptionless.Dependency;
using Exceptionless.Extensions;
using Exceptionless.Models;
namespace Exceptionless {
public static class DataDictionaryExtensions {
public static T GetValue<T>(this DataDictionary items, string key, IJsonSerializer serializer = null) {
if (items == null)
throw new ArgumentNullException(nameof(items));
if (String.IsNullOrEmpty(key))
throw new ArgumentNullException(nameof(key));
if (!items.TryGetValue(key, out object data))
throw new KeyNotFoundException($"The key '{key}' was not found.");
if (data == null || data is T)
return (T)data;
var json = data as string ?? data.ToString();
serializer = serializer ?? DependencyResolver.Default.GetJsonSerializer();
return serializer.Deserialize<T>(json);
}
/// <summary>
/// Sets a property value on the extended data.
/// </summary>
/// <param name="target">The target object.</param>
/// <param name="name">The name of the property to add.</param>
/// <param name="value">The property value to add.</param>
/// <param name="maxDepth">The max depth of the object to include. Used when the property value is an object.</param>
/// <param name="excludedPropertyNames">Any property names that should be excluded in complex object values.</param>
/// <param name="ignoreSerializationErrors">Specifies whether complex object properties that throw errors while serializing be ignored</param>
/// <param name="client">
/// The ExceptionlessClient instance used for configuration. If a client is not specified, it will use
/// ExceptionlessClient.Default.
/// </param>
public static void SetProperty(this IData target, string name, object value, int? maxDepth = null, IEnumerable<string> excludedPropertyNames = null, bool ignoreSerializationErrors = false, ExceptionlessClient client = null) {
if (String.IsNullOrEmpty(name))
throw new ArgumentNullException(nameof(name));
AddObject(target, value, name, maxDepth, excludedPropertyNames, ignoreSerializationErrors, client);
}
/// <summary>
/// Adds the object to extended data.
/// </summary>
/// <param name="data">The error.</param>
/// <param name="value">The data object to add.</param>
/// <param name="name">The name of the object to add. If not specified, the name will be implied from the object type.</param>
/// <param name="maxDepth">The max depth of the object to include.</param>
/// <param name="excludedPropertyNames">Any property names that should be excluded</param>
/// <param name="ignoreSerializationErrors">Specifies whether properties that throw errors while serializing be ignored</param>
/// <param name="client">
/// The ExceptionlessClient instance used for configuration. If a client is not specified, it will use
/// ExceptionlessClient.Default.
/// </param>
public static void AddObject(this IData data, object value, string name = null, int? maxDepth = null, IEnumerable<string> excludedPropertyNames = null, bool ignoreSerializationErrors = false, ExceptionlessClient client = null) {
if (client == null)
client = ExceptionlessClient.Default;
if (value == null)
return;
ExtendedDataInfo info;
if (value is ExtendedDataInfo)
info = value as ExtendedDataInfo;
else {
info = new ExtendedDataInfo {
Data = value,
Name = name,
MaxDepthToSerialize = maxDepth,
IgnoreSerializationErrors = ignoreSerializationErrors
};
if (excludedPropertyNames != null)
info.ExcludedPropertyNames.AddRange(excludedPropertyNames);
}
AddObject(data, info, client);
}
/// <summary>
/// Adds the object to extended data.
/// </summary>
/// <param name="data">The error to add the object to.</param>
/// <param name="info">The data object to add.</param>
/// <param name="client">
/// The ExceptionlessClient instance used for configuration. If a client is not specified, it will use
/// ExceptionlessClient.Default.
/// </param>
public static void AddObject(this IData data, ExtendedDataInfo info, ExceptionlessClient client = null) {
if (client == null)
client = ExceptionlessClient.Default;
if (info == null || info.Data == null)
return;
string[] exclusions = info.ExcludedPropertyNames != null
? client.Configuration.DataExclusions.Union(info.ExcludedPropertyNames).ToArray()
: client.Configuration.DataExclusions.ToArray();
string name = !String.IsNullOrWhiteSpace(info.Name) ? info.Name.Trim() : null;
var dataType = info.Data.GetType();
if (String.IsNullOrEmpty(name)) {
name = dataType.Name;
int index = 1;
while (data.Data.ContainsKey(name))
name = dataType.Name + index++;
} else if (exclusions.Length > 0 && name.AnyWildcardMatches(exclusions, true)) {
return;
}
if (dataType == typeof(bool) || dataType == typeof(string) || dataType.IsNumeric()) {
data.Data[name] = info.Data;
return;
}
string json;
try {
if (dataType.IsPrimitiveType()) {
json = info.Data.ToString();
} else {
var serializer = client.Configuration.Resolver.GetJsonSerializer();
json = serializer.Serialize(info.Data, exclusions, info.MaxDepthToSerialize.GetValueOrDefault(5), info.IgnoreSerializationErrors);
}
} catch (Exception ex) {
json = ex.ToString();
}
if (String.IsNullOrEmpty(json))
return;
if (dataType.IsPrimitiveType())
data.Data[name] = json;
else
data.Data.SetRawJson(name, json);
}
}
}