Skip to content
Permalink
Browse files

add proper exception handling

  • Loading branch information...
SergeyKanzhelev committed Sep 7, 2015
1 parent 7825ceb commit a4d2fd0334328976d43f03f9a0c2d704d05955f8
@@ -1,13 +1,13 @@
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc;
using Glimpse.Site.Telemetry;

namespace Glimpse.Site
{
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
filters.Add(new ApplicationInsightsErrorAttribute());
}
}
}
@@ -228,6 +228,7 @@
<DesignTime>True</DesignTime>
<DependentUpon>T4MVC.tt</DependentUpon>
</Compile>
<Compile Include="Telemetry\ApplicationInsightsErrorAttribute.cs" />
<Compile Include="Version.AdminController.generated.cs">
<DependentUpon>T4MVC.tt</DependentUpon>
</Compile>
@@ -1,4 +1,5 @@
using System;
using System.Threading;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
@@ -9,6 +10,8 @@
using Glimpse.Package;
using Glimpse.Release;
using Glimpse.Twitter;
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;

namespace Glimpse.Site
@@ -25,29 +28,46 @@ protected void Application_Start()
TelemetryConfiguration.Active.InstrumentationKey = ikeyValue;
}

AreaRegistration.RegisterAllAreas();
try
{

AreaRegistration.RegisterAllAreas();

GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
BindingConfig.RegisterGlobalBindings(ModelBinders.Binders, GlobalConfiguration.Configuration);
FormatterConfig.RegisterFormatters(GlobalConfiguration.Configuration);
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
BindingConfig.RegisterGlobalBindings(ModelBinders.Binders, GlobalConfiguration.Configuration);
FormatterConfig.RegisterFormatters(GlobalConfiguration.Configuration);

ReleaseSettings.Settings.Options.PackageListingPath = Server.MapPath("~/Content/packages.json");
ReleaseSettings.Settings.Initialize();
ReleaseSettings.Settings.Options.PackageListingPath = Server.MapPath("~/Content/packages.json");
ReleaseSettings.Settings.Initialize();

PackageSettings.Settings.NugetListingPath = Server.MapPath("~/Content/nuget.json");
PackageSettings.Settings.Initialize();
PackageSettings.Settings.NugetListingPath = Server.MapPath("~/Content/nuget.json");
PackageSettings.Settings.Initialize();

TwitterSettings.Settings.Initialize();
TwitterSettings.Settings.Initialize();

BuildSettings.Settings.Initialize();
BuildSettings.Settings.Initialize();

BlogSettings.Settings.Initialize();
BlogSettings.Settings.Initialize();

ContributorSettings.Settings.Options.ContributorListingPath = Server.MapPath("~/Content/committers.json");
ContributorSettings.Settings.Initialize();
ContributorSettings.Settings.Options.ContributorListingPath = Server.MapPath("~/Content/committers.json");
ContributorSettings.Settings.Initialize();
}
catch (Exception ex)
{
TelemetryClient client = new TelemetryClient();
ExceptionTelemetry excTelemetry = new ExceptionTelemetry(ex);
excTelemetry.HandledAt = ExceptionHandledAt.Unhandled;
client.TrackException(excTelemetry);

// this exception will terminate the process. Let's wait till telemetry will be delivered
client.Flush();
Thread.Sleep(1000);

throw;
}
}
}
}
@@ -0,0 +1,29 @@
using System;
using System.Web.Mvc;
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.DataContracts;

namespace Glimpse.Site.Telemetry
{
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
public class ApplicationInsightsErrorAttribute : HandleErrorAttribute
{
private TelemetryClient client = new TelemetryClient();

public override void OnException(ExceptionContext filterContext)
{
if (filterContext != null && filterContext.HttpContext != null && filterContext.Exception != null)
{
//If customError is Off, then AI HTTPModule will report the exception
if (filterContext.HttpContext.IsCustomErrorEnabled)
{
ExceptionTelemetry exc = new ExceptionTelemetry(filterContext.Exception);
exc.HandledAt = ExceptionHandledAt.Platform;
client.TrackException(exc);
}
base.OnException(filterContext);
}
}
}
}

0 comments on commit a4d2fd0

Please sign in to comment.
You can’t perform that action at this time.