# Create span as a child of a span Asp.net apm agent

**URL:** <https://discuss.elastic.co/t/create-span-as-a-child-of-a-span-asp-net-apm-agent/226678>\
**Category:** APM\
**Tags:** dotnet\
**Created:** [April 6, 2020, 10:51am UTC](https://discuss.elastic.co/t/create-span-as-a-child-of-a-span-asp-net-apm-agent/226678 "2020-04-06T10:51:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jyothi\_Singh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jyothi_singh/32/61521_2.png) [@Jyothi\_Singh](https://discuss.elastic.co/u/Jyothi_Singh)\
**Post date:** [April 6, 2020, 10:51am UTC](https://discuss.elastic.co/t/create-span-as-a-child-of-a-span-asp-net-apm-agent/226678/1 "2020-04-06T10:51:21Z")

</div>

```auto
Hi,

I have created spans in my ASP.net application using CaptureSpan(), but this creates spans as a child of a transaction . I was wondering, is there a way I could create spans as child of a span?

Thanks

```

---

<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:** [April 6, 2020, 12:07pm UTC](https://discuss.elastic.co/t/create-span-as-a-child-of-a-span-asp-net-apm-agent/226678/2 "2020-04-06T12:07:49Z")

</div>

Hi @Jyothi_Singh,

yes, you can do this.

Once you have an `ISpan` instance you can call the `CaptureSpan()` method on the `ISpan` instance - it's the same as on `ITransaction`, except it'll be a child span of the given span.

Like this:

```auto
transaction.CaptureSpan("span1", "sampleSpan", (firstSpan) =>
{
	firstSpan.CaptureSpan("span2", "sample", (childSpan) =>
	{
		// childSpan is the child span of the 1. span. 
	});
		 
});

```

You can also use the `StartSpan` method like this: (this is also the same as `StartSpan` on a transaction, except this time it'll be a child span of the given span):

```auto
transaction.CaptureSpan("span1", "sampleSpan", (firstSpan) =>
{
	var childSpan = firstSpan.StartSpan("span2", "sample");
	try
	{
		// Code that you capture as span
	}
	catch (Exception e)
	{
		childSpan.CaptureException(e);
		throw;
	}
	finally
	{
		childSpan.End();
	}
});

```

You can also use the `Agent.Tracer.CurrentSpan;` to get the current span at any point in the code - this returns `null` if there is no currently active span.

---

<div class="post-metadata">

**Author:** ![Jyothi\_Singh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jyothi_singh/32/61521_2.png) [@Jyothi\_Singh](https://discuss.elastic.co/u/Jyothi_Singh)\
**Post date:** [April 6, 2020, 2:02pm UTC](https://discuss.elastic.co/t/create-span-as-a-child-of-a-span-asp-net-apm-agent/226678/3 "2020-04-06T14:02:14Z")

</div>

Awesome, thank you so much for a quick reply. I am going to try this out!

---

<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:** [April 27, 2020, 10:02am UTC](https://discuss.elastic.co/t/create-span-as-a-child-of-a-span-asp-net-apm-agent/226678/4 "2020-04-27T10:02:21Z")

</div>

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