Skip to content

Commit 38a6b78

Browse files
committed
Added new event extension methods.
1 parent b7af543 commit 38a6b78

File tree

2 files changed

+75
-38
lines changed

2 files changed

+75
-38
lines changed

Source/Shared/EventBuilder.cs

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Globalization;
4-
using System.Linq;
53
using Exceptionless.Plugins;
6-
using Exceptionless.Extensions;
74
using Exceptionless.Models;
85

96
namespace Exceptionless {
@@ -47,10 +44,7 @@ public EventBuilder SetSource(string source) {
4744
/// </summary>
4845
/// <param name="referenceId">The event reference id.</param>
4946
public EventBuilder SetReferenceId(string referenceId) {
50-
if (!IsValidIdentifier(referenceId))
51-
throw new ArgumentException("ReferenceId must contain between 8 and 100 alphanumeric or '-' characters.", "referenceId");
52-
53-
Target.ReferenceId = referenceId;
47+
Target.SetReferenceId(referenceId);
5448
return this;
5549
}
5650

@@ -60,26 +54,10 @@ public EventBuilder SetReferenceId(string referenceId) {
6054
/// <param name="name">Reference name</param>
6155
/// <param name="id">The reference id that points to a specific event</param>
6256
public EventBuilder SetEventReference(string name, string id) {
63-
if (String.IsNullOrEmpty(name))
64-
throw new ArgumentNullException("name");
65-
66-
if (!IsValidIdentifier(id) || String.IsNullOrEmpty(id))
67-
throw new ArgumentException("Id must contain between 8 and 100 alphanumeric or '-' characters.", "id");
68-
69-
Target.SetProperty(String.Format("@ref:{0}", name), id);
57+
Target.SetEventReference(name, id);
7058
return this;
7159
}
72-
73-
private bool IsValidIdentifier(string value) {
74-
if (value == null)
75-
return true;
76-
77-
if (value.Length < 8 || value.Length > 100)
78-
return false;
79-
80-
return value.IsValidIdentifier();
81-
}
82-
60+
8361
/// <summary>
8462
/// Sets the event message.
8563
/// </summary>
@@ -104,12 +82,7 @@ public EventBuilder SetGeo(string coordinates) {
10482
/// <param name="latitude">The event latitude.</param>
10583
/// <param name="longitude">The event longitude.</param>
10684
public EventBuilder SetGeo(double latitude, double longitude) {
107-
if (latitude < -90.0 || latitude > 90.0)
108-
throw new ArgumentOutOfRangeException("latitude", "Must be a valid latitude value between -90.0 and 90.0.");
109-
if (longitude < -180.0 || longitude > 180.0)
110-
throw new ArgumentOutOfRangeException("longitude", "Must be a valid longitude value between -180.0 and 180.0.");
111-
112-
Target.Geo = latitude.ToString("#0.0#######", CultureInfo.InvariantCulture) + "," + longitude.ToString("#0.0#######", CultureInfo.InvariantCulture);
85+
Target.SetGeo(latitude, longitude);
11386
return this;
11487
}
11588

@@ -127,10 +100,7 @@ public EventBuilder SetValue(decimal value) {
127100
/// </summary>
128101
/// <param name="tags">The tags to be added to the event.</param>
129102
public EventBuilder AddTags(params string[] tags) {
130-
if (tags == null || tags.Length == 0)
131-
return this;
132-
133-
Target.Tags.AddRange(tags.Where(t => !String.IsNullOrWhiteSpace(t)).Select(t => t.Trim()));
103+
Target.AddTags(tags);
134104
return this;
135105
}
136106

Source/Shared/Extensions/EventExtensions.cs

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Globalization;
4+
using System.Linq;
5+
using Exceptionless.Extensions;
36
using Exceptionless.Models;
47
using Exceptionless.Models.Data;
58

@@ -185,6 +188,7 @@ public static void SetUserDescription(this Event ev, UserDescription description
185188
/// <summary>
186189
/// Sets the event geo coordinates. Can be either "lat,lon" or an IP address that will be used to auto detect the geo coordinates.
187190
/// </summary>
191+
/// <param name="ev">The event.</param>
188192
/// <param name="coordinates">The event coordinates.</param>
189193
public static void SetGeo(this Event ev, string coordinates) {
190194
if (String.IsNullOrWhiteSpace(coordinates)) {
@@ -197,11 +201,11 @@ public static void SetGeo(this Event ev, string coordinates) {
197201
else
198202
throw new ArgumentException("Must be either lat,lon or an IP address.", "coordinates");
199203
}
200-
201-
204+
202205
/// <summary>
203206
/// Sets the event geo coordinates.
204207
/// </summary>
208+
/// <param name="ev">The event.</param>
205209
/// <param name="latitude">The event latitude.</param>
206210
/// <param name="longitude">The event longitude.</param>
207211
public static void SetGeo(this Event ev, double latitude, double longitude) {
@@ -210,7 +214,70 @@ public static void SetGeo(this Event ev, double latitude, double longitude) {
210214
if (longitude < -180.0 || longitude > 180.0)
211215
throw new ArgumentOutOfRangeException("longitude", "Must be a valid longitude value between -180.0 and 180.0.");
212216

213-
ev.Geo = latitude.ToString("#0.0#######") + "," + longitude.ToString("#0.0#######");
217+
ev.Geo = latitude.ToString("#0.0#######", CultureInfo.InvariantCulture) + "," + longitude.ToString("#0.0#######", CultureInfo.InvariantCulture);
218+
}
219+
220+
/// <summary>
221+
/// Adds one or more tags to the event.
222+
/// </summary>
223+
/// <param name="ev">The event.</param>
224+
/// <param name="tags">The tags to be added to the event.</param>
225+
public static void AddTags(this Event ev, params string[] tags) {
226+
if (tags == null || tags.Length == 0)
227+
return;
228+
229+
ev.Tags.AddRange(tags.Where(t => !String.IsNullOrWhiteSpace(t)).Select(t => t.Trim()));
230+
}
231+
232+
/// <summary>
233+
/// Sets the event reference id.
234+
/// </summary>
235+
/// <param name="ev">The event.</param>
236+
/// <param name="referenceId">The event reference id.</param>
237+
public static void SetReferenceId(this Event ev, string referenceId) {
238+
if (!IsValidIdentifier(referenceId))
239+
throw new ArgumentException("ReferenceId must contain between 8 and 100 alphanumeric or '-' characters.", "referenceId");
240+
241+
ev.ReferenceId = referenceId;
242+
}
243+
244+
/// <summary>
245+
/// Returns the event reference id.
246+
/// </summary>
247+
/// <param name="ev">The event.</param>
248+
/// <param name="name">Reference name</param>
249+
/// <returns></returns>
250+
public static string GetEventReference(this Event ev, string name) {
251+
if (ev == null || String.IsNullOrEmpty(name))
252+
return null;
253+
254+
return ev.Data.GetString($"@ref:{name}");
255+
}
256+
257+
/// <summary>
258+
/// Allows you to reference a parent event by its <seealso cref="Event.ReferenceId" /> property. This allows you to have parent and child relationships.
259+
/// </summary>
260+
/// <param name="ev">The event.</param>
261+
/// <param name="name">Reference name</param>
262+
/// <param name="id">The reference id that points to a specific event</param>
263+
public static void SetEventReference(this Event ev, string name, string id) {
264+
if (String.IsNullOrEmpty(name))
265+
throw new ArgumentNullException("name");
266+
267+
if (!IsValidIdentifier(id) || String.IsNullOrEmpty(id))
268+
throw new ArgumentException("Id must contain between 8 and 100 alphanumeric or '-' characters.", "id");
269+
270+
ev.SetProperty(String.Format("@ref:{0}", name), id);
271+
}
272+
273+
private static bool IsValidIdentifier(string value) {
274+
if (value == null)
275+
return true;
276+
277+
if (value.Length < 8 || value.Length > 100)
278+
return false;
279+
280+
return value.IsValidIdentifier();
214281
}
215282

216283
/// <summary>

0 commit comments

Comments
 (0)