2

We have an Azure Function V3 developed in C#. We tried to log some custom events, dependencies, etc to Azure Application Insights. We are not able to log in app insights using TelemetryClient. The code runs fine without any errors. Also, could see the instrumentation key retrieved from the config file. However Ilogger logs can be found in the app insights traces table. Please find below the code that we used,

public class CommunityCreate
{
    private readonly TelemetryClient telemetryClient;
    public CommunityCreate(TelemetryConfiguration telemetryConfiguration)
    {
        this.telemetryClient = new TelemetryClient(telemetryConfiguration);
    }

    [FunctionName("Function1")]
    [return: ServiceBus("sample", Connection = "ServiceBusProducerConnection")]
    public async Task<string> Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "Account/{id:int?}")] HttpRequest req, string id, ILogger log)
    {
        //log.LogInformation("C# HTTP trigger function processed a request.");
        DateTime start = DateTime.UtcNow;

        string name = req.Query["name"];

        string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
        dynamic data = JsonConvert.DeserializeObject(requestBody);
        name = name ?? data?.name;

        var evt = new EventTelemetry("Function called");
        evt.Context.User.Id = name;
        this.telemetryClient.TrackEvent(evt);

        // Generate a custom metric, in this case let's use ContentLength.
        this.telemetryClient.GetMetric("contentLength").TrackValue(req.ContentLength);

        // Log a custom dependency in the dependencies table.
        var dependency = new DependencyTelemetry
        {
            Name = "GET api/planets/1/",
            Target = "swapi.co",
            Data = "https://swapi.co/api/planets/1/",
            Timestamp = start,
            Duration = DateTime.UtcNow - start,
            Success = true
        };
        
        dependency.Context.User.Id = name;
        this.telemetryClient.TrackDependency(dependency);

        telemetryClient.TrackEvent("Ack123 Received");
        telemetryClient.TrackMetric("Test Metric", DateTime.Now.Millisecond);

        return name;
    }
}

1 Answer 1

1

Please make sure you're using the correct packages as below:

Microsoft.Azure.WebJobs.Logging.ApplicationInsights, version 3.0.18

and

update the package Microsoft.NET.Sdk.Functions the latest version 3.0.9.

If you're running the project locally, please add the APPINSIGHTS_INSTRUMENTATIONKEY in local.settings.json, like below:

{
    "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "xxxx",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "APPINSIGHTS_INSTRUMENTATIONKEY": "xxx"
  }
}

Or if you're running it on azure portal, please configure the Application insights with azure function.

Then I tested your code, the custom events or dependency are correctly logged into Application insights. Here is the screenshot:

enter image description here

If you still have the issue, please let me know(and please also provide more details).

Sign up to request clarification or add additional context in comments.

9 Comments

Thanks for your suggestions. Everything else is fine, but I was using Microsoft.ApplicationInsights package instead of Microsoft.Azure.WebJobs.Logging.ApplicationInsights. But i dont see TelemetryClient object in Microsoft.Azure.WebJobs.Logging.ApplicationInsights library. Am i missing something?
@Raj, the TelemetryClient is in the Microsoft.ApplicationInsights package. But when you install the package Microsoft.Azure.WebJobs.Logging.ApplicationInsights, the package Microsoft.ApplicationInsights will be automatically installed as well. Because the 2 packages have dependency relationship. So you can also use TelemetryClient.
I have all these packages. But still i could not see logs in app insights.
@Raj, where are you running the project? in azure or locally?
@Raj, I suggest that you can test it locally as per the steps in my answer. And during test, please take a look at the visual studio output window, to see if there are any telemetry data being sent.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.