What is the purpose of the .json files in Azure Functions? - azure

It seems there are many types, although certain projects only include one or two of them.
Create a new Function in Visual Studio preview, and it gives you a host.json and local.settings.json.
I've also seen projects with function.json and project.json.
The Function docs illustrate what function.json is for:
The function.json file defines the function bindings and other configuration settings. The runtime uses this file to determine the events to monitor and how to pass data into and return data from function execution.
An example:
{
"disabled": false,
"bindings": [
// ... bindings here
{
"type": "bindingType",
"direction": "in",
"name": "myParamName",
// ... more depending on binding
}
]
}
host.json also has a purpose, per the Azure webjobs SDK:
In the root script directory, there should be a host.json metadata file that contains the global configuration options affecting all functions. The Script runtime will map these settings into their corresponding WebHobs SDK JobHostConfiguration settings when initializing the host.
So what does local.settings.json and project.json do?

local.settings.json is to store the application settings for your local development environment, see Local settings file. Production settings will be taken from Azure environment configuration.
project.json is to reference any custom NuGet packages that your function utilizes, see this answer.

Related

terraform azurerm function app settings host.json documentation missing

I'm working with an existing pipeline deploying AzureRM resources with terraform code. One of the current requirements is to set the logging level to Debug on the "Function app settings" page of a function. Here is where things get fuzzy, as i can't seem to find any examples or documentation. The part I'm trying to set is the host.json section.
Here an example of what I'm trying to set is in the code section. image link : https://ibb.co/v3Nxb6d
In the terraform code i have a section for app_settings , that works fine. I have looked at the terraform documentation, and googled around, can't find anything about it.
{
"version": "2.0",
"functionTimeout": "00:10:00",
"logging": {
"logLevel": {
"default": "Debug"
}
}
}
The host.json file is generated automatically. When function host starts, it reads log level from host.json and inject ILogger instance with corresponding filter rules.
Host configurations are not env variables, its settings cannot be overwritten by anything at the ARM level. However there are also various things that you can configure via App Settings. You could refer to this wiki.

Default Azure Function app base path D:\home\site\wwwroot Azure Function 2.x

Currently, by default, Azure function base path is set to "D:\home\site\wwwroot". For example, when publishing, VS uploads app to this folder.
I need to read config file from this folder. We have problem of ExecutionContext is null via dependency injection via constructor
Setting a new environment variable might cause issue if the path is changed in the future.
My question is that how can I use app base path that is reliable and stable, that works with DI via constructor.
Azure Function 2.x
VS 2017
you can use function.json to have your configuration key pairs.
for example:
System.Environment.GetEnvironmentVariable(name, EnvironmentVariableTarget.Process);
and in function.json you can do like this:
"mykey": "myvalue"
{
"generatedBy": "Microsoft.NET.Sdk.Functions-1.0.24",
"configurationSource": "attributes",
"bindings": [
{
"direction":"in",
"type": "timerTrigger",
"useMonitor": true,
"runOnStartup": false,
"name": "myTimer",
"mykey": "myvalue"
}
],
"disabled": false,
"scriptFile": "../bin/**.dll",
"entryPoint": "**.Run"
}
There is an environment variable pointing to home directory. This would not change as many services including function app take dependency on it. Below is how function runtime fetches it in azure environment.
string home = GetEnvironmentVariable("HOME");
Path = System.IO.Path.Combine(home, "site", "wwwroot");

Auto loading setting file depending on environment with Azure Functions 2.x and VS

I need to support multiple settings file for different environment, e.g. Dev, Prod, for Azure Functions.
Below is what I tried, I want to find out if it is possible to make the settings file loaded automicatlly without step 2, and 3, like ASP.NET CORE 2.x.
Step 1: The settings files below are defined,
MyAppSettings.development.json
{
ThirdParty: {
"Key": "Key1"
}
}
MyAppSettings.production.json
{
ThirdParty: {
"Key": "Key2"
}
}
Step 2: Define an environment variable that is used to load the settings file
Settings_File_To_Load: MyAppSettings.development.json
Is it possible to reuse a variable instead of creating a new one, e.g. ASPNETCORE_ENVIRONMENT
Step 3: Read the value of "Settings_File_To_Load", and load the content of the file.
Is it possible to let the file automicatlly loaded?
Again, is it possible to make the settings file loaded automicatlly without step 2, and 3?
Visual studio 2017
There is no automatic override for the settings file- the runtime is expecting local.settings.json and you must override this if you want to use multiple settings files in local development.
If you are changing the settings as part of publishing the solution to Azure, you can override the settings in the local.settings.json as part of the publish profile. You can use the slots feature in Azure Functions to allow for multiple configs/environments within the service.

Where is the log file created when debugging Azure Function in Visual Studio

I have a Timer Azure Function which I execute in VS. Right click on the Azure Function project and Debug. The function has an ILogger log.
Inspecting the log object I can see that is has two loggers
Azure.Functions.Cli.Diagnostics.ColoredConsoleLogger
Microsoft.Azure.WebJobs.Script.Diagnostics.FileLogger
I also can see that the RootLogPath is %temp%\LogFiles\Application\Functions.
However at that location there is only a "Host" folder. I expected to find a "Function" folder as well with the log file.
Do I need to enable somehow the File Logger? Do I miss anything?
To get file logs in local dev, we do have to modify the fileLoggingMode to always in host.json. The default debugOnly setting doesn't make function write file logs locally.
For v2 Functions
{
"version": "2.0",
"logging": {
"fileLoggingMode": "always"
}
}
For v1 Functions
{
"tracing": {
"fileLoggingMode": "always"
}
}

Implementing Application Insight to Azure Function App

Anyone know of any god guide for this?
First i created an Application Insight Resource and put:
APPINSIGHTS_INSTRUMENTATIONKEY = "INSTRUMENTATION KEY"
in the Function Apps Application Settings.
I have tried implementering the nuget package for the funtion app like this.
Createing a project.json file and pasting this:
{
"frameworks": {
"net46":{
"dependencies": {
"Microsoft.ApplicationInsights": "2.1.0"
}
}
}
}
It installed the nuget package (i could see it in the log, everything went well).
After that i put these snippets in my code to use the telemetry.TrackException(exception) functionality:
First...
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.Extensibility;
Then:
var telemetry = new TelemetryClient(new TelemetryConfiguration("INSTRUMENTATION KEY"));
and in my catch:
telemetry.TrackException(e);
and when i try to save my Function app i get this error:
error CS1729: 'TelemetryConfiguration' does not contain a constructor that takes 1 arguments
You don't need to use reference the Application Insights library to use it with Functions. If you've already set the APPINSIGHTS_INSTRUMENTATIONKEY application setting, you can simply add the ILogger interface as a parameter to your function and it will automatically send the log data to your Application Insights instance.
Full documentation can be found here: https://learn.microsoft.com/en-us/azure/azure-functions/functions-monitoring
Addition to #ChrisGillum answer for .Net Core 3.1 Azure Function:
If you create a new Azure Function with Http trigger from Visual Studio the following line will exist in the example:
log.LogInformation("C# HTTP trigger function processed a request.");
Add "APPINSIGHTS_INSTRUMENTATIONKEY" to local.settings.json Values.
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"APPINSIGHTS_INSTRUMENTATIONKEY": "<YOUR_GUID>"
},
}
Note that the key must be in an app setting named APPINSIGHTS_INSTRUMENTATIONKEY and nothing else.
Logging is then added automatically:
Complete guide for hosted values:
https://learn.microsoft.com/en-us/azure/azure-functions/functions-monitoring?tabs=cmd#enable-application-insights-integration

Resources