Search code examples
azureazure-web-app-serviceazure-application-insightstelemetry

Correct way to write invalid behavior of user


i.e. I have a controller action:

    public ActionResult Details(int? id)
    {
        telemetryClient.TrackEvent("CompanyDetailPage");
        if (id == null)
        {
            return RedirectToAction("Error", "Error", new { Text = "id is not specified" });
        }
        var company = _companyService.CompanyDetail(id.Value);
        if (company == null)
        {
            return RedirectToAction("Error", "Error", new { Text = "Invalid company id" });

        }

        // create model here and return to client view
        return View(model);
    }

Now I want to add a notification when:

  1. User calls method without id
  2. User passes invalid id

what is correct way to do it?

Add event (metric) like "CompanyDetailPageNullId", "CompanyDetailPageInvalidId"? But for CompanyDetailPageInvalidId I need to pass id also.

Add event (metric) like "CompanyDetailPageError" and pass in parameters what is a problem?

Or, probably, track a trace?


Solution

  • there's always going to be lots of ways to do this, none "more correct" than the others. it all depends on how you want to do it.

    • one would be to add those 2 pieces of information to the 1 event you're already sending as custom properties.

    • another would be to set those 2 pieces of information on the already existing request telemetry context for the request that is in progress via

      var requestTelemetry = System.Web.HttpContext.Current.GetRequestTelemetry();

    After that, there are many ways to set up various kinds of alerts, either using azure alerts on failed requests, or something like Microsoft Flow and the Application Insights connector