Currently in Azure when a a WebJob throws an exception, the exception gets caught and handled by the JobHost
(somehow) and then logs the exception to the dashboard that's available through the blade of the Web App in which the webjob is hosted. Is there any way to intercept the error handling or override it so that I can plug in my Application Insights instance ?
Asked
Active
Viewed 4,661 times
13

abatishchev
- 98,240
- 88
- 296
- 433

Alex Marshall
- 10,162
- 15
- 72
- 117
-
Check this? http://stackoverflow.com/questions/31798640/monitoring-azure-webjobs – Igorek Mar 11 '16 at 02:07
2 Answers
6
You can use the Azure WebJobs SDK Extensions : there is an ErrorTrigger
so that you can use to intercept unhandled exceptions :
public class UnhandledErrorTrigger : IDisposable
{
private readonly TelemetryClient _telemetryClient;
public UnhandledErrorTrigger(TelemetryClient telemetryClient)
{
_telemetryClient = telemetryClient;
}
public void UnHandledException([ErrorTrigger("0:01:00", 1)] TraceFilter filter, TextWriter log)
{
foreach (var traceEvent in filter.Events)
{
_telemetryClient.TrackException(traceEvent.Exception);
}
// log the last detailed errors to the Dashboard
log.WriteLine(filter.GetDetailedMessage(1));
}
public void Dispose()
{
_telemetryClient.Flush();
}
}
To register the Error extensions, call config.UseCore()
in your startup code :
private static void Main()
{
var config = new JobHostConfiguration();
config.UseCore();
...
new JobHost(config).RunAndBlock();
}
So if you are using an IoC container, you can easily inject your TelemetryClient. To configure a job activator for the webjob you can look at this post:
2
Have a look at some azure docs here. You can attach a handler to the AppDomain
handling unknown exceptions (taken from the link above):
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
// ...
private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
ExceptionTelemetry excTelemetry = new ExceptionTelemetry((Exception)e.ExceptionObject);
excTelemetry.SeverityLevel = SeverityLevel.Critical;
excTelemetry.HandledAt = ExceptionHandledAt.Unhandled;
telemetryClient.TrackException(excTelemetry);
telemetryClient.Flush();
}

Stephen Reindl
- 5,659
- 2
- 34
- 38
-
Actually I found something similar in the documents for Raygun. The problem with this approach is that I can't guarantee any metrics or extra properties I've built up during the job get recorded unless I build in some very messy exception handling for every possible throw statement. If I'm going to our that kind of logic in, I might as well just instrument the job methods directly – Alex Marshall Mar 11 '16 at 20:31
-
hmmm... I'm currently thinking of some dictionary to store key/value pairs which can be stored in a global property that can be used to store the information to be passed to AI. – Stephen Reindl Mar 11 '16 at 21:23
-
-
-