使用 Application Insights 监视 SharePoint site 站点Monitor a SharePoint site with Application Insights

Azure Application Insights 监视应用的可用性、性能和使用情况。Azure Application Insights monitors the availability, performance and usage of your apps. 下面介绍如何为 SharePoint 站点设置它。Here you'll learn how to set it up for a SharePoint site.

备注

出于安全考虑,你不能在 SharePoint 新式用户体验中直接向网页添加本文所述的脚本。Due to security concerns, you can't directly add the script that's described in this article to your webpages in the SharePoint modern UX. 作为替代方法,可使用 SharePoint 框架 (SPFx) 来构建可用于在 SharePoint 站点上安装 Application Insights 的自定义扩展。As an alternative, you can use SharePoint Framework (SPFx) to build a custom extension that you can use to install Application Insights on your SharePoint sites.

创建 Application Insights 资源Create an Application Insights resource

Azure 门户中,创建新的 Application Insights 资源。In the Azure portal, create a new Application Insights resource. 选择 ASP.NET 作为应用程序类型。Choose ASP.NET as the application type.

单击“属性”,选择密钥,并按 Ctrl+C

打开的窗口是查看关于应用的性能和用法数据的位置。The window that opens is the place where you'll see performance and usage data about your app. 若要在下次登录 Azure 时返回到它,应在开始屏幕上查找它的磁贴。To get back to it next time you login to Azure, you should find a tile for it on the start screen. 或者单击“浏览”查找它。Alternatively click Browse to find it.

将脚本添加到网页Add the script to your web pages

<!-- 
To collect user behavior analytics tools about your application, 
insert the following script into each page you want to track.
Place this code immediately before the closing </head> tag,
and before any other scripts. Your first data will appear 
automatically in just a few seconds.
-->
<script type="text/javascript">
var sdkInstance="appInsightsSDK";window[sdkInstance]="appInsights";var aiName=window[sdkInstance],aisdk=window[aiName]||function(n){var o={config:n,initialize:!0},t=document,e=window,i="script";setTimeout(function(){var e=t.createElement(i);e.src=n.url||"https://az416426.vo.msecnd.net/scripts/b/ai.2.min.js",t.getElementsByTagName(i)[0].parentNode.appendChild(e)});try{o.cookie=t.cookie}catch(e){}function a(n){o[n]=function(){var e=arguments;o.queue.push(function(){o[n].apply(o,e)})}}o.queue=[],o.version=2;for(var s=["Event","PageView","Exception","Trace","DependencyData","Metric","PageViewPerformance"];s.length;)a("track"+s.pop());var r="Track",c=r+"Page";a("start"+c),a("stop"+c);var u=r+"Event";if(a("start"+u),a("stop"+u),a("addTelemetryInitializer"),a("setAuthenticatedUserContext"),a("clearAuthenticatedUserContext"),a("flush"),o.SeverityLevel={Verbose:0,Information:1,Warning:2,Error:3,Critical:4},!(!0===n.disableExceptionTracking||n.extensionConfig&&n.extensionConfig.ApplicationInsightsAnalytics&&!0===n.extensionConfig.ApplicationInsightsAnalytics.disableExceptionTracking)){a("_"+(s="onerror"));var p=e[s];e[s]=function(e,n,t,i,a){var r=p&&p(e,n,t,i,a);return!0!==r&&o["_"+s]({message:e,url:n,lineNumber:t,columnNumber:i,error:a}),r},n.autoExceptionInstrumented=!0}return o}(
{
  instrumentationKey:"INSTRUMENTATION_KEY"
}
);(window[aiName]=aisdk).queue&&0===aisdk.queue.length&&aisdk.trackPageView({});
</script>

在要跟踪的每个页面的 </head> 标记之前插入脚本。如果网站有母版页,可以在那里插入脚本。Insert the script just before the </head> tag of every page you want to track. If your website has a master page, you can put the script there. 例如,在 ASP.NET MVC 项目中,将其放置在 View\Shared_Layout.cshtml 中For example, in an ASP.NET MVC project, you'd put it in View\Shared_Layout.cshtml

该脚本包含将遥测定向到 Application Insights 资源的检测密钥。The script contains the instrumentation key that directs the telemetry to your Application Insights resource.

将代码添加到站点页面Add the code to your site pages

在母版页上On the master page

如果可以编辑站点的模板页,这会为站点中的每个页面提供监控。If you can edit the site's master page, that will provide monitoring for every page in the site.

签出母版页并使用 SharePoint Designer 或任何其他编辑器编辑它。Check out the master page and edit it using SharePoint Designer or any other editor.

显示如何使用 SharePoint Designer 或任何其他编辑器编辑母版页的屏幕截图。

将代码添加到以下项前面:Add the code just before the 标记。tag.

显示要将代码添加到站点页的位置的屏幕截图。

或在个别页面上Or on individual pages

要监视一组有限的页面,请将脚本分别添加到每个页面。To monitor a limited set of pages, add the script separately to each page.

插入 Web 部分并在其中嵌入代码片段。Insert a web part and embed the code snippet in it.

显示如何添加脚本来监视一组有限页面的屏幕截图。

查看关于应用的数据View data about your app

重新部署应用。Redeploy your app.

返回到 Azure 门户中的应用程序边栏选项卡。Return to your application blade in the Azure portal.

第一批事件会显示在“搜索”中。The first events will appear in Search.

显示可在应用中查看的新数据的屏幕截图。

如果需要更多数据,在几秒后单击“刷新”。Click Refresh after a few seconds if you're expecting more data.

捕获用户 IDCapturing User Id

标准网页代码片段不从 SharePoint 捕获用户 ID,但可在进行少量修改的情况下执行该操作。The standard web page code snippet doesn't capture the user id from SharePoint, but you can do that with a small modification.

  1. 从 Application Insights 中的 Essentials 下拉列表中复制应用的检测密钥。Copy your app's instrumentation key from the Essentials drop-down in Application Insights.

    显示如何从 Application Insights 中的 Essentials 下拉列表中复制应用的检测的屏幕截图。

  2. 在下面代码片段中将“XXXX”替换为检测密钥。Substitute the instrumentation key for 'XXXX' in the snippet below.

  3. 将脚本嵌入 SharePoint 应用,而不是从门户获取的代码片段。Embed the script in your SharePoint app instead of the snippet you get from the portal.



<SharePoint:ScriptLink ID="ScriptLink1" name="SP.js" runat="server" localizable="false" loadafterui="true" /> 
<SharePoint:ScriptLink ID="ScriptLink2" name="SP.UserProfiles.js" runat="server" localizable="false" loadafterui="true" /> 

<script type="text/javascript"> 
var personProperties; 

// Ensure that the SP.UserProfiles.js file is loaded before the custom code runs. 
SP.SOD.executeOrDelayUntilScriptLoaded(getUserProperties, 'SP.UserProfiles.js'); 

function getUserProperties() { 
    // Get the current client context and PeopleManager instance. 
    var clientContext = new SP.ClientContext.get_current(); 
    var peopleManager = new SP.UserProfiles.PeopleManager(clientContext); 

    // Get user properties for the target user. 
    // To get the PersonProperties object for the current user, use the 
    // getMyProperties method. 

    personProperties = peopleManager.getMyProperties(); 

    // Load the PersonProperties object and send the request. 
    clientContext.load(personProperties); 
    clientContext.executeQueryAsync(onRequestSuccess, onRequestFail); 
} 

// This function runs if the executeQueryAsync call succeeds. 
function onRequestSuccess() { 
var appInsights=window.appInsights||function(config){
function s(config){t[config]=function(){var i=arguments;t.queue.push(function(){t[config].apply(t,i)})}}var t={config:config},r=document,f=window,e="script",o=r.createElement(e),i,u;for(o.src=config.url||"//az416426.vo.msecnd.net/scripts/a/ai.0.js",r.getElementsByTagName(e)[0].parentNode.appendChild(o),t.cookie=r.cookie,t.queue=[],i=["Event","Exception","Metric","PageView","Trace"];i.length;)s("track"+i.pop());return config.disableExceptionTracking||(i="onerror",s("_"+i),u=f[i],f[i]=function(config,r,f,e,o){var s=u&&u(config,r,f,e,o);return s!==!0&&t["_"+i](config,r,f,e,o),s}),t
    }({
        instrumentationKey:"XXXX"
    });
    window.appInsights=appInsights;
    appInsights.trackPageView(document.title,window.location.href, {User: personProperties.get_displayName()});
} 

// This function runs if the executeQueryAsync call fails. 
function onRequestFail(sender, args) { 
} 
</script> 


后续步骤Next Steps