Azure Function- Highlights
Develop more efficiently with Functions, an event-driven server less compute platform that can also solve complex orchestration problems. Build and debug locally without additional setup, deploy and operate at scale in the cloud, and integrate services using triggers and bindings.
There are some highlights on the commonly used features in Azure function
Create and Read Keys
1. Create Key
Go to azure portal > Go to Function app > Select your function app > Go to "Platform features" > Go to "Function app settings"> click on manage “application settings” > add app key
Note: Both "application key(s)" and "connection string" are created from the same window.
Add key in local.settings.json file in your VS solution (Fun app) to read the key locally
Note: local.settings.json: it contains the configuration individual functions to debug for dev
2. Read Key
For version: 2.0
using Microsoft.Extensions.Configuration;
[FunctionName("Function5")]
public static IActionResult Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]HttpRequest req, TraceWriter log, ExecutionContext context)
{
var config = new ConfigurationBuilder()
.SetBasePath(context.FunctionAppDirectory)
.AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
var value = config["key1"];
return value != null
? (ActionResult)new OkObjectResult($"value of key, {value} ")
: new BadRequestObjectResult("Please pass a name on the query string or in the request body");
}
OR
var value = Environment.GetEnvironmentVariable("key1");
Output:
Logging in Azure Function
- Create custom Log
log.Info($"Hello, {name}");
[FunctionName("Function5")]
public static IActionResult Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]HttpRequest req, TraceWriter log, ExecutionContext context)
{
log.Info("C# HTTP trigger function processed a request.");
string name = req.Query["name"];
log.Info($"Hello, {name}");
return name != null
? (ActionResult)new OkObjectResult($"Hello , {name} ")
: new BadRequestObjectResult("Please pass a name on the query string or in the request body");
}
2. Enable Log - Application Insight
To check the log, turn on the application insight for the function app you created
3. Check Logs
Go to you Azure Function > Click on Monitor
To check more about Application Insight, please follow below article:
Hope it helps...
Good one buddy..