import { ApplicationInsightsManagementClient , ApplicationInsightsManagementModels as AIModels } from '@azure/arm-appinsights' ; import { WebSiteManagementModels } from '@azure/arm-appservice' ; import * as appservice from 'vscode-azureappservice' ; import { ParsedSite } from 'vscode-azureappservice' ; import { AzExtTreeItem , DialogResponses , IActionContext } from 'vscode-azureextensionui' ; import { ext } from '../../extensionVariables' ; import { localize } from '../../localize' ; import { ProductionSlotTreeItem } from '../../tree/ProductionSlotTreeItem' ; import { RemoteFunctionTreeItem } from '../../tree/remoteProject/RemoteFunctionTreeItem' ; import { SlotTreeItemBase } from '../../tree/SlotTreeItemBase' ; import { createAppInsightsClient } from '../../utils/azureClients' ; import { nonNullProp } from '../../utils/nonNull' ; import { openUrl } from '../../utils/openUrl' ; import { enableFileLogging } from './enableFileLogging' ; export async function startStreamingLogs ( context , treeItem ? ) { if ( ! treeItem ) { treeItem = await ext . tree . showTreeItemPicker < SlotTreeItemBase > ( ProductionSlotTreeItem . contextValue , context ) ; } const site = treeItem instanceof SlotTreeItemBase ? treeItem . site : treeItem . parent . parent . site ; if ( site . isLinux ) { try { await appservice . pingFunctionApp ( context , site ) ; } catch { } await openLiveMetricsStream ( context , site , treeItem ) ; } else { const verifyLoggingEnabled = async ( ) : Promise < void > => { const client = await site . createClient ( context ) ; const logsConfig = await client . getLogsConfig ( ) ; if ( ! isApplicationLoggingEnabled ( logsConfig ) ) { const message = localize ( 'enableApplicationLogging' , 'Do you want to enable application logging for "{0}"?' , client . fullName ) ; await context . ui . showWarningMessage ( message , { modal : true , stepName : 'enableAppLogging' } , DialogResponses . yes ) ; await enableFileLogging ( context , site , logsConfig ) ; } } ; await appservice . startStreamingLogs ( context , site , verifyLoggingEnabled , treeItem . logStreamLabel , treeItem . logStreamPath ) ; } } /**
 * Linux Function Apps only support streaming through App Insights
 * For initial support, we will just open the "Live Metrics Stream" view in the portal
 */ async function openLiveMetricsStream ( context , site , node ) { const client = await site . createClient ( context ) ; const appSettings = await client . listApplicationSettings ( ) ; const aiKey = appSettings . properties && appSettings . properties . APPINSIGHTS_INSTRUMENTATIONKEY ; if ( ! aiKey ) { throw new Error ( localize ( 'mustConfigureAI' , 'You must configure Application Insights to stream logs on Linux Function Apps.' ) ) ; } else { const aiClient = await createAppInsightsClient ( [ context , node ] ) ; const components = await aiClient . components . list ( ) ; const component = components . find ( c => c . instrumentationKey === aiKey ) ; if ( ! component ) { throw new Error ( localize ( 'failedToFindAI' , 'Failed to find application insights component.' ) ) ; } else { const componentId = encodeURIComponent ( JSON . stringify ( { Name : site . fullName , SubscriptionId : node . subscription . subscriptionId , ResourceGroup : site . resourceGroup } ) ) ; const resourceId = encodeURIComponent ( nonNullProp ( component , 'id' ) ) ; const url = `${ node . subscription . environment . portalUrl }/#blade/AppInsightsExtension/QuickPulseBladeV2/ComponentId/${ componentId }/ResourceId/${ resourceId }` ; await openUrl ( url ) ; } } } function isApplicationLoggingEnabled ( config ) { if ( config . applicationLogs ) { if ( config . applicationLogs . fileSystem ) { return config . applicationLogs . fileSystem . level !== undefined && config . applicationLogs . fileSystem . level . toLowerCase ( ) !== 'off' ; } else if ( config . applicationLogs . azureBlobStorage ) { return config . applicationLogs . azureBlobStorage . level !== undefined && config . applicationLogs . azureBlobStorage . level . toLowerCase ( ) !== 'off' ; } else if ( config . applicationLogs . azureTableStorage ) { return config . applicationLogs . azureTableStorage . level !== undefined && config . applicationLogs . azureTableStorage . level . toLowerCase ( ) !== 'off' ; } } return false ; }