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);
}
}
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.