Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

OneTrueError and ASP.NET

0.00/5 (No votes)
4 Aug 2014 1  
OneTrueError is my new startup which also is a member of Microsoft BizSpark. This post is about the client library for ASP.NET (WebForms/MVC/WebAPI). It will catch and handle all uncaught exceptions automatically. They are also uploaded to our site for … Continue reading →

OneTrueError is my new startup which also is a member of Microsoft BizSpark. This post is about the client library for ASP.NET (WebForms/MVC/WebAPI). It will catch and handle all uncaught exceptions automatically. They are also uploaded to our site for analysis to enable us to suggest possible solutions

To get started you need to have an account at onetrueerror.com and download our nuget-package OneTrueError.AspNet. Once you’ve completed that you’ll only have to add these lines of code to your global.asax.

OneTrue.Configuration.Credentials("appKeyThatYouGot", 
                                  "sharedSecretThatYouGot");
OneTrue.Configuration.CatchAspNetExceptions();

Now we’ll be able to detect and handle all uncaught exceptions. Simply do something which would generate an exception, like visiting a page that do not exist. You should get our default error page which looks like this:

default-error-page

If you inspect the error page HTTP headers you’ll see that we use correct status codes:

404-error-code 402-error-code

We also honor the requested type. If the caller wants XML we do provide XML. Same goes for JSON.

return-xml return-json

Options

As you’ve might have seen, we collect a lot of context information to be able to provide you with possible error solutions. Some users do not like that and we can therefore enable you to allow them to choose if we can upload the report or not.

You simply change the initialization code:

OneTrue.Configuration.Credentials("appKeyThatYouGot", 
                                  "sharedSecretThatYouGot");
OneTrue.Configuration.AskForPermission = true;
OneTrue.Configuration.CatchAspNetExceptions();

That changes the error page to:

only_collect

You can also ask the user to provide details:

OneTrue.Configuration.AskForDetails = true;

only_details

To enable them to follow the progress of the error (and get notified when you’ve have completed the error) you change another flag:

OneTrue.Configuration.AskForEmail = true;

only_email

You can of course combine them:

combination

Customization

You do not have to use our error page. You can create your own ASPX or HTML page (those works for WebApi/MVC too). To activate it, use the following command:

OneTrue.Configuration.SetErrorPage("~/views/error.aspx");

Here is a sample that I’ve made:

< %@ Page Language="C#" AutoEventWireup="true" CodeBehind="Error.aspx.cs" Inherits="TestWebApp.Views.WebForm1" %>
< %@ Import Namespace="OneTrueError.Reporting" %>
< !DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>OneSite - Deadly error</title>
    <link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=Butcherman"/>
</head>
<body style="background: #000; color: red;font-family: Butcherman; text-align: center; font-size: 1.2em">
    <h1>We are all doomed</h1>
    <img runat="server" src="~/Images/doomed.jpg"/>
    <form id="form1" runat="server">
    <div>
    < %: ((Exception)Context.Items["Exception"]).Message  %>
    </div>
    </form>
</body>
</html>

Result:

custom-error-page

You can also use a callback to provide different error pages for different errors:

public class WebApiApplication : System.Web.HttpApplication
{
	protected void Application_Start()
	{
		OneTrue.Configuration.Credentials("appKeyThatYouGot", "sharedSecretThatYouGot");
		OneTrue.Configuration.SetErrorPage(ErrorPageGenerator);
		OneTrue.Configuration.CatchAspNetExceptions();

		AreaRegistration.RegisterAllAreas();
		WebApiConfig.Register(GlobalConfiguration.Configuration);
		FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
		RouteConfig.RegisterRoutes(RouteTable.Routes);
		BundleConfig.RegisterBundles(BundleTable.Bundles);
	}

	private string ErrorPageGenerator(HttpErrorReporterContext context)
	{
		var errorPage = context.HttpContext.Response.StatusCode == 404
			? "~/Views/404.aspx"
			: "~/Views/Error.html";

		// class in our library
		return PageBuilder.Build(errorPage, context);
	}
}

Context information

At our website you can see all information about the HTTP request, response, sessions and cookies.

Dashboard

The collected information (exceptions etc) are presented in our dashboard:

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here