I working on Azure Function App and would like to adding custom messages/traces which can aid in debugging and improving performance. THis is my code I am using:
var telemetry = new Microsoft.ApplicationInsights.TelemetryClient();
telemetry.TrackTrace("Alert Button Pressed by Device ->" CloudObject.A, SeverityLevel.Warning,new Dictionary<string, string> { { "IoT Object", IOTMESSAGE } });
But when I go to Application Insight and Query traces(All) I do not see trace message I am setting.
Am I doing something wrong?
CodePudding user response:
I have reproduced with the same code and got the expected results as below:
[FunctionName("Function1")]
public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequestMessage req, TraceWriter log)
{
log.Info("C# HTTP trigger function processed a request.");
string name = req.GetQueryNameValuePairs()
.FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0)
.Value;
var telemetry = new TelemetryClient();
telemetry.TrackEvent("Loading HomeController-Index View");
telemetry.TrackTrace("Alert Button Pressed by Device ->" CloudObject.A, SeverityLevel.Warning, new Dictionary<string, string> { { "IoT Object", IOTMESSAGE } });
if (name == null)
{
// Get request body
dynamic data = await req.Content.ReadAsAsync<object>();
name = data?.name;
}
return name == null
? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name on the query string or in the request body")
: req.CreateResponse(HttpStatusCode.OK, "Hello " name);
}
Application Insights:
Query Traces:
CodePudding user response:
It still not working. I am getting this warning, could this be an issue?
I have set a sampling and data ap on application insight, I am hoping custom trace messages ignore those set limits
CodePudding user response:
You shoud not instantiate TelemetryClient
using the paramterless constructor. Use dependency injection.
- Make sure you have added the Microsoft.Azure.WebJobs.Logging.ApplicationInsights NuGet package
- Make sure the instrumentation key is set (APPINSIGHTS_INSTRUMENTATIONKEY)
- Create an instance in the constructor of the function, for example:
public HttpTrigger2(TelemetryConfiguration telemetryConfiguration)
{
this.telemetryClient = new TelemetryClient(telemetryConfiguration);
}
When 1 and 2 are done you could also use the ILogger
interface to create traces telemetry in application insights. You can inject a logger like this:
[FunctionName("HttpTrigger2")]
public Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)]
HttpRequest req, ExecutionContext context, ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
[..]
}
Please read this section in the docs carefully.