Skip to content
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

Use Singleton RaygunClient in RaygunHttpModule (.NET Framework) #537

Merged
merged 7 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -440,43 +440,47 @@ private void StripAndSend(Exception exception, IList<string> tags, IDictionary u
{
var requestMessage = BuildRequestMessage();
IList<RaygunBreadcrumb> breadcrumbs = BuildBreadCrumbList();
var contextId = GetContextId();

foreach (var e in StripWrapperExceptions(exception))
{
Send(BuildMessage(e, tags, userCustomData, userInfo, currentTime, x =>
{
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 requestMessage = BuildRequestMessage();
IList<RaygunBreadcrumb> breadcrumbs = BuildBreadCrumbList();
var contextId = GetContextId();

foreach (var e in StripWrapperExceptions(exception))
{
SendInBackground(() => BuildMessage(e, tags, userCustomData, userInfo, currentTime, x =>
{
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;
MattByers marked this conversation as resolved.
Show resolved Hide resolved
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;
}
}
}
}