Daniel
Daniel

Reputation: 3374

Application insight logging - only log what I say and nothing else

I have an Azure Function V3 (.net core). I'm using ILogger comes from Microsoft.Extensions.Logging. My problem is I can't make it to do:

I want the exact host.json logging value that does that.

ONLY WHAT I PUT IN C# CODE AND NOTHING ELSE.

Example of my code:

[FunctionName("GetNetworkHouseDetail")]
public static IActionResult Run([HttpTrigger(AuthorizationLevel.Function, "get", Route = "network/houseMonitoringDevices")] HttpRequest req, ILogger log, ClaimsPrincipal claims)
{
  try
  {
    var start = DateTime.UtcNow;

    // some actions

    var end = DateTime.UtcNow;
    var duration = (end - start).TotalSeconds;
    log.LogInformation($"API - GetNetworkHouseDetail: took {duration} second to finish");
    return new OkObjectResult(JsonConvert.SerializeObject(result));
  }
  catch (Exception ex)
  {
      log.LogError($"{ex.GetExceptionMessages()}", ex);
  }
}

Upvotes: 1

Views: 2868

Answers (2)

Mathias R
Mathias R

Reputation: 314

Specifying LogLevel "Function.MyFunction.User": "Information" did not work for me at all, even when explicitly writing the entire namespace and Function name. But even if it did work, it would be very cumbersome to need to write one such line for each of your Functions, not to mention brittle when adding/removing/renaming Functions and namespaces. I ended up with a default level of Information and then trying to "exclude" as many non-user-generated categories as possible. Seems to work fine for now.

"logLevel": {
  "default": "Information",
  "Azure": "Warning",
  "Function": "Warning",
  "Host": "Warning",
  "Microsoft": "Warning",
  "System": "Warning"
},

Upvotes: 0

Peter Bons
Peter Bons

Reputation: 29840

Well, you can use Log Level for that. From the docs:

You can use Application Insights without any custom configuration. The default configuration can result in high volumes of data. If you're using a Visual Studio Azure subscription, you might hit your data cap for Application Insights. Later in this article, you learn how to configure and customize the data that your functions send to Application Insights. For a function app, logging is configured in the host.json file.

By using LogLevel None you can disable all logging for given categories and providers.

For example, you can do

{
  "version": "2.0",
  "logging": {
    "LogLevel": {
      "Default": "None",
      "Host.Results": "Information",
      "Host.Aggregator": "Information",
      "Function.MyFunction.User": "Information"
    },
  }
}

It will disable all logging except for your function. The log category for a function written by you is "Function.<YOUR_FUNCTION_NAME>.User.". (Replace <YOUR_FUNCTION_NAME> with the actual name of your function, from the FunctionName attribute in your code)

Unfortunately you have to specify all functions, you cannot include wilcards like specifying "Function.*.User": "Information". You can do "Function": "Information" to set the log level for all functions to Information but then some extra logging will appear as well.

This will exclude dependency and request tracking from the logging as well. See this.

If you at any point want to include requests and dependencies you need to set the log level for all categories starting with "Function" to Information.

You might disable logging from the Host categories but they are special categories and you might now want to do that.

Upvotes: 3

Related Questions