-
Notifications
You must be signed in to change notification settings - Fork 2
Azure WebJobs
The ApplicationInsights.Helpers.WebJobs
package contains two components that help better instrument Azure WebJobs apps.
Version 1.1.0-beta1 of the Microsoft.Azure.WebJobs
package has new capabilities for specifying additional Tracing options for the WebJob. There are two ways to turn it on. The first way uses a new TelemetryClient
instance specific for these traces (which is not ideal):
var storageConnection = ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString;
var jobsConfig = new JobHostConfiguration(storageConnection);
jobsConfig.Tracing.Trace = new InsightsTraceWriter(TraceLevel.Warn);
new JobHost(jobsConfig).RunAndBlock();
The second way involves passing in an existing TelemetryClient
. This is useful when using IoC containers, like this:
_telemetry = _container.Resolve<TelemetryClient>();
var storageConnection = ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString;
var jobsConfig = new JobHostConfiguration(storageConnection);
jobsConfig.Tracing.Trace = new InsightsTraceWriter(_telemetry, TraceLevel.Warn);
new JobHost(jobsConfig).RunAndBlock();
There are some situations where, due to the order that things are being initialized, you can't specify the TelemetryClient until later. In that case, you can swap out the TelemetryClient, like this:
var jobsConfig = _container.Resolve<JobHostConfiguration>();
//RWM: Make sure the TraceWriter is using the singleton TelemetryClient.
var insightsTraceWriter = (InsightsTraceWriter)jobsConfig.Tracing.Trace;
insightsTraceWriter.ChangeTelemetryClient(_telemetry);
new JobHost(jobsConfig).RunAndBlock();
The TelemetryClientExtensions
add a new function to JobHostConfiguration
, in a pattern similar how you call UseXXX()
in ASP.NET Katana configuration. The function is called UseApplicationInsights()
. This function sets the Tracing level for the Console output, optionally adds the InsightsTraceWriter
with the same output level, adds the ConsoleContectInitializer
from the ApplicationInsights.Helpers.Console
package, and adds the InstrumentationKey
to any future TelemetryContext
instances.
This is the code that AdvancedREI uses in its WebJobs, which also use AutoFac IoC containers.
/// <summary>
///
/// </summary>
internal static void Main()
{
_container = ComposeApplication();
_telemetry = _container.Resolve<TelemetryClient>();
_telemetry.TrackEvent("WebJob: Console Started");
var jobsConfig = _container.Resolve<JobHostConfiguration>();
//RWM: Make sure the TraceWriter is using the singleton TelemetryClient.
var insightsTraceWriter = (InsightsTraceWriter)jobsConfig.Tracing.Trace;
insightsTraceWriter.ChangeTelemetryClient(_telemetry);
new JobHost(jobsConfig).RunAndBlock();
}
/// <summary>
/// Builds the DI Container.
/// </summary>
/// <returns>Returns an IContainer with the composed parts of the application.</returns>
private static IContainer ComposeApplication()
{
// Create the container builder
var builder = new ContainerBuilder();
builder.Register(context =>
{
var storageConnection = ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString;
var jobsConfig = new JobHostConfiguration(storageConnection);
jobsConfig.Queues.MaxDequeueCount = 1;
jobsConfig.UseApplicationInsights(CloudConfigurationManager.GetSetting("APPINSIGHTS_INSTRUMENTATIONKEY"), true, TraceLevel.Verbose);
return jobsConfig;
})
.As<JobHostConfiguration>()
.SingleInstance();
TelemetryConfiguration.Active.TelemetryChannel = new PersistenceChannel();
builder.Register(context =>
{
var telemetry = new TelemetryClient();
telemetry.HandleAppDomainEvents();
return telemetry;
})
.As<TelemetryClient>()
.SingleInstance();
// Build the container
return builder.Build();
}