Skip to content

Commit 0002bd8

Browse files
committed
Added helper extension method for getting extended data values.
1 parent cd818a4 commit 0002bd8

File tree

1 file changed

+17
-48
lines changed

1 file changed

+17
-48
lines changed

Source/Shared/Extensions/EventExtensions.cs

Lines changed: 17 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -6,47 +6,19 @@
66
namespace Exceptionless {
77
public static class EventExtensions {
88
public static Error GetError(this Event ev, IJsonSerializer serializer = null) {
9-
if (ev == null || !ev.Data.ContainsKey(Event.KnownDataKeys.Error))
10-
return null;
11-
12-
try {
13-
return ev.Data.GetValue<Error>(Event.KnownDataKeys.Error, serializer);
14-
} catch (Exception) {}
15-
16-
return null;
9+
return ev.GetDataValue<Error>(Event.KnownDataKeys.Error, serializer);
1710
}
1811

1912
public static SimpleError GetSimpleError(this Event ev, IJsonSerializer serializer = null) {
20-
if (ev == null || !ev.Data.ContainsKey(Event.KnownDataKeys.SimpleError))
21-
return null;
22-
23-
try {
24-
return ev.Data.GetValue<SimpleError>(Event.KnownDataKeys.SimpleError, serializer);
25-
} catch (Exception) {}
26-
27-
return null;
13+
return ev.GetDataValue<SimpleError>(Event.KnownDataKeys.SimpleError, serializer);
2814
}
2915

3016
public static RequestInfo GetRequestInfo(this Event ev, IJsonSerializer serializer = null) {
31-
if (ev == null || !ev.Data.ContainsKey(Event.KnownDataKeys.RequestInfo))
32-
return null;
33-
34-
try {
35-
return ev.Data.GetValue<RequestInfo>(Event.KnownDataKeys.RequestInfo, serializer);
36-
} catch (Exception) {}
37-
38-
return null;
17+
return ev.GetDataValue<RequestInfo>(Event.KnownDataKeys.RequestInfo, serializer);
3918
}
4019

4120
public static EnvironmentInfo GetEnvironmentInfo(this Event ev, IJsonSerializer serializer = null) {
42-
if (ev == null || !ev.Data.ContainsKey(Event.KnownDataKeys.EnvironmentInfo))
43-
return null;
44-
45-
try {
46-
return ev.Data.GetValue<EnvironmentInfo>(Event.KnownDataKeys.EnvironmentInfo, serializer);
47-
} catch (Exception) {}
48-
49-
return null;
21+
return ev.GetDataValue<EnvironmentInfo>(Event.KnownDataKeys.EnvironmentInfo, serializer);
5022
}
5123

5224
/// <summary>
@@ -141,14 +113,7 @@ public static void SetVersion(this Event ev, string version) {
141113
/// Gets the user info object from extended data.
142114
/// </summary>
143115
public static UserInfo GetUserIdentity(this Event ev, IJsonSerializer serializer = null) {
144-
if (ev == null || !ev.Data.ContainsKey(Event.KnownDataKeys.UserInfo))
145-
return null;
146-
147-
try {
148-
return ev.Data.GetValue<UserInfo>(Event.KnownDataKeys.UserInfo, serializer);
149-
} catch (Exception) { }
150-
151-
return null;
116+
return ev.GetDataValue<UserInfo>(Event.KnownDataKeys.UserInfo, serializer);
152117
}
153118

154119
/// <summary>
@@ -189,14 +154,7 @@ public static void SetUserIdentity(this Event ev, UserInfo userInfo) {
189154
/// Gets the user description from extended data.
190155
/// </summary>
191156
public static UserDescription GetUserDescription(this Event ev, IJsonSerializer serializer = null) {
192-
if (ev == null || !ev.Data.ContainsKey(Event.KnownDataKeys.UserDescription))
193-
return null;
194-
195-
try {
196-
return ev.Data.GetValue<UserDescription>(Event.KnownDataKeys.UserDescription, serializer);
197-
} catch (Exception) { }
198-
199-
return null;
157+
return ev.GetDataValue<UserDescription>(Event.KnownDataKeys.UserDescription, serializer);
200158
}
201159

202160
/// <summary>
@@ -235,6 +193,17 @@ public static void SetManualStackingKey(this Event ev, string manualStackingKey)
235193

236194
ev.Data[Event.KnownDataKeys.ManualStackingKey] = manualStackingKey.Trim();
237195
}
196+
197+
public static T GetDataValue<T>(this Event ev, string key, IJsonSerializer serializer = null) {
198+
if(ev == null || String.IsNullOrEmpty(key) || !ev.Data.ContainsKey(key))
199+
return default(T);
200+
201+
try {
202+
return ev.Data.GetValue<T>(key, serializer);
203+
} catch (Exception) { }
204+
205+
return default(T);
206+
}
238207
}
239208

240209
/// <summary>

0 commit comments

Comments
 (0)