private void Application_EndRequest(object sender, EventArgs e)
        {
            AspNetTelemetryEventSource.Log.TraceCallback("Application_EndRequest");
            bool trackActivity = true;

            var context = ((HttpApplication)sender).Context;

            if (!ActivityHelper.HasStarted(context, out Activity aspNetActivity))
            {
                // Rewrite: In case of rewrite, a new request context is created, called the child request, and it goes through the entire IIS/ASP.NET integrated pipeline.
                // The child request can be mapped to any of the handlers configured in IIS, and it's execution is no different than it would be if it was received via the HTTP stack.
                // The parent request jumps ahead in the pipeline to the end request notification, and waits for the child request to complete.
                // When the child request completes, the parent request executes the end request notifications and completes itself.
                // Do not create activity for parent request. Parent request has IIS_UrlRewriteModule ServerVariable with success response code.
                // Child request contains an additional ServerVariable named - IIS_WasUrlRewritten.
                // Track failed response activity: Different modules in the pipleline has ability to end the response. For example, authentication module could set HTTP 401 in OnBeginRequest and end the response.
                if (context.Request.ServerVariables != null && context.Request.ServerVariables[URLRewriteRewrittenRequest] == null && context.Request.ServerVariables[URLRewriteModuleVersion] != null && context.Response.StatusCode == 200)
                {
                    trackActivity = false;
                }
                else
                {
                    // Activity has never been started
                    aspNetActivity = ActivityHelper.StartAspNetActivity(Options.TextMapPropagator, context, Options.OnRequestStartedCallback);
                }
            }

            if (trackActivity)
            {
                ActivityHelper.StopAspNetActivity(Options.TextMapPropagator, aspNetActivity, context, Options.OnRequestStoppedCallback);
            }
        }
        private void Application_Error(object sender, EventArgs e)
        {
            AspNetTelemetryEventSource.Log.TraceCallback("Application_Error");

            var context = ((HttpApplication)sender).Context;

            var exception = context.Error;

            if (exception != null)
            {
                if (!ActivityHelper.HasStarted(context, out Activity aspNetActivity))
                {
                    aspNetActivity = ActivityHelper.StartAspNetActivity(Options.TextMapPropagator, context, Options.OnRequestStartedCallback);
                }

                ActivityHelper.WriteActivityException(aspNetActivity, context, exception, Options.OnExceptionCallback);
            }
        }