# Agent.Tracer.CaptureException throws NullReferenceException if Agent is disabled

**URL:** <https://discuss.elastic.co/t/agent-tracer-captureexception-throws-nullreferenceexception-if-agent-is-disabled/288883>\
**Category:** APM\
**Tags:** dotnet\
**Created:** [November 10, 2021, 11:29am UTC](https://discuss.elastic.co/t/agent-tracer-captureexception-throws-nullreferenceexception-if-agent-is-disabled/288883 "2021-11-10T11:29:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lken0815](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lken0815/32/96952_2.png) [@lken0815](https://discuss.elastic.co/u/lken0815)\
**Post date:** [November 10, 2021, 11:29am UTC](https://discuss.elastic.co/t/agent-tracer-captureexception-throws-nullreferenceexception-if-agent-is-disabled/288883/1 "2021-11-10T11:29:19Z")

</div>

**Kibana version** : N/A

**Elasticsearch version** : N/A

**APM Server version** : 7.15.0

**APM Agent language and version** : c# .Net Core 3.1, v1.11.1

**Browser version** : N/A

**Original install method (e.g. download page, yum, deb, from source, etc.) and version**: Nuget package

**Description of the problem including expected versus actual behavior. Please include screenshots (if relevant)**:

Hi,

if Agent.Tracer.CaptureException(exception) gets called and the agent configuration '[enabled](https://www.elastic.co/guide/en/apm/agent/dotnet/current/config-core.html#config-enabled)' is set to false, the method throws a System.NullReferenceException:

```nohighlight
System.NullReferenceException: Object reference not set to an instance of an object.
   at Elastic.Apm.BackendComm.BackendCommComponentBase.ThrowIfDisposed()
   at Elastic.Apm.Report.PayloadSenderV2.EnqueueEvent(Object eventObj, String dbgEventKind)
   at Elastic.Apm.Report.PayloadSenderV2.QueueError(IError error)
   at Elastic.Apm.Model.ExecutionSegmentCommon.CaptureException(Exception exception, IApmLogger logger, IPayloadSender payloadSender, IExecutionSegment executionSegment, IConfigurationReader configurationReader, Transaction transaction, IApmServerInfo apmServerInfo, String culprit, Boolean isHandled, String parentId, Dictionary`2 labels)
   at Elastic.Apm.Api.Tracer.CaptureException(Exception exception, String culprit, Boolean isHandled, String parentId, Dictionary`2 labels)
   at ConsoleApp1.Program.Main(String[] args) in .\ConsoleApp1\ConsoleApp1\Program.cs:line 13

```

it looks like 'BackendCommComponentBase' aborts initialization of the '\_disposableHelper' if 'enabled' is set to false.

```auto
// BackendCommComponentBase.cs Line 39
if (!_isEnabled)
{
    _logger.Debug()?.Log("Disabled - exiting without initializing any members used by work loop");
    return;
}

CancellationTokenSource = new CancellationTokenSource();

_disposableHelper = new DisposableHelper();

```

However, later if Agent.Tracer.CaptureException gets called 'PayloadSenderV2.EnqueueEvent' calls 'ThrowIfDisposed()' which checks '\_disposableHelper.HasStarted' and '\_disposableHelper' is null at this point because it was not initialized.

```auto
// BackendCommComponentBase.cs Line 131
protected void ThrowIfDisposed()
{
    if (_disposableHelper.HasStarted) throw new ObjectDisposedException( /* objectName: */ _dbgName);
}

```

**Steps to reproduce** :

1. Create .Net Core 3.1 console app
2. Install Nuget 'Elastic.Apm.AspNetCore'
3. Copy code:

```auto
static void Main(string[] args)
{
    Console.WriteLine("Hello World!");

    Environment.SetEnvironmentVariable("ELASTIC_APM_ENABLED", "false");
    Agent.Tracer.CaptureException(new Exception());
}

```

**Provide logs and/or server output (if relevant)**:

```nohighlight
System.NullReferenceException: Object reference not set to an instance of an object.
   at Elastic.Apm.BackendComm.BackendCommComponentBase.ThrowIfDisposed()
   at Elastic.Apm.Report.PayloadSenderV2.EnqueueEvent(Object eventObj, String dbgEventKind)
   at Elastic.Apm.Report.PayloadSenderV2.QueueError(IError error)
   at Elastic.Apm.Model.ExecutionSegmentCommon.CaptureException(Exception exception, IApmLogger logger, IPayloadSender payloadSender, IExecutionSegment executionSegment, IConfigurationReader configurationReader, Transaction transaction, IApmServerInfo apmServerInfo, String culprit, Boolean isHandled, String parentId, Dictionary`2 labels)
   at Elastic.Apm.Api.Tracer.CaptureException(Exception exception, String culprit, Boolean isHandled, String parentId, Dictionary`2 labels)
   at ConsoleApp1.Program.Main(String[] args) in .\ConsoleApp1\ConsoleApp1\Program.cs:line 13

```

---

<div class="post-metadata">

**Author:** ![GregKalapos](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gregkalapos/32/37205_2.png) [@GregKalapos](https://discuss.elastic.co/u/GregKalapos)\
**Post date:** [November 10, 2021, 12:40pm UTC](https://discuss.elastic.co/t/agent-tracer-captureexception-throws-nullreferenceexception-if-agent-is-disabled/288883/2 "2021-11-10T12:40:28Z")

</div>

Thanks for reporting this @lken0815 - also thanks for the nice and small reproducer, it was super helpful.

I opened a PR to fix this: [Handle enabled/recording=false on Tracer.CaptureError by gregkalapos · Pull Request #1557 · elastic/apm-agent-dotnet · GitHub](https://github.com/elastic/apm-agent-dotnet/pull/1557)

---

<div class="post-metadata">

**Author:** ![lken0815](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lken0815/32/96952_2.png) [@lken0815](https://discuss.elastic.co/u/lken0815)\
**Post date:** [November 11, 2021, 8:35am UTC](https://discuss.elastic.co/t/agent-tracer-captureexception-throws-nullreferenceexception-if-agent-is-disabled/288883/3 "2021-11-11T08:35:46Z")

</div>

Version 1.12.0 fixes the issue.

Thanks for your help! 👍

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [December 2, 2021, 4:35am UTC](https://discuss.elastic.co/t/agent-tracer-captureexception-throws-nullreferenceexception-if-agent-is-disabled/288883/4 "2021-12-02T04:35:51Z")

</div>

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.
