Search code examples
c#asp.netazureasp.net-web-api2azure-application-insights

how to generate a correlation id in web api 2/app insights?


I am using App Insights in my web api 2 project, which is being called form a React Front End.

When something wrong happens I would like to show the user a generic error like: Please contact the admin, and show them a Guid, or error number.

Then with that error number I could check in App Insights what the real exception was.

Is this possible?

My web api code is below

namespace LuloWebApi.Controllers
{
    [Authorize]
    public class ClientController : ApiController
    {

        [HttpGet]
        public async Task<List<Client>> GetClients()
        {
            //var telemetry = new TelemetryClient();
            //try
            //{
                var clientStore = CosmosStoreHolder.Instance.CosmosStoreClient;
                return await clientStore.Query().ToListAsync();
            //}
            //catch (System.Exception ex)
            //{
            //    telemetry.TrackException(ex);

           //}
        }

        [HttpGet]
        public async Task<IHttpActionResult> GetClient(string clientId)
        {     
            var telemetry = new TelemetryClient();
            try
            {
                var clientStore = CosmosStoreHolder.Instance.CosmosStoreClient;
                var client = await clientStore.Query().FirstOrDefaultAsync(x => x.Id == clientId);
                if (client == null)
                {
                    return NotFound();
                }
                return Ok(client);
            }
            catch (System.Exception ex)
            {
                telemetry.TrackException(ex);
                return BadRequest("Unknown error");
            }
        }

        [HttpPut]
        public async Task<IHttpActionResult> UpdateClient(string id,[FromBody]Client client)
        {

            var telemetry = new TelemetryClient();
            try
            {

                var clientStore = CosmosStoreHolder.Instance.CosmosStoreClient;
                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState);
                }

                var result = await clientStore.UpdateAsync(client);
                return Ok(result);
            }
            catch (System.Exception ex)
            {
                telemetry.TrackException(ex);
                return BadRequest("Unknown error");
            }
        }


        [HttpPost]
        public async Task<IHttpActionResult> AddCLient([FromBody]Client Client)
        {
            var telemetry = new TelemetryClient();
            try
            {
                var clientStore = CosmosStoreHolder.Instance.CosmosStoreClient;
                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState);
                }

                var added = await clientStore.AddAsync(Client);
                return StatusCode(HttpStatusCode.NoContent);
            }
            catch (System.Exception ex)
            {
                telemetry.TrackException(ex);
                return BadRequest("Unknown error");
            }

        }



        public async Task<IHttpActionResult> DeleteClient(string clientId)
        {
            var telemetry = new TelemetryClient();
            try
            {

                var clientStore = CosmosStoreHolder.Instance.CosmosStoreClient;
                await clientStore.RemoveByIdAsync(clientId);
                return Ok(clientId);
            }
            catch (System.Exception ex)
            {
                telemetry.TrackException(ex);
                return BadRequest("Unknown error");
            }
        }
    }
}

Solution

  • Please correct me if I misunderstand you.

    I think it's as easy as manually creating a guid, and add to the exception telemetry as well to the BadRequest().

            try
            {
               //some code here
            }
            catch(Exception ex)
            {
                string guid = Guid.NewGuid().ToString();                
                Dictionary<string,string> dt = new Dictionary<string, string>();
                dt.Add("my error number1", guid);
    
                telemetryClient.TrackException(ex,dt);
                return BadRequest("Unknown error:"+guid);
            }
    

    And when you get the guid, you can search the related error in azure portal:

    enter image description here