Azure function verbose trace logging to Application Insights - azure

I have an Azure function that's connected to an App Insights instance. The function app emits log messages which I can see in the log stream in the Azure portal, and as App Insights traces.
I've increased the console log level to Verbose by adding a "tracing" element to host.json (https://github.com/Azure/azure-webjobs-sdk-script/wiki/host.json), so Verbose level messages show up in the log stream (in both the function page in the Azure portal, and in Kudu), but I can't get Verbose level traces to appear in App Insights.
Does anyone know how to get App Insights to show Verbose level traces from an Azure Function? Is it even possible? (Info traces and above are showing up just fine in App Insights)

You have a lot of control over your log levels for App Insights in Functions, but you don't use the tracing element for these. We're working on pulling the docs together in one cohesive location, but here's some links that can help:
The new logger.categoryLevel host.json settings: https://github.com/Azure/Azure-Functions/wiki/App-Insights-(Preview)#hostjson-settings
The WebJobs documentation, which gives a bit more detail on how the category filter works (behind the scenes, the host.json settings are serialized into this): https://github.com/Azure/azure-webjobs-sdk/wiki/Application-Insights-Integration#filtering
For your specific example, you can open up all the Debug logs (which matches Verbose in TraceWriter) with this in your host.json:
{
"logger": {
"categoryFilter": {
"defaultLevel": "Debug"
}
}
}
If you just want to see the verbose logs coming from your Function itself (i.e. you don't want the host's verbose logs appearing), you can restrict that with this -- which says 'for logs with the "Function" category (which is the category that function logs use), show me everything with Debug or higher log level':
{
"logger": {
"categoryFilter": {
"categoryLevels": {
"Function": "Debug"
}
}
}
}

Related

Not sending data to Application Insights from Function App

I have Function App and Application Insight services. I've noticed that amount of data which my application sends is big and generates big costs. Can I disable/completely stop sending data to AI without deleting APPINSIGHTS_INSTRUMENTATIONKEY or APPLICATIONINSIGHTS_CONNECTION_STRING? But of course I want keep the both services alive.
Should host.json be configured in some way?
Here is the workaround I did for Optimizing the cost generated by Application Insights Logs:
To minimize the number of logs, you can use the higher logging level of logs in host.json as you can see in the below screenshot:
As you can see the minimization of logs here function information log is not produced and the output log is shown in the browser, only the manual logging is shown in the logs/terminal.
And the other ways of reducing the logs and optimizing the AI Cost for Azure Functions:
- Disable unneeded modules: Edit the ApplicationInsights.config to turn off collection modules which are not required.
- Disable telemetry dynamically: To disable telemetry conditionally and dynamically anywhere in the code, set the DisableTelemetry flag on it using TelemetryConfiguration instance.
This code sample prevents the sending of telemetry to Application Insights but not the automatic collection modules from colleting telemetry and in order to remove auto collection modules also, please refer this Microsoft Documentation.
- . Customize Logs Collection:
{
"Logging": {
"LogLevel": {
"Default": "Warning"
},
"ApplicationInsights": {
"LogLevel": {
"Default": "Information"
}
}
}
}
The following above configuration allows Application Insights to capture all Information logs and severe warning logs. To change this behavior, explicitly override the logging configuration for the provider ApplicationInsights as shown below:
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
}
}
There are few more techniques to manage the data volume used for telemetry data optimizing also like:
Sampling:
Daily Cap:
Pre-aggregate metrics
Please check these references for more information:
Resolve if logs shows twice in Application Insights
Optimizing logging costs of Azure Functions
Configuring or removing the necessary Telemetry Initializers
Also, Please visit my practical workarounds (Ref1, Ref2)to reduce the unnecessary logs and optimize the cost.

Why is App Insights only showing Warning and Error tracing levels? What about lower levels?

I have a .Net Framework 4.5.1 app service that has the following code:
Console.WriteLine("🔥 Console.WriteLine");
Trace.WriteLine("🔥 Trace.WriteLine");
Trace.TraceInformation("🔥 Trace.TraceInformation");
Trace.TraceWarning("🔥 Trace.TraceWarning");
Trace.TraceError("🔥 Trace.TraceError");
I would like to be able to see logs that we log into Azure when the app is in production. So I enabled Application Service Logging for my web app:
As you can see, I have the logging level set to verbose because we want to see everything. I also have Application Insights turned on for my app. But when I check the logs, all I see are the logs at the warning level and higher.
I checked in the "logs" tab:
I also checked in my blob storage. I see logs, but they're all on a warning level or higher.
There was one way I got this to work, and that's using the feature that saves logs to a file system. But that feature turns itself off after 12 hours, and the file might get too big anyways, and I'd like to use Azure Insights since it has a nicer interface.
How can I view all my logs my application outputs over a long period of time?
I usually just remove all logging configuration from config files like appsettings.json and use the ApplicationInsightsLoggerProvider type specifier in Startup.cs, like so:
public void ConfigureServices(IServiceCollection services)
{
services.AddLogging(conf =>
{
conf.AddApplicationInsights();
conf.AddFilter<ApplicationInsightsLoggerProvider>("", LogLevel.Information);
conf.AddFilter<ApplicationInsightsLoggerProvider>("Microsoft", LogLevel.Warning);
conf.AddFilter<ApplicationInsightsLoggerProvider>("System", LogLevel.Warning);
});
...
}
It's documented here.
If you have various trace levels in your web app but are only interested in having certain levels of logs sent to the logging endpoint, you can set a filter for the minimum level in your application settings under Configuration. By default, even without the app setting, the minimum trace level is set to Warning.
How to set App setting for AppServiceAppLogs level
The application setting name will be APPSERVICEAPPLOGS_TRACE_LEVEL and the value will be the minimum level (ie. Error, Warning, Verbose, etc.). Refer to TraceLevel for more info.
Links
https://learn.microsoft.com/en-us/azure/app-service/troubleshoot-diagnostic-logs
https://azure.github.io/AppService/2020/08/31/AzMon-AppServiceAppLogs.html (Image Source)

Azure functions is not loggin all the traces in AppInsights

I have an Azure Function App with multiples functions connected with Application Insights.
For some reason that I don't know sometimes, some requests and traces get lost and it's like they never happen, but I can see the data in our DB and also in others systems.
Here is a new function with just one call, in the azure function dashboard I can see the log:
But in Application Insights, when I try to search for the logs of the trace or the request, there is not info retrived.
This's not happening everytime, but there's not the first time I saw this issue. I can see the logs for others requests but I don't know why sometimes logs are lost.
Azure function info:
Runtime Version: 3
Stack: NodeJS
Have you configured sampling? This can appear as data loss.
You can control it as follows, as per the documentation:
const appInsights = require("applicationinsights");
appInsights.setup("<instrumentation_key>");
appInsights.defaultClient.config.samplingPercentage = 33; // 33% of all telemetry will be sent to Application Insights
appInsights.start();

How to use BeginScope in Azure Application Insights (in https://portal.azure.com)?

My C# code is log.BeginScope("Testing Scope1"); and log.BeginScope("Testing Scope2");. How can I use in Azure Application Insights (in https://portal.azure.com)?
If your code like below:
using (_logger.BeginScope("Testing Scope1"))
{
_logger.LogInformation("this is an info from index page 111111");
}
Then, after the code is executed, nav to azure portal -> your application insights -> Logs -> in the traces table, write the following query(also note that select a proper "time range"):
traces
| where customDimensions.Scope contains "Testing Scope1"
| project message, customDimensions
The screenshot is as below:
By the way, it may take a few minutes for the logs being generated. And please also set the proper log level in your application(like set the proper log level in your azure function).

Azure Function is not logging LogMetric telemetry to Application Insights

I have a 3-week-old Function on the v2 SDK and uses the out-box App Insights support, i.e. just set the environment variable and ...magic.
I did read that by default it doesn't send Information level logs to AppInsights and this looks correct because I can't see any of my information tracing but I do see occasional errors I've logged out with my custom message.
But for metrics, its broken. I can see some semblance of my telemetry in the logs, sort of. It's strange.
Look.
Now look at these curious logs.
...see those n/a that match the LogMetric lines in my code?
And when I search explicitly for one of them, nothing is found.
And if I drill down on those n/a logs.
...they're pretty empty.
Is it me or is this all rather fishy?
How do I log metrics from a Function in a way that works?
First, for the metric, do you mean custom metrics? If yes, then you should find it in the customMetrics table in your app insights.
Second, what's the method are you using to send metrics? From your code, what's the implementation of this.Logger?.LogMetric method?
For sending metrics, you can use TrackMetric(just tested with this method, and works) or GetMetric method.
A sample code use TrackMetric(deprecated, but still can use) / GetMetric(recommended) method:
TelemetryConfiguration.Active.InstrumentationKey = "your key";
TelemetryClient client = new TelemetryClient();
client.TrackMetric("my metric", 60);
client.GetMetric("my metric").TrackValue(89);
client.Flush();
In my case, the settings in the host.json file were excluding the metrics from getting to Application Insights. The metrics are being logged at the Information level and I originally had mine configured for the Warning level.
"logging": {
"logLevel": {
"Function.<function name>": "Information", //<--won't see metrics if set to Warning
...
See here for more information on configuring the logging levels.

Resources