1

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:

  • The function just to log what I put in my C# code using ILogger. Either information, exception, warning, ...

    ex: log.LogInformation("my log");

  • Nothing from the built-in logging features including errors, exception, dependency, nothing

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);
  }
}
Daniel
  • 3,322
  • 5
  • 30
  • 40

2 Answers2

3

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.

Peter Bons
  • 26,826
  • 4
  • 50
  • 74
  • this configuration is not logging any of my logs. it only does the requests from built-in logs. – Daniel Oct 01 '20 at 09:17
  • @Daniel What is the name of your function and how do you inject the ILogger, can you post some code? Because I did test it with my own function and it does output user logs. Did you replace "MyFunction" with the name of your function in my example configuration? – Peter Bons Oct 01 '20 at 09:19
  • I have many functions, can't i do something like star, for all the functions? just updated the question with the code example – Daniel Oct 01 '20 at 09:26
  • @Daniel and can you post your host.conf as well? A full working demo can be found [here](https://github.com/Ibis-Software/AppInsightsDemo/tree/demo/src/FunctionApp) – Peter Bons Oct 01 '20 at 09:27
  • @Daniel : *I have many functions, can't i do something like star, for all the functions? just updated the question with the code example* AFAIK not, updated my answer – Peter Bons Oct 01 '20 at 09:31
  • What is the purpose of the `.User` on the end? – Murphybro2 Nov 10 '20 at 13:19
  • @Murphybro2 it is a special category for logs you write in your function, see [the docs](https://learn.microsoft.com/en-us/azure/azure-functions/configure-monitoring?tabs=v2#configure-categories). In my example I deny all logging except from my own and some statistics logging – Peter Bons Nov 10 '20 at 13:21
  • Hi all... I've the same error.. all my traces are cut off, not only the ones from the underlying AzureFunctions sdk... I use temetryClient instead of the ILog object supplied. How do you solve your issues? – user2896152 Jul 22 '21 at 11:24
  • @user2896152 you might have misconfigured the loglevels. What is in your `host.json` file? – Peter Bons Jul 22 '21 at 12:09
  • @PeterBons below the gloabl host.json file, evn with the applicationInsights node { "version": "2.0", "logging": { "LogLevel": { "Default": "Error", "Function..User": "Trace" }, "applicationInsights": { "enableDependencyTracking": false, "samplingSettings": { "isEnabled": true, "excludedTypes": "Trace,Exception,Event" } } } } – user2896152 Jul 22 '21 at 12:52
  • @user2896152 You have to replace `FUNCTIONAPP_NAME` with your actual function name (from the `FunctionName` attribute in your code) – Peter Bons Jul 22 '21 at 13:11
  • @PeterBons yes, I did it... I used the placeholder when pasting here in stackoverflow but my configuration has the actual FunctionName – user2896152 Jul 22 '21 at 13:14
  • I don't know if this can be different but I'm not using logger.LogWarning("MyT personal message"); but I'm using TelemetryClient and call _telemetryClient.TrackTrace(new TraceTelemetry("My message")); – user2896152 Jul 22 '21 at 15:31
0

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"
},
Mathias R
  • 134
  • 1
  • 4