Skip to content
Docs Try Aspire
Azure Log Analytics logo

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 following type:

  • AzureLogAnalyticsWorkspaceResource: Represents an Azure Log Analytics workspace resource.

To access this type and APIs, add the 📦 Aspire.Hosting.Azure.OperationalInsights NuGet package to your AppHost project.

Aspire CLI — Add Aspire.Hosting.Azure.OperationalInsights package
aspire add azure-operationalinsights

The Aspire CLI is interactive, be sure to select the appropriate search result when prompted:

Aspire CLI — Example output prompt
Select an integration to add:
> azure-operationalinsights (Aspire.Hosting.Azure.OperationalInsights)
> Other results listed as selectable options...

Add Azure Log Analytics workspace resource

Section titled “Add Azure Log Analytics workspace resource”

In your AppHost project, call AddAzureLogAnalyticsWorkspace to add and return an Azure Log Analytics workspace resource builder:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var logAnalytics = builder.AddAzureLogAnalyticsWorkspace("log-analytics");
// After adding all resources, run the app...

The preceding code adds an Azure Log Analytics workspace resource named log-analytics to the application model.

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:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var logAnalytics = builder.AddAzureLogAnalyticsWorkspace("log-analytics");
var appInsights = builder.AddAzureApplicationInsights("app-insights", logAnalytics);
builder.AddProject<Projects.ExampleProject>()
.WithReference(appInsights);
// After adding all resources, run the app...

The preceding code:

  • Adds an Azure Log Analytics workspace resource named log-analytics.
  • Adds an Azure Application Insights resource named app-insights that uses the Log Analytics workspace for log storage.
  • References the Application Insights resource in the ExampleProject.

For more information, see Azure Application Insights integration.

The Azure Log Analytics workspace resource exposes the following connection property:

Property nameDescription
logAnalyticsWorkspaceIdThe resource ID of the 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.')
param location string = resourceGroup().location
resource log_analytics 'Microsoft.OperationalInsights/workspaces@2023-09-01' = {
name: take('log-analytics-${uniqueString(resourceGroup().id)}', 63)
location: location
properties: {
sku: {
name: 'PerGB2018'
}
}
tags: {
'aspire-resource-name': 'log-analytics'
}
}
output logAnalyticsWorkspaceId string = log_analytics.id

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:

AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var logAnalytics = builder.AddAzureLogAnalyticsWorkspace("log-analytics")
.ConfigureInfrastructure(infra =>
{
var workspace = infra.GetProvisionableResources()
.OfType<OperationalInsightsWorkspace>()
.Single();
workspace.Sku = new OperationalInsightsWorkspaceSku(
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.

For more information, see Customize Azure resources. For the full list of configurable properties, see the Azure.Provisioning.OperationalInsights API documentation.