Azure Developer
Azure Developer

Reputation: 1

how to insert custom logs in app insights from azure function app?

I want to send custom logs to azure app insights from azure function app. For eg i want to send error code error message and payload from azure functions to app insights

Upvotes: 0

Views: 1997

Answers (2)

Kamran Mammadov
Kamran Mammadov

Reputation: 113

1.Enable AppInsights in Azure Portal

2.Get Instrumentation Key from Azure portal for AppInsights

3.Add key to local.settings.json:

"IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
    "APPINSIGHTS_INSTRUMENTATIONKEY": "00000000-0000-0000-0000-0000000000000"
  }

Upvotes: 1

tmaj
tmaj

Reputation: 35135

Here's an example:

[FunctionName("MyFunc")]
public static async Task<IActionResult> Run(
    [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
    ILogger log)
{
   ...
   log.LogInformation($"Hello!");

For extra properties you can do the following:

var p = new Dictionary<string, object>
{
   ["MyExtraProperty1"] = 7
};

using var scope = log.BeginScope(p);
log.LogWarning("Oh no!"); 

Upvotes: 2

Related Questions