# Elastic-apm-agent in .net 6 generates a lot of Microsoft-Windows-DotNETRuntime/ExceptionThrown events

**URL:** <https://discuss.elastic.co/t/elastic-apm-agent-in-net-6-generates-a-lot-of-microsoft-windows-dotnetruntime-exceptionthrown-events/365177>\
**Category:** APM\
**Tags:** dotnet\
**Created:** [August 20, 2024, 7:08am UTC](https://discuss.elastic.co/t/elastic-apm-agent-in-net-6-generates-a-lot-of-microsoft-windows-dotnetruntime-exceptionthrown-events/365177 "2024-08-20T07:08:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tselofan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tselofan/32/136878_2.png) [@tselofan](https://discuss.elastic.co/u/tselofan)\
**Post date:** [August 20, 2024, 7:08am UTC](https://discuss.elastic.co/t/elastic-apm-agent-in-net-6-generates-a-lot-of-microsoft-windows-dotnetruntime-exceptionthrown-events/365177/1 "2024-08-20T07:08:44Z")

</div>

We use [prometheus-net.DotNetMetrics](https://github.com/djluck/prometheus-net.DotNetRuntime) for system monitoring in ASP .net6 application. And when we started using [Elastic Apm agent for .net](https://github.com/elastic/apm-agent-dotnet), the `dotnet_exceptions_total` metric began to show increased values for `System.NotSupportedException` exceptions. We tried v1.18.0 and v 1.28.4 agent versions.

dotnet-trace shows, that the reason is in DataFlow (Task Parallel library). Exceptions have message:

> This member is not supported on this dataflow block. The block is  
> intended for a specific purpose that does not utilize this member.

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/3/6/36824c4c99676274dfc7e248c5449d0642b40c80.png)

These exceptions are catched in DataFlow, but create noise in the metric and can affect performance.

Source code, where exception is cached:

> <https://github.com/dotnet/runtime/blob/dc0432bcb6e7afba54c426f15d03fe8c8fd0d0aa/src/libraries/System.Threading.Tasks.Dataflow/src/Internal/Common.cs#L384>

We have this problem in kubernetes pods, but not in the local machine under the Windows.

Has anyone encountered such a problem?

---

<div class="post-metadata">

**Author:** ![Martijn\_Laarman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/martijn_laarman/32/4410_2.png) [@Martijn\_Laarman](https://discuss.elastic.co/u/Martijn_Laarman)\
**Post date:** [August 20, 2024, 8:44am UTC](https://discuss.elastic.co/t/elastic-apm-agent-in-net-6-generates-a-lot-of-microsoft-windows-dotnetruntime-exceptionthrown-events/365177/2 "2024-08-20T08:44:15Z")

</div>

Thanks for bringing this to our attention @tselofan.

We only use a plain `BatchBlock` which definitely implements `block.Completion`.

> <https://github.com/dotnet/runtime/blob/dc0432bcb6e7afba54c426f15d03fe8c8fd0d0aa/src/libraries/System.Threading.Tasks.Dataflow/src/Blocks/BatchBlock.cs#L142>

I had a quick peek at the differences of BatchBlock/SourceCore and the various [BlockOption](https://github.com/dotnet/runtime/blob/cf9c2496474059049d7a935d47690da28d035bfc/src/libraries/System.Threading.Tasks.Dataflow/src/Base/DataflowBlockOptions.cs#L60)  
between .NET and .NET core but could not spot anything obvious.

I created a channel based payload sender in this draft PR: [Elastic.Apm.Ingest, new PayloadSender implementation by Mpdreamz · Pull Request #2171 · elastic/apm-agent-dotnet · GitHub](https://github.com/elastic/apm-agent-dotnet/pull/2171/files)

It might be worth finishing that exercise to move us of TPL in the long run.

---

<div class="post-metadata">

**Author:** ![tselofan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tselofan/32/136878_2.png) [@tselofan](https://discuss.elastic.co/u/tselofan)\
**Post date:** [August 21, 2024, 4:31am UTC](https://discuss.elastic.co/t/elastic-apm-agent-in-net-6-generates-a-lot-of-microsoft-windows-dotnetruntime-exceptionthrown-events/365177/3 "2024-08-21T04:31:03Z")

</div>

Thanks for your answer! My research has shown the following:  
The reason is in targetBlock which is of type ReceiveTarget.

> <https://github.com/dotnet/runtime/blob/2831f11ab3adadf867cc96449b36090f4a520f2c/src/libraries/System.Threading.Tasks.Dataflow/src/Base/DataflowBlock.cs#L1390>

And the main problem is that the exception occurs when DataflowEtwProvider eventsource is enabled. So it tries to collect tracing data.

> <https://github.com/dotnet/runtime/blob/2831f11ab3adadf867cc96449b36090f4a520f2c/src/libraries/System.Threading.Tasks.Dataflow/src/Internal/TargetRegistry.cs#L100>

I think it is not usual behaviour and I need to disabled it. First of all I think that I need to find which EventSource enabled it.

---

<div class="post-metadata">

**Author:** ![tselofan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tselofan/32/136878_2.png) [@tselofan](https://discuss.elastic.co/u/tselofan)\
**Post date:** [August 22, 2024, 4:39am UTC](https://discuss.elastic.co/t/elastic-apm-agent-in-net-6-generates-a-lot-of-microsoft-windows-dotnetruntime-exceptionthrown-events/365177/4 "2024-08-22T04:39:13Z")

</div>

The reason was in prometheus-net library. In version 7.0.0 it subscribed to all eventsources in app domain.

> <https://github.com/prometheus-net/prometheus-net/blob/75cc8171a9eb6ceb099b14af70d861cc11c03ccc/Prometheus/EventCounterAdapterOptions.cs#L10>

I upgraded it to version 8 and the problem went away.

> <https://github.com/prometheus-net/prometheus-net/blob/60e9106a83ff1274fec0022c37366f04822b1d1b/Prometheus/EventCounterAdapter.cs#L237>

Thank you for your participation!
