ASP.NET
Installation
Step 1 - Install the NuGet package
The best way to install Raygun4Net.AspNetCore is to use use the dotnet
cli tool. In your project folder run dotnet add package Mindscape.Raygun4Net.AspNetCore
to install it.
Another method is to directly edit your .csproj file and add "Mindscape.Raygun4Net.AspNetCore": "6.6.6"
to your dependencies. After you've done this, run dotnet.exe restore or restore packages within Visual Studio to download and install the package.
Alternatively, visit https://www.nuget.org/packages/Mindscape.Raygun4Net.AspNetCore/ for instructions on installation.
Step 2 - Setup Raygun in appsettings.json
Add the following code to your appsettings.json
(if you're using another type of config, add it there):
"RaygunSettings": {
"ApiKey": "paste_your_api_key_here"
}
Your app API key is displayed when you create a new application in your Raygun account, or can be viewed in the application settings.
Step 3 - Configure the Raygun Middleware
Although it is not necessary for Real User Monitoring, we also recommend adding Raygun Crash reporting to your ASP.NET backend code. Details on how to do this can be found here.
Step 4 - Adding Raygun to the Front End in ASP.NET
In the /[Pages|Views]/Shared/_layout.cshtml
file, on the first line, add @inject Microsoft.Extensions.Configuration.IConfiguration Configuration
The at the end of the <head/>
section add the Raygun4JS code with the API key automatically imported from the app configuration
@inject Microsoft.Extensions.Configuration.IConfiguration Configuration
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>@ViewData["Title"] - razorDemo</title>
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
<link rel="stylesheet" href="~/css/site.css" asp-append-version="true" />
<link rel="stylesheet" href="~/razorDemo.styles.css" asp-append-version="true" />
<script type="text/javascript">
!function(a,b,c,d,e,f,g,h){a.RaygunObject=e,a[e]=a[e]||function(){
(a[e].o=a[e].o||[]).push(arguments)},f=b.createElement(c),g=b.getElementsByTagName(c)[0],
f.async=1,f.src=d,g.parentNode.insertBefore(f,g),h=a.onerror,a.onerror=function(b,c,d,f,g){
h&&h(b,c,d,f,g),g||(g=new Error(b)),a[e].q=a[e].q||[],a[e].q.push({
e:g})}}(window,document,"script","//cdn.raygun.io/raygun4js/raygun.min.js","rg4js");
</script>
<script type="text/javascript">
rg4js('apiKey', '@Configuration["RaygunSettings:ApiKey"]');
rg4js('enableRUM', true);
rg4js('enableCrashReporting', true); <!-- optional, enable CrashReporting -->
</script>
</head>
<body>
<!-- the rest of the body -->
</body>
Note: If you encounter a situation where no events are appearing within Raygun, you may need to hard code the protocol so that the CDN matches your hosting environment. This could look like one of the following -
https://cdn.raygun.io/raygun4js/raygun.min.js
http://cdn.raygun.io/raygun4js/raygun.min.js
This will be in replacement of //cdn.raygun.io/raygun4js/raygun.min.js
.
Including logged-in users details
If you want to include the logged-in users details, they can populated from the HttpContext.User
object
Add @using System.Security.Claims
to the top of the _layout.cshtml
file
Then update the end of the <head>
section to include the rg4js('setUser', {});
section below.
Depending on your authentication model, you may need to adjust what
ClaimTypes
you use.
<script type="text/javascript">
rg4js('apiKey', '@Configuration["RaygunSettings:ApiKey"]');
rg4js('enableCrashReporting', true);
rg4js('enableRUM', true); <!-- optional enable RUM -->
rg4js('setUser', {
identifier: @ViewContext.HttpContext?.User?.FindFirstValue(ClaimTypes.NameIdentifier),
isAnonymous: @(!(ViewContext.HttpContext?.User?.Identity?.IsAuthenticated ??false)),
email: @ViewContext.HttpContext?.User?.FindFirstValue(ClaimTypes.Email),
fullName: @ViewContext.HttpContext?.User?.FindFirstValue(ClaimTypes.GivenName),
});
</script>
There are more configuration options for the javascript Raygun provider. See the javascript language guide for more details.
The provider is open source and available at the Raygun4Net repository.