Describe the bug
When Autofac is attached and function request is invoked (simple HTTP) the first cycle is executed successfully. Any next request is failing (or logs are not written - depending on the approach) because as I've done some analysis I assume that TelemetryProcessors property or even some other part of TelemetrySink is disposed, probably improperly.
So basic issue is that telemetry client does not write the logs into Application Insights.
It was almost impossible to find out what is wrong and what is the difference between execution with and without Autofac, but I successfully done with serializing whole TelemetryClient by simple JsonConvert class. The difference has immedietaly shown - with Autofac serializer can't get TelemetryProcessors property after the first request. (documentation).
Without Autofac everything works good. The serializer is accessing this property fine and logs are written into Application Insights successfully.
It's also worth to say that I'm not adding Telemetry Client manually. As I see there is already a client added in UseAutofacServiceProviderFactory method in line
containerBuilder.RegisterModule<LoggerModule>();
which is already adding some telemetry/logging stuff.
Azure Portal:
To reproduce
Attach Autofac to existing Azure Function
Register your function class in Autofac container builder (and other components)
Turn on Application Insights on Azure Portal
Make a request to your example endpoint
Try to view your performance logs in Portal or just try to access TelemetryProcessors property
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddHttpClient();
builder.UseAutofacServiceProviderFactory(
containerBuilder => ConfigureContainer(containerBuilder, builder));
// ...
containerBuilder.RegisterType<DebugFunction>()
.AsSelf()
.InstancePerTriggerRequest(); // has tried instance per lifetimescope, but no difference in this case
public class DebugFunction
{
private readonly TelemetryClient _client;
public DebugFunction(TelemetryClient client)
{
_client = client;
}
[FunctionName(nameof(GetHealthStatus))]
public IActionResult GetHealthStatus([HttpTrigger(
AuthorizationLevel.Anonymous,
HttpMethods.Get,
Route = "Debug/Health")]
HttpRequest request)
{
try
{
return new OkObjectResult(
new
{
Status = "Running",
Timestamp = DateTime.UtcNow,
TelemetryClient = new
{
TelemetrySinkProcessorCount = _client.TelemetryConfiguration.DefaultTelemetrySink
.TelemetryProcessors.Count, // this line is failing
TelemetrySinkCount = _client.TelemetryConfiguration.TelemetrySinks.Count
}
});
}
catch (Exception ex)
{
return new OkObjectResult(new { error = true, exception = ex.ToString() });
}
}
Newtonsoft.Json.JsonSerializationException: Error getting value from 'TelemetryProcessors' on 'Microsoft.ApplicationInsights.Extensibility.TelemetrySink'.
---> System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.ApplicationInsights.Extensibility.TelemetrySink.get_TelemetryProcessors()
at lambda_method(Closure, Object )
at Newtonsoft.Json.Serialization.ExpressionValueProvider.GetValue(Object target)
Describe the bug When Autofac is attached and function request is invoked (simple HTTP) the first cycle is executed successfully. Any next request is failing (or logs are not written - depending on the approach) because as I've done some analysis I assume that
TelemetryProcessors
property or even some other part ofTelemetrySink
is disposed, probably improperly.So basic issue is that telemetry client does not write the logs into Application Insights.
It was almost impossible to find out what is wrong and what is the difference between execution with and without Autofac, but I successfully done with serializing whole
TelemetryClient
by simpleJsonConvert
class. The difference has immedietaly shown - with Autofac serializer can't getTelemetryProcessors
property after the first request. (documentation).Without Autofac everything works good. The serializer is accessing this property fine and logs are written into Application Insights successfully.
It's also worth to say that I'm not adding Telemetry Client manually. As I see there is already a client added in
UseAutofacServiceProviderFactory
method in linecontainerBuilder.RegisterModule<LoggerModule>();
which is already adding some telemetry/logging stuff.Azure Portal:
To reproduce
Assembly/dependency versions:
Additional context Response without Autofac:
Response with Autofac: