问题
In a Azure Function we make a number of request using the .NET HttpClient class, the Azure Servicebus SDK and the Azure Storage SDK - everything is beautifully logged via the build in Application Insight logging, showing dependencies and all!
Now we however like to add an implementation of ITelemetryInitializer
to add a few dimensions to the EventTelemetry
being written by the SDKs mentioned above.
We start by creating a class implementing the ITelemetryInitializer
interface.
public class ReferrerTelemetryInitializer : ITelemetryInitializer
{
public void Initialize(ITelemetry telemetry)
{
if (telemetry is EventTelemetry)
{
// Add some code
}
}
}
I then create a class implementing the IWebJobsStartup
interface to DI inject my initializer.
[assembly: WebJobsStartup(typeof(Startup))]
namespace LoggingTest
{
public class Startup : IWebJobsStartup
{
public void Configure(IWebJobsBuilder builder)
{
builder.Services.AddSingleton<ITelemetryInitializer, ReferrerTelemetryInitializer>();
}
}
}
Note: I only got the Startup class to fire in .NET Core 2.0 (and not in 2.1) - this seems to be an open an known issue though?
BUT, the Initialize
method in the initializer class does however never fires (in core 2.0, after I've seen that the Startup class has registered the DI ReferrerTelemetryInitializer
implementation)?
What are we missing?
回答1:
You are not missing anything and it should just work. There is a known issue that breaks WebJobStartup in functions.
EventTelemtery is never reported by the SDK automatically (they report Requests, Dependencies, Exceptions, Traces and Metrics). Can you check if initialize is called with other types of telemetry on net core 2.0?
来源:https://stackoverflow.com/questions/54416463/custom-application-insight-telemetryinitializer-using-azure-function-v2