Azure / Azure/azure-functions-dotnet-worker
Marking request as failed in Application Insights using middleware?
- Dominant language
- C#
- Stars
- 466
- Forks
- 215
- Avg merge
- 3d 10h
- Merged PRs (30d)
- 7
Description
We've implemented a middleware that basically catches some known exceptions and sets the statuscode and message in the response.
It is implemented using a simple
```csharp
public async Task Invoke(FunctionContext context, FunctionExecutionDelegate next)
{
try
{
await next(context);
}
catch (Exception ex)
{
//Pseudo code
var httpReqData = await context.GetHttpRequestDataAsync();
var newHttpResponse = httpRequestData.CreateResponse(statusCode);
await newHttpResponse.WriteStringAsync(message);
var httpOutputBindingFromMultipleOutputBindings = GetHttpOutputBindingFromMultipleOutputBinding(context);
if (httpOutputBindingFromMultipleOutputBindings is not null)
{
httpOutputBindingFromMultipleOutputBindings.Value = newHttpResponse;
}
else
{
var invocationResult = context.GetInvocationResult();
invocationResult.Value = newHttpResponse;
}
}
}
```
However since the exception is caught, the request is marked as success in Application Insights no matter what the status code is (so a 500 status code is marked as success for instance). Is there a way to hook into the telemetry and set the status of the request to be failed?
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.