Azure / azure-functions-dotnet-worker

Azure Functions out-of-process .NET language worker
MIT License
428 stars 182 forks source link

.NET 6 Isolated Worker - AppInsights exception snapshots take snapshot of host, not worker #1245

Open asyncmeow opened 1 year ago

asyncmeow commented 1 year ago

(Sorry if this is in the wrong repo, not sure if the host is a separate one) I have an azure functions application running on the .NET 6 isolated runtime, with exception snapshots enabled for application insights. When the application hits an exception, it seems the snapshot is taken in the context of the host runtime, not the worker itself, as the exception shown inside the snapshot is an RpcException (not the exception thrown in the worker), and none of my code is in the stack trace shown in the snapshot: image

Is there a way to have the snapshot that's uploaded to application insights given in the context of the worker process instead of the host?

fabiocav commented 1 year ago

@kassienull this capability has been added to the worker, but since this changed the logging behavior, today it is an opt-in setting.

You can set the EnableUserCodeExceptions property to true, similar to the following:

    var host = new HostBuilder()
    .ConfigureFunctionsWorkerDefaults(builder =>
    {
    }, options =>
    {
        options.EnableUserCodeExceptions = true;
    })
    .Build();

Also leaving this issue open so we can provide a better sample and documentation for this feature.

fabiocav commented 1 year ago

Review: documentation item

inishantmishra commented 1 year ago

Even after enabling the EnableUserCodeException = true in dotnet isolated Azure function, I am still receiving RPC exceptions with no call stack. I thought after implementing this change, if l throw some custom exception in my code and if I handle it and log it, I will get my custom exception in appInsights but it is not happening, I am still getting RPC exceptions with no call stack.

Let me know what am I missing? Why am i still receiving RPC exceptions?

I am using dotnet-isolated ~4 .net 7 Isolated Azure function

@fabiocav @liliankasem @madelinegordon

liliankasem commented 1 month ago

Hi @inishantmishra, sorry about the delayed response.

The EnableUserCodeException capability was created to handle a specific scenario where AppInsights in the worker isn't an option and you need basic logging, and so the host is used to log some information to AppInsights. Via this model, custom exceptions do not work as we use Grpc to pass this information from the Worker -> Host, there is no way for the host to have any awareness of what your custom exceptions are, it does not have access to the Type.

If you want to see custom exceptions in AppInsights, you can follow this guidance to enable AppInsights within the worker instead of depending on the host logging behaviour. This is the recommended way to enable logging for the isolated model. The host exception path is a legacy model that provides basic logging capabilities when Application Insights is not a viable option.