Skip to content

Commit

Permalink
Merge pull request #3 from MindscapeHQ/polish
Browse files Browse the repository at this point in the history
Polish
  • Loading branch information
QuantumNightmare authored Apr 23, 2024
2 parents be6ff80 + 28bc179 commit 6ee01f2
Show file tree
Hide file tree
Showing 17 changed files with 739 additions and 218 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Raygun4Aspire

[Raygun](http://raygun.com) provider for .NET Aspire projects. Collects crash reports from .NET code and displays them in a locally running Raygun portal. Optionally can be configured to send crash reports to the [Raygun](http://raygun.com) cloud service from your production environment.
[Raygun](http://raygun.com) provider for .NET Aspire projects. Collects crash reports from .NET code and displays them in a locally running Raygun portal. Optionally can be configured to send crash reports to the [Raygun](http://raygun.com) cloud service from your production environment. Tested to work with .NET 8.0 Aspire Preview 5.

# Installation

Expand All @@ -26,7 +26,7 @@ builder.AddRaygun();
// The builder is used to build and run the app somewhere down here
```

The steps so far will cause a Raygun resource to be listed in the orchestration app. Clicking on the URL of that resource will open a local Raygun portal in a new tab where you'll later be able to view crash reports captured in your local development environment.
The steps so far will cause a Raygun resource to be listed in the orchestration app. Clicking on the URL of that resource will open a local Raygun portal in a new tab where you'll later be able to view up to 1,000 crash reports captured in your local development environment.

## 3. Instrument your .NET projects

Expand Down
4 changes: 2 additions & 2 deletions src/Raygun4Aspire/Raygun4Aspire.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<RepositoryType>git</RepositoryType>
<PackageProjectUrl>https://raygun.com/platform/crash-reporting</PackageProjectUrl>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<Version>1.0.0-preview.1.0.0</Version>
<Version>1.0.0</Version>
</PropertyGroup>

<ItemGroup>
Expand All @@ -32,7 +32,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Aspire.Hosting" Version="8.0.0-preview.4.24156.9" />
<PackageReference Include="Aspire.Hosting" Version="8.0.0-preview.5.24201.12" />
<PackageReference Include="Mindscape.Raygun4Net.NetCore.Common" Version="10.1.1" />
</ItemGroup>

Expand Down
10 changes: 6 additions & 4 deletions src/Raygun4Aspire/RaygunAspireWebAppBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Net.Sockets;
using Aspire.Hosting;
using Aspire.Hosting.ApplicationModel;
using System.Net.Sockets;

namespace Raygun4Aspire
{
Expand All @@ -10,9 +12,9 @@ public static IResourceBuilder<RaygunAspireWebAppResource> AddRaygun(this IDistr
{
var raygun = new RaygunAspireWebAppResource(name);
return builder.AddResource(raygun)
.WithAnnotation(new EndpointAnnotation(ProtocolType.Tcp, uriScheme: "http", port: port ?? DefaultHostPort, containerPort: 8080))
.WithAnnotation(new ContainerImageAnnotation { Image = "raygunowner/raygun-aspire-portal", Tag = "1.0.0-preview.1.0.0" })
.WithVolumeMount("raygun-data", "/app/raygun")
.WithAnnotation(new ContainerImageAnnotation { Image = "raygunowner/raygun-aspire-portal", Tag = "1.0.0" })
.WithAnnotation(new EndpointAnnotation(ProtocolType.Tcp, uriScheme: "http", port: port ?? DefaultHostPort, targetPort: 8080))
.WithBindMount("raygun-data", "/app/raygun")
.PublishAsContainer();
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/Raygun4Aspire/RaygunAspireWebAppResource.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Raygun4Aspire
using Aspire.Hosting.ApplicationModel;

namespace Raygun4Aspire
{
public class RaygunAspireWebAppResource(string name) : ContainerResource(name)
{
Expand Down
30 changes: 25 additions & 5 deletions src/RaygunAspireWebApp/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ namespace RaygunAspireWebApp.Controllers
{
public class HomeController : Controller
{
private const int PageSize = 50;

private readonly ILogger<HomeController> _logger;

public HomeController(ILogger<HomeController> logger)
Expand All @@ -14,19 +16,37 @@ public HomeController(ILogger<HomeController> logger)
}

public IActionResult Index()
{
var model = CreateErrorListViewModel(PageSize);

return View(model);
}

public IActionResult ErrorList(int loaded)
{
var model = CreateErrorListViewModel(loaded + PageSize);

return PartialView("_ErrorList", model);
}

private ErrorListViewModel CreateErrorListViewModel(int loadAmount)
{
if (Directory.Exists(IngestionController.ErrorsFolderPath))
{
var files = Directory.GetFiles(IngestionController.ErrorsFolderPath)
.Select(filePath => new FileInfo(filePath))
.OrderByDescending(filePath => filePath.CreationTime)
.Select(ConvertFileInfoToErrorInstance)
.ToList();
.OrderByDescending(filePath => filePath.CreationTime).ToList();

loadAmount = Math.Min(loadAmount, files.Count);

var errors = files.Take(loadAmount)
.Select(ConvertFileInfoToErrorInstance)
.ToList();

return View(files);
return new ErrorListViewModel() { Errors = errors, Loaded = loadAmount, Total = files.Count };
}

return View(new List<ErrorInstanceRow>());
return new ErrorListViewModel() { Errors = new List<ErrorInstanceRow>() };
}

private ErrorInstanceRow ConvertFileInfoToErrorInstance(FileInfo fileInfo)
Expand Down
24 changes: 23 additions & 1 deletion src/RaygunAspireWebApp/Controllers/IngestionController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ public class IngestionController : Controller
{
public const string ErrorsFolderPath = "/app/raygun/errors";

// If changing this limit, also update it in the README and public-site documentation:
private const int RetentionCount = 1000;

private RaygunClient _raygunClient;

public IngestionController(RaygunClient raygunClient)
Expand All @@ -29,10 +32,17 @@ public async Task<IActionResult> Entries()
{
var info = Directory.CreateDirectory(ErrorsFolderPath);

var message = raygunMessage.Details.Error.Message;
var message = raygunMessage.Details?.Error?.Message;
var uniqueSlug = DateTime.UtcNow.Ticks;

if (string.IsNullOrWhiteSpace(message))
{
message = "Unknown error";
}

System.IO.File.WriteAllText($"{ErrorsFolderPath}/{uniqueSlug}|{message}.json", requestBody);

EnforceRetentionAsync();
}
}
catch (Exception ex)
Expand All @@ -43,5 +53,17 @@ public async Task<IActionResult> Entries()

return Accepted();
}

private void EnforceRetentionAsync()
{
var files = Directory.GetFiles(ErrorsFolderPath)
.Select(filePath => new FileInfo(filePath))
.OrderByDescending(filePath => filePath.CreationTime).Skip(RetentionCount).ToList();

foreach (var file in files)
{
System.IO.File.Delete(file.FullName);
}
}
}
}
9 changes: 9 additions & 0 deletions src/RaygunAspireWebApp/Models/ErrorListViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace RaygunAspireWebApp.Models
{
public class ErrorListViewModel
{
public List<ErrorInstanceRow> Errors { get; set; }
public int Loaded { get; set; }
public int Total { get; set; }
}
}
18 changes: 18 additions & 0 deletions src/RaygunAspireWebApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@ public static void Main(string[] args)

var app = builder.Build();

var raygunClient = app.Services.GetService<RaygunClient>();

if (raygunClient != null)
{
raygunClient.SendingMessage += (sender, eventArgs) =>
{
if (eventArgs?.Message?.Details != null)
{
eventArgs.Message.Details.MachineName = null;
}

if (eventArgs?.Message?.Details?.Request != null)
{
eventArgs.Message.Details.Request.IPAddress = null;
}
};
}

app.UseRaygun();

// Configure the HTTP request pipeline.
Expand Down
32 changes: 30 additions & 2 deletions src/RaygunAspireWebApp/Views/ErrorInstance/Details.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,42 @@
<script src="~/js/htmx.min.js"></script>
<script src="~/js/moment.min.js"></script>

@{
string BuildHeading(string className, string message)
{
var heading = "";

if (!string.IsNullOrWhiteSpace(className))
{
heading = $"{className}: ";
}

if (!string.IsNullOrWhiteSpace(message))
{
heading += message;
}
else
{
heading += "Unknown error";
}

return heading;
}
}

<div class="margin--24">
<h2 class="font-opensans--18--28--600 color--color-text-default">@Model.RaygunMessage.Details.Error.ClassName: @Model.RaygunMessage.Details.Error.Message</h2>
<h2 class="font-opensans--18--28--600 color--color-text-default">
@BuildHeading(Model?.RaygunMessage?.Details?.Error?.ClassName, Model?.RaygunMessage?.Details?.Error?.Message)
</h2>

<p id="occurredOnHeading" class="font-opensans--14--32--600 color--color-text-low-emphasis">@Model.RaygunMessage.OccurredOn</p>

<ul class="tab-group clearfix font-opensans--14--32--600 color--color-text-default margin-top--24">
<li class="js-tab-item tab-group--item tab-group--item--active" hx-get="/ErrorInstance/TabContent" hx-target="#tab-content" hx-trigger="click" hx-vals='{"tab": "summary"}'>Summary</li>
<li class="js-tab-item tab-group--item" hx-get="/ErrorInstance/TabContent" hx-target="#tab-content" hx-trigger="click" hx-vals='{"tab": "http"}'>Http</li>
@if (Model?.RaygunMessage?.Details?.Request != null || Model?.RaygunMessage?.Details?.Response != null)
{
<li class="js-tab-item tab-group--item" hx-get="/ErrorInstance/TabContent" hx-target="#tab-content" hx-trigger="click" hx-vals='{"tab": "http"}'>Http</li>
}
<li class="js-tab-item tab-group--item tab-group--item-last" hx-get="/ErrorInstance/TabContent" hx-target="#tab-content" hx-trigger="click" hx-vals='{"tab": "rawdata"}'>Raw data</li>
</ul>
<div id="tab-content" class="background--color-bg-surface-01 box-shadow--tabs border-color--color-border-elevation border-width--1 border-style--solid">
Expand Down
Loading

0 comments on commit 6ee01f2

Please sign in to comment.