This article is the reference for the Aspire Azure Log Analytics Hosting integration. It enumerates the AppHost APIs — with examples for both AppHost.cs and apphost.mts — that you use to model Azure Log Analytics workspace resources in your AppHost project.
Azure Log Analytics is a tool in Azure Monitor that allows you to edit and run log queries against data in Azure Monitor Logs. The Aspire Azure Log Analytics integration enables you to provision Log Analytics workspaces for centralized logging and monitoring.
The Aspire Azure Log Analytics hosting integration models the Log Analytics workspace as the AzureLogAnalyticsWorkspaceResource type. To add a Log Analytics workspace resource to your app host, call AddAzureLogAnalyticsWorkspace (or addAzureLogAnalyticsWorkspace):
A common pattern is to use a Log Analytics workspace with Application Insights for centralized telemetry collection. You can link an Application Insights resource to a Log Analytics workspace:
If you’re new to Bicep, it’s a domain-specific language for defining Azure resources. With Aspire, you don’t need to write Bicep by-hand—the provisioning APIs generate Bicep for you. When you publish your app, the generated Bicep is output alongside the manifest file. When you add an Azure Log Analytics workspace resource, the following Bicep is generated:
Generated Bicep — log-analytics.bicep
@description('The location for the resource(s) to be deployed.')
The preceding Bicep provisions an Azure Log Analytics workspace with the pay-per-GB pricing tier.
The generated Bicep is a starting point and is influenced by changes to the provisioning infrastructure in C#. Customizations to the Bicep file directly will be overwritten, so make changes through the C# provisioning APIs to ensure they are reflected in the generated files.
All Aspire Azure resources are subclasses of the AzureProvisioningResource type. This type enables the customization of the generated Bicep by providing a fluent API to configure the Azure resources using the ConfigureInfrastructure API. For example, you can configure the SKU, retention period, and more. The following example demonstrates how to customize the Azure Log Analytics workspace:
C# — AppHost.cs
var builder =DistributedApplication.CreateBuilder(args);
var logAnalytics =builder.AddAzureLogAnalyticsWorkspace("log-analytics")
.ConfigureInfrastructure(infra =>
{
var workspace =infra.GetProvisionableResources()
.OfType<OperationalInsightsWorkspace>()
.Single();
workspace.Sku=newOperationalInsightsWorkspaceSku(
OperationalInsightsWorkspaceSkuName.PerGB2018);
workspace.RetentionInDays=90;
workspace.Tags.Add("environment","production");
});
var appInsights =builder.AddAzureApplicationInsights("app-insights",logAnalytics);
The preceding code:
Chains a call to the ConfigureInfrastructure API:
The infra parameter is an instance of the AzureResourceInfrastructure type.
The provisionable resources are retrieved by calling GetProvisionableResources.
The OperationalInsightsWorkspace is configured with 90-day retention and a custom tag.