Skip to content

Commit

Permalink
Merge pull request #537 from MindscapeHQ/mb/singleton-client-asp-net-…
Browse files Browse the repository at this point in the history
…framework-http-module

Use Singleton RaygunClient in RaygunHttpModule (.NET Framework)
  • Loading branch information
MattByers authored Jul 25, 2024
2 parents ff5ef7e + f350e0b commit c1ed837
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 25 deletions.
6 changes: 5 additions & 1 deletion CHANGE-LOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Full Change Log for Raygun4Net.* packages

### v11.0.3
- Update `RaygunHttpModule` (Raygun4Net ASP.NET Framework) to use a singleton `RaygunClient` instance
- See: https://github.com/MindscapeHQ/raygun4net/pull/537

### v11.0.2
- Fix null signature issue when Debug Symbols are set to None and the application is built in Release mode
- See: https://github.com/MindscapeHQ/raygun4net/pull/535
Expand Down Expand Up @@ -127,4 +131,4 @@
- Fixed issue with signed packages for NetCore nugets

### v6.7.0
- Changed `SendInBackground` to no longer be blocking
- Changed `SendInBackground` to no longer be blocking
32 changes: 20 additions & 12 deletions Mindscape.Raygun4Net4/RaygunClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ public void SendInBackground(Func<RaygunMessage> raygunMessage)

private void StripAndSend(Exception exception, IList<string> tags, IDictionary userCustomData, RaygunIdentifierMessage userInfo, DateTime? currentTime)
{
var contextId = GetContextId();
var requestMessage = BuildRequestMessage();
IList<RaygunBreadcrumb> breadcrumbs = BuildBreadCrumbList();

Expand All @@ -447,23 +448,14 @@ private void StripAndSend(Exception exception, IList<string> tags, IDictionary u
{
x.Details.Request = requestMessage;
x.Details.Breadcrumbs = breadcrumbs;
x.Details.ContextId = contextId;
}));
}
}

private static IList<RaygunBreadcrumb> BuildBreadCrumbList()
{
IList<RaygunBreadcrumb> breadCrumbs = null;
foreach (var breadCrumb in _breadcrumbs)
{
breadCrumbs ??= new List<RaygunBreadcrumb>();
breadCrumbs.Add(breadCrumb);
}
return breadCrumbs ?? Array.Empty<RaygunBreadcrumb>();
}

private void StripAndSendInBackground(Exception exception, IList<string> tags, IDictionary userCustomData, RaygunIdentifierMessage userInfo, DateTime? currentTime)
{
var contextId = GetContextId();
var requestMessage = BuildRequestMessage();
IList<RaygunBreadcrumb> breadcrumbs = BuildBreadCrumbList();

Expand All @@ -473,10 +465,22 @@ private void StripAndSendInBackground(Exception exception, IList<string> tags, I
{
x.Details.Request = requestMessage;
x.Details.Breadcrumbs = breadcrumbs;
x.Details.ContextId = contextId;
}));
}
}

private static IList<RaygunBreadcrumb> BuildBreadCrumbList()
{
IList<RaygunBreadcrumb> breadCrumbs = null;
foreach (var breadCrumb in _breadcrumbs)
{
breadCrumbs ??= new List<RaygunBreadcrumb>();
breadCrumbs.Add(breadCrumb);
}
return breadCrumbs ?? Array.Empty<RaygunBreadcrumb>();
}

/// <summary>
/// Posts a RaygunMessage to the Raygun API endpoint.
/// </summary>
Expand Down Expand Up @@ -600,7 +604,6 @@ protected RaygunMessage BuildMessage(Exception exception, IList<string> tags, ID
.SetVersion(ApplicationVersion)
.SetTags(tags)
.SetUserCustomData(userCustomData)
.SetContextId(ContextId)
.SetUser(userInfoMessage ?? UserInfo ?? (!string.IsNullOrEmpty(User) ? new RaygunIdentifierMessage(User) : null))
.Customise(customise)
.Build();
Expand Down Expand Up @@ -668,6 +671,11 @@ protected IEnumerable<Exception> StripWrapperExceptions(Exception exception)
}
}

private string GetContextId()
{
return HttpContext.Current?.Session?.SessionID;
}

#endregion // Message Building Methods

#region Message Offline Storage
Expand Down
18 changes: 6 additions & 12 deletions Mindscape.Raygun4Net4/RaygunHttpModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace Mindscape.Raygun4Net
{
public class RaygunHttpModule : IHttpModule
{
private RaygunClient _raygunClient;

private bool ExcludeErrorsBasedOnHttpStatusCode { get; set; }

private bool ExcludeErrorsFromLocal { get; set; }
Expand Down Expand Up @@ -68,20 +70,12 @@ public void SendError(HttpApplication application, Exception exception)

protected RaygunClient GetRaygunClient(HttpApplication application)
{
var raygunApplication = application as IRaygunApplication;
return raygunApplication != null ? raygunApplication.GenerateRaygunClient() : GenerateDefaultRaygunClient(application);
}

private RaygunClient GenerateDefaultRaygunClient(HttpApplication application)
{
var instance = new RaygunClient();

if (HttpContext.Current != null && HttpContext.Current.Session != null)
if (application is IRaygunApplication raygunApplication)
{
instance.ContextId = HttpContext.Current.Session.SessionID;
return raygunApplication.GenerateRaygunClient();
}

return instance;
return _raygunClient ??= new RaygunClient();
}

protected bool CanSend(Exception exception)
Expand Down Expand Up @@ -111,4 +105,4 @@ private Assembly GetWebEntryAssembly(HttpApplication application)
return type == null ? null : type.Assembly;
}
}
}
}

0 comments on commit c1ed837

Please sign in to comment.