Search code examples
c#.netamazon-web-servicesaws-xray

Record Exception w/ AWS X-Ray .NET MVC


I am having an issue with recording an exception to AWS X-Ray.

Here is what is in Application_Error:

protected void Application_Error(object sender, EventArgs e)
    {
        Exception exception = Server.GetLastError();

        if (exception != null)
        {
            var InnerException = exception.InnerException;

            Session["error"] = String.Format("{0} ::: {1}", exception.Message, exception.StackTrace);
            try
            {
                AWSXRayRecorder.Instance.AddException(exception);
            }
            catch (Exception ee)
            {
                // ignore errors for XRay
            }
        }
        Server.ClearError();

    }

However, an error is thrown on the AddException line stating "Segment doesn't exist in CallContext"

First time trying to get X-Ray going. Any advice? Are there any tutorials that anybody has run into that may help?


Solution

  • The default behavior of XRay is to throw an exception if you are attempting to access a Segment, and none is currently setup in the current context. You can see the default behavior mentioned in the environment variables documentation.

    As that documentation points out, you can set the AWS_XRAY_CONTEXT_MISSING environment variable on your EC2 instance to LOG_ERROR to adjust this behavior.

    Alternatively, you can use the ContextMissingStrategy property to change this behavior within the code. Documentation link.