Configuring Log Level for Azure Functions
To further add to @Ivan Yang's excellent answer, you can specify a minimum logging level per function in v2 of Azure Functions. (I have not verified if it does/doesn't work in v1) Using his example host.json
:
{
"version": "2.0",
"logging": {
"fileLoggingMode": "always",
"logLevel": {
"default": "Information",
"Host.Results": "Error",
"Function": "Trace",
"Function.FunctionA": "Warning",
"Host.Aggregator": "Trace"
}
}
}
Function.FunctionA
is assuming you have a function named (via the FunctionName
attribute) "FunctionA", e.g.:
[FunctionName("FunctionA")]
public static async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "")]HttpRequest req, ILogger log)
{
...function code here
}
So whatever value you specify in the FunctionName
attribute can be used to explicitly define a minimum log level just for that function. In the host.json
example above, all functions, by default, will have a minimum log level of Trace
while FunctionA will have a minimum log level of Warning
.
Please take a look at this article on how to set log level for function v1 or v2.
In the file host.json
, for the filed "Function", set its value to Trace. Then LogTrace() can be logged into application insights.
Sample host.json
for Azure function v2, which can log trace messages to Application Insights:
{
"version": "2.0",
"logging": {
"fileLoggingMode": "always",
"logLevel": {
"default": "Information",
"Host.Results": "Error",
"Function": "Trace",
"Host.Aggregator": "Trace"
}
}
}
And if you publish your function app with visual studio, you can modify your host.json file as per the above before publishing.
And if you want to change the log level in azure portal, please follow this:
In Azure portal, navigate to your function app -> in the function app settings, make sure enable the Read/Write, then change log level to trace in the host.json
.