No data in Crash Reporting for ASP.NET Core 6 app
jmankin
Posted on
Oct 17 2023
We have implemented Raygun according to the ASP.NET instructions.
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRaygun(builder.Configuration);
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error/Index");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseRaygun();
And in AppSettings.Test.json and AppSettings.Production.json
"RaygunSettings": {
"ApiKey": "xxxxxx"
}
When we run it in both test and release mode, the app has an exception and displays our /Error/Index page, but the error detail is not showing up in Raygun Crash Reporting page for the site.
Any help?
Thanks,
Jen
Jasen
Raygun
Posted on
Oct 17 2023
Hi Jen,
I was able to get the configuration you have provided to work correctly on a .NET 7 application. What version of .NET are you using?
I can think of 2 reasons why it might not be working for you:
- The API key is incorrect or not being read from the appsettings file correctly
- Another middleware is capturing the exception before the Raygun middleware can process it
To confirm if the first reason is the issue you can include this code to print out the configured API key:
var raygunConfig = builder.Configuration.GetSection("RaygunSettings").Get<RaygunSettings>();
Console.WriteLine($"Raygun API Key: {raygunConfig?.ApiKey}");
For the second reason, check if you have any other error-handling middleware configured and temporarily disable them to see if that allows Raygun to process the exceptions. It might be the case that you need to re-arrange the order of your middleware to ensure the Raygun middleware can process the exceptions.
Let me know how you get on.
Kind regards,
Jasen
jmankin
Posted on
Oct 18 2023
Hi Jasen,
Thanks for your response.
This is .NET 6.
I output the Raygun API key on the published-to server, and it is correct.
We don't have any additional exception-handling middleware, but I moved UseRaygun() to be the first item, and it still isn't working.
Here is what it used to look like:
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error/Index");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseCookiePolicy();
app.UseRaygun();
app.UseAuthentication();
app.UseAuthorization();
app.MapRazorPages();
app.MapDefaultControllerRoute();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();
I moved "app.UseRaygun();" to be right above app.UseHttpsRedirection();.
NLog wouldn't interfere with Raygun would it?
PHaydon
Raygun
Posted on
Oct 18 2023
Hello,
When you say you moved it to be the first item, did you put it before:
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error/Index");
i.e
app.UseRaygun();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error/Index");
I'm thinking that if UserExceptionHandler
is run first it might be short circuiting the request so Raygun is never run.
NLog wouldn't interfere with Raygun would it?
No it shouldn't interfere at all. However if you're using the NLog Raygun provider there maybe a conflict. NLog.Raygun only works with Raygun v6.#, we haven't rolled out an update for v7 yet.
If it doesn't work moving it before the if (!app.Environment.IsDevelopment())
would you mind showing your Nuget References so we can try reproduce the issue?
Thank you,
Phill
jmankin
Posted on
Oct 23 2023
Hi,
Thanks for your reply.
I tried moving it before the UseExceptionHandler, and it is still not reporting.
I noticed this in the stack trace, if it offers any insight. You will notice the third and second to-last lines seem to be calling Raygun.
at BannerSecurityReview.Policies.Handlers.ReportConfigHandler.HandleRequirementAsync(AuthorizationHandlerContext context, ReportConfigRequirement requirement) in C:\repos\BannerSecurityReview\BannerSecurityReview\Policies\Handlers\ReportConfigHandler.cs:line 47
at Microsoft.AspNetCore.Authorization.AuthorizationHandler`1.HandleAsync(AuthorizationHandlerContext context)
at Microsoft.AspNetCore.Authorization.DefaultAuthorizationService.AuthorizeAsync(ClaimsPrincipal user, Object resource, IEnumerable`1 requirements)
at Microsoft.AspNetCore.Authorization.Policy.PolicyEvaluator.AuthorizeAsync(AuthorizationPolicy policy, AuthenticateResult authenticationResult, HttpContext context, Object resource)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
at Mindscape.Raygun4Net.AspNetCore.RaygunAspNetMiddleware.Invoke(HttpContext httpContext) in C:\buildAgent\work\75116afedfe31196\Mindscape.Raygun4Net.AspNetCore\RaygunAspNetMiddleware.cs:line 75
at Mindscape.Raygun4Net.AspNetCore.RaygunAspNetMiddleware.Invoke(HttpContext httpContext) in C:\buildAgent\work\75116afedfe31196\Mindscape.Raygun4Net.AspNetCore\RaygunAspNetMiddleware.cs:line 86
at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>g__Awaited|6_0(ExceptionHandlerMiddleware middleware, HttpContext context, Task task)
jmankin
Posted on
Oct 23 2023
Oh, and here are the NuGet packages installed for this project
Jasen
Raygun
Posted on
Oct 24 2023
Hi Jen,
Thanks for the extra information.
I have attempted to reproduce your issue but so far have been unsuccessful.
Looking at your stack trace, are exceptions being handled within the ReportConfigHandler
class? Raygun will only automatically capture unhandled exceptions. Handled exceptions will need to be reported to Raygun manually.
Would you be able to provide a minimal project with the issue present so that I can investigate further?
Thanks,
Jasen
jmankin
Posted on
Oct 24 2023
Hi Jasen,
Thanks for your reply.
ReportConfigHandler handles authorization. It does not handle exceptions; it's just where I put my throw to test.
Attaching a test project. The interesting thing is that Raygun is working on my localhost (hadn't tried that before), but not on our test server. So I'm assuming it's something in the server config, but I can't find any documentation on system requirements; so, I have no idea where to start.
jmankin
Posted on
Oct 24 2023
You probably actually want the source code, huh? It's saying it's too big to attach. So here's the git repo: https://github.com/jermifer/RaygunTest
Jasen
Raygun
Posted on
Oct 24 2023
Hi Jen,
Thanks for sending through that project. There does not seem to be any issues with the configuration.
It does seem very likely that there is a configuration issue with your server. Take a look at the troubleshooting information here and let me know how you get on.
Thanks,
Jasen
jmankin
Posted on
Oct 25 2023
Hi Jasen,
The powershell script returns an empty JSON response. It doesn't tell me the status code, but I assume that means 201. But still no error shows up in the crash reporting for the application.
We are on Windows Server 2019, so the TLS version should be fine. I believe .NET 6 uses TLS 1.2.
I don't know how to set ThrowOnError for ASP.NET 6...? The documentation seems to be for framework.
I've triple checked we are using the right API key.
None of the other scenarios from the troubleshooting page apply here.
Thanks for sticking with me!
--Jen
Jasen
Raygun
Posted on
Oct 26 2023
An empty JSON response does indicate a 201 response but it is very odd that the error never turned up in Crash Reporting. Just to rule out any transitive issues could you try running the script one more time?
To enable the ThrowOnError
setting you will need to add it to your appsettings file like so:
"RaygunSettings": {
"ApiKey": "xxxxxx",
"ThrowOnError": true
}
Let me know if enabling that setting provides any clues.
Thanks,
Jasen
jmankin
Posted on
Oct 26 2023
Hi Jasen,
I tried running the Powershell script again with the same result.
I also added the ThrowOnError to my appsettings, and no error was reported.
Thanks,
Jen
PHaydon
Raygun
Posted on
Oct 26 2023
Hey,
So just to double check, the PowerShell script produced that result on your test server right?
If we create a self-contained console app to test with are you able to put that on the test-server to test?
Alternatively if we published a pre-release nuget which outputs some more information can you test that out?
Thanks,
Phill
jmankin
Posted on
Oct 26 2023
Hi Phill,
Yes, I ran the PowerShell script on our test server.
I should be able to execute a console app on the test server, no problem. I could also test out a pre-release NuGet package, whichever.
Thanks,
Jen
Jasen
Raygun
Posted on
Oct 31 2023
Hi,
Sorry for the delay in replying.
We have released a fix for the issue that was preventing you from receiving errors when running the PowerShell script.
Could you please run the script one more time from your test server to validate there are no more problems there?
We should be able to get you a test application to run shortly.
Thanks for your patience,
Jasen
jmankin
Posted on
Nov 01 2023
Hi Jasen,
Thanks for getting back to me.
I re-ran the PS script, and it showed up in Crash Reporting! =D Still no output in the console, just to be clear.
The error thrown from the app on test still doesn't show up, either.
Thanks,
Jen
Jasen
Raygun
Posted on
Nov 07 2023
Hi Jen,
I have attached a test application that contains a debug build of the raygun4net provider. The application has one endpoint /test
that will throw an exception when called. Build, run the application on your server, navigate to the /test
route and you should see in the output information about the status of sending the exception to Raygun.
Could you please run this test application on your server and let me know what the output is?
Thanks,
Jasen
jmankin
Posted on
Nov 13 2023
Hi Jasen,
Sorry for the delay (I didn't get an email notification that you had posted a new response for some reason).
I ran your app; I had to update the API key, of course. Locally, the output was "RaygunClient: Successfully sent exception to Raygun," and it showed up in Crash Reporting, as expected.
On the test server however, it said "ApiKey has not been provided, exception will not be logged." Even though the API key is in the appsettings file. I'm attaching the console output and a redacted settings file straight from the test server.
Thanks,
Jen
Jasen
Raygun
Posted on
Nov 15 2023
Hi Jen,
Thanks for running the test project.
It seems like there is something interfering with how the configuration is being loaded. This may be from an Environment Variable or some other configuration on your server.
To check this, within the test application I sent, could you add the following lines of code after the line var app = builder.Build();
var logger = app.Services.GetRequiredService<ILogger<Program>>();
var root = builder.Configuration as IConfigurationRoot;
logger.LogInformation(root.GetDebugView());
This will print all configuration keys and values with the provider that has set the value.
There should be a section for the RaygunSettings which looks like the following:
RaygunSettings:
ApiKey=API_KEY_HERE (JsonConfigurationProvider for 'appsettings.json' (Optional))
Let me know if this contains the expected ApiKey value. If not you will need to check where it has pulled the configuration from and either update the value there or remove it.
Regards,
Jasen
jmankin
Posted on
Nov 15 2023
Hi Jasen,
I added the code you requested. It outputs the API key locally, but doesn't on the test server. All the values it outputs on test seem to come from the environment variables.
I admit I can't figure out how to tell where it is pulling the configuration from (if it's pulling one at all...?). I also tried calling AddJsonFile() and that didn't change anything.
(We do have Core 6 apps working in test with plenty of settings coming from appsettings successfully, just as a side note.)
Thanks,
Jen
Jasen
Raygun
Posted on
Nov 15 2023
From what you have said it seems like there are some environment variables that are overriding the values in your appsettings file.
If you run one of the following commands it will output all the environment variables on your server
PowerShell:
Get-ChildItem Env: | Format-Table -Wrap -AutoSize
Command Prompt:
set
If there is an entry with the name RaygunSettings__ApiKey
you may need to investigate why it has been added as removing may have adverse effects on your other applications running on the server if they also run Raygun (if they are running Raygun, it might be worth checking how they are passing through the API key).
One solution is to pass in the Raygun API key when you run the application. For example, when running the RaygunTest application from the command line:
RaygunTest.exe --RaygunSettings:ApiKey API_KEY_HERE
Let me know how you get on with checking the environment variables.
Regards,
Jasen
jmankin
Posted on
Nov 17 2023
Hi Jasen,
There is no RaygunSettings__ApiKey in the environment variables.
I'm attaching the console output of the running the exe with the api key passed in as an arg. There is still nothing showing up in Crash Reporting. I'm a little confused, though, because there also is no "Raygun Client: ..." output line anymore, which I still see locally (and saw on the test server previously).
However, is opening a command prompt and executing the RaygunTest.exe, then going to http://localhost:5000/Test in the browser, a valid test? The appsettings never seem to load for any of my apps if I just execute the exe in the command prompt. But they load fine otherwise (when running the app from Visual Studio locally, or when going to the test url in the browser). Starting to think I might not be running the test right.
Thanks,
Jen
Jasen
Raygun
Posted on
Nov 19 2023
Hi Jen,
From the screenshots, I can see you are executing the exe from a different directory than where it is located. By default .NET does not set the content root to the directory of the executing file but rather from where you have executed the command. In this case, you are executing from the C:\Users\jm5-admin
directory so this is set as the content root. This is where .NET will look for the appsettings files. To always have the content root set to the directory of the executing program you can update the program as follows:
Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
var builder = WebApplication.CreateBuilder(args);
Otherwise, run the exe from within the directory it is located and that will have the same outcome.
Doing one of these steps should allow your appsettings to be loaded correctly.
As for why the RaygunClient
log messages are no longer being shown, it might be due to the logger and trace both trying to write to the console at the same time. You can add the following line to remove the logging and only see the output of the RaygunClient:
builder.Services.AddLogging(s => s.ClearProviders());
Let me know how you get on.
Regards,
Jasen
jmankin
Posted on
Nov 20 2023
Jasen,
So I executed the app from the directory, and it is loading the appsettings now. Thanks!
The RaygunClient log message is still not being shown, even after adding ClearProvider. Error is still not showing up in Crash Reporting either :(
I appreciate you sticking with me,
Jen
Jasen
Raygun
Posted on
Nov 20 2023
Just to clarify where we are at the program file of the RaygunTest app should look something like this:
using System.Diagnostics;
using Mindscape.Raygun4Net.AspNetCore;
Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
var builder = WebApplication.CreateBuilder(args);
Trace.Listeners.Add(new ConsoleTraceListener());
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddRaygun(builder.Configuration);
builder.Services.AddLogging(s => s.ClearProviders());
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseRaygun();
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
Now that we are setting the CurrentDirectory
the app can be run from anywhere and successfully load the appsettings
file.
For example with the command: ./RaygunTest.exe
The app will no longer output anything into the console as we have disabled logging but you should be able to navigate to http://localhost:5000/Test
to trigger the exception. Once triggered, there should be a message in the console about the status of sending the exception to Raygun.
Please confirm this is your setup and we'll take it from there.
Thanks,
Jasen
jmankin
Posted on
Nov 21 2023
Hi Jasen,
Yes, that is what the program file looks like.
There is no message in the console about the status of sending the exception to Raygun. (I see it locally, but not on our test server.) Attaching output from test server.
Jasen
Raygun
Posted on
Nov 22 2023
Hi Jen,
I have created a new test application that I would like you to build and then run on your test server. This new application has a new version of raygun4net that has some additional logging enabled. This should be able to tell us if raygun4net is being configured correctly as well as the result of sending exceptions to Raygun.
You will need to add your API key to the appsettings file but otherwise, it is the same as the RaygunTest application and you simply need to navigate to /Test
route to trigger an exception.
Let me know if you get any output from running this application.
jmankin
Posted on
Nov 22 2023
Hi Jasen,
We get the first 2 debug output lines, but not the "successfully sent..." line. Console output attached.
Thanks,
Jen
Jasen
Raygun
Posted on
Nov 22 2023
This has narrowed it down for us. I have updated the DebugRaygun app with some more debug logging which should tell us exactly where it is going wrong.
Let me know how this one goes, thanks.
Jasen
jmankin
Posted on
Nov 27 2023
Hi Jasen,
That returned the result:
System.Net.Http.HttpRequestException: The SSL connection could not be established, see inner exception.
---> System.Security.Authentication.AuthenticationException: Authentication failed, see inner exception.
---> System.ComponentModel.Win32Exception (0x80090326): The message received was unexpected or badly formatted.
Attaching console screenshot.
Well, after all that work, only to find out it's a problem with our test server specifically. I was able to deploy the DebugRaygun app to a prod server, and it worked fine. We are using a self-signed cert on test. Is that likely the issue?
Thanks so much for all of your support! You have been very responsive and helpful. If there's any way to get the error logging to work on our test server, that would be helpful.
--Jen
Jasen
Raygun
Posted on
Nov 29 2023
Hi Jen,
Assuming your self-signed certs are valid I wouldn't think they are the cause.
You will need to do some debugging into why your server is failing to complete the TLS handshake with the Raygun API. It might be an issue with the cipher suites available on your server. Take a look at this post and see if it can help to resolve this issue.
Let me know how you get on with this.
Jasen