This solution was created as an illustrative demo for a conference presentation. Its purpose is purely to demonstrate how to write log entries to a Log Analytics workspace via an API.
Create a new resource group for the resources while adhering to your organisation's naming conventions.
- Create a new data collection endpoint resource while adhering to your organisation's naming conventions.
- Note down the Logs Ingestion endpoint URL from the Overview blade.
- Create a new log analytics workspace.
- Create a new custom table with a descriptive name in the log analytics workspace (e.g., AppSecurityEvents).
- Select DCR-based log.
- Create a new data collection rule (adhere to your organisation's naming conventions) and select the data collection endpoint you created earlier.
- Use the log-sample.json file in the root folder as a sample for the schema.
- Use the following transformer to get rid of the error.
source | extend TimeGenerated = todatetime(timestamp)
- This resource was created when you created the custom table for log analytics.
- Note down the immutable ID of the data collection rule. You can find it in the Overview blade of the data collection rule resource.
-
Create a Function App via the Azure portal.
-
Enable its managed identity.
-
Go to Environment variables and add the following settings:
Name Value Description AppId App identifier Included in log entries. DCR_DataSource Custom-TableName_CL Your custom log analytics workspace table name. Always starts with "Custom-" and ends with "_CL". DCE_LogsIngestionUrl https://your-dce-xyz.region-1.ingest.monitor.azure.com Logs Ingestion endpoint URL from the Data Collection Endpoint resource Overview blade. DCR_ImmutableId dcr-immutable-id Immutable Id from the Data Collection Rule resource Overview blade.
For quick demos, we can publish the code using publish profiles. In production scenarios, always use CI/CD pipelines to publish code to Azure services.
- enable SCM Basic Auth Publishing Credentials under Configuration -> General.
- Download the publish profile from the Overview blade
- Use the publish profile to publish the function project code to the function app.
❗ Never check in publish profiles to version control! Add them to .gitignore.
- Create a
local.settings.jsonfile in the project root folder. - Create an Entra ID application registration with a client secret.
- Add the following entries to the
local.settings.jsonfileValuessection:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
"DCR_DataSource": "Custom-TableName_CL",
"DCE_LogsIngestionUrl": "https://your-dce-xyz.region-1.ingest.monitor.azure.com",
"DCR_ImmutableId": "dcr-immutable-id",
"TenantId": "your-tenant-id",
"ClientId": "your-client-id",
"ClientSecret": "your-client-secret"
}
}You need to grant the service principal, and/or function app managed identity Monitoring Metrics Publisher role on the data collection rule resource. It can take a bit for the permissions to come into effect.