3
3
4
4
using System . Diagnostics ;
5
5
using Aspire . Hosting . ApplicationModel ;
6
- using Azure . ResourceManager . ApplicationInsights ;
7
6
using Azure ;
7
+ using Azure . ResourceManager . ApplicationInsights ;
8
+ using Azure . ResourceManager . OperationalInsights ;
8
9
using Microsoft . Extensions . Configuration ;
9
10
using Microsoft . Extensions . Logging ;
10
11
@@ -38,6 +39,17 @@ public override async Task GetOrCreateResourceAsync(AzureApplicationInsightsReso
38
39
39
40
if ( applicationInsightsResource is null )
40
41
{
42
+ var logAnalytics = new OperationalInsightsWorkspaceData ( context . Location ) ;
43
+ logAnalytics . Tags . Add ( AzureProvisioner . AspireResourceNameTag , resource . Name ) ;
44
+
45
+ var workspaceName = Guid . NewGuid ( ) . ToString ( ) . Replace ( "-" , string . Empty ) [ 0 ..20 ] ;
46
+
47
+ logger . LogInformation ( "Creating Log Analytics Workspace {workspaceName} in {location}..." , workspaceName , context . Location ) ;
48
+ var sw = Stopwatch . StartNew ( ) ;
49
+ var workspaceOp = await context . ResourceGroup . GetOperationalInsightsWorkspaces ( ) . CreateOrUpdateAsync ( WaitUntil . Completed , workspaceName , logAnalytics , cancellationToken ) . ConfigureAwait ( false ) ;
50
+ sw . Stop ( ) ;
51
+ logger . LogInformation ( "Log Analytics Workspace {workspaceName} created in {elapsed}" , workspaceOp . Value . Data . Name , TimeSpan . FromSeconds ( 10 ) ) ;
52
+
41
53
var applicationInsightsName = Guid . NewGuid ( ) . ToString ( ) . Replace ( "-" , string . Empty ) [ 0 ..20 ] ;
42
54
43
55
// We could model application insights as a child resource of a log analytics workspace, but instead,
@@ -47,11 +59,11 @@ public override async Task GetOrCreateResourceAsync(AzureApplicationInsightsReso
47
59
48
60
var applicationInsightsCreateOrUpdateContent = new ApplicationInsightsComponentData ( context . Location , "web" )
49
61
{
50
- WorkspaceResourceId = "" // context.LogAnalyticsWorkspace .Id
62
+ WorkspaceResourceId = workspaceOp . Value . Id
51
63
} ;
52
64
applicationInsightsCreateOrUpdateContent . Tags . Add ( AzureProvisioner . AspireResourceNameTag , resource . Name ) ;
53
65
54
- var sw = Stopwatch . StartNew ( ) ;
66
+ sw . Restart ( ) ;
55
67
var operation = await context . ResourceGroup . GetApplicationInsightsComponents ( ) . CreateOrUpdateAsync ( WaitUntil . Completed , applicationInsightsName , applicationInsightsCreateOrUpdateContent , cancellationToken ) . ConfigureAwait ( false ) ;
56
68
applicationInsightsResource = operation . Value ;
57
69
sw . Stop ( ) ;
0 commit comments