رؤى التطبيق لتطبيقات وحدة التحكم .NET
تحذير
استخدم حزمة Microsoft.ApplicationInsights.WorkerService والإرشادات المقترنة من تطبيقات Application Insights for Worker Service (تطبيقات غير HTTP) لتطبيقات وحدة التحكم. وهو متوافق مع إصدارات الدعم طويل الأجل (LTS) من .NET Core .NET Framework أو أعلى.
يتيح لك تطبيق نتائج تحليلات مراقبة تطبيقك للويب للتوفر والأداء والاستخدام.
الشروع في العمل
في مدخل Microsoft Azure، قم بإنشاء مورد Application Insights.
خذ نسخة من سلسلة الاتصال. ابحث عن سلسلة الاتصال في القائمة المنسدلة Essentials للمورد الجديد الذي أنشأته.
قم بتثبيت أحدث حزمة Microsoft.ApplicationInsights .
قم بتعيين سلسلة الاتصال في التعليمات البرمجية الخاصة بك قبل تعقب أي بيانات تتبع الاستخدام (أو تعيين
APPLICATIONINSIGHTS_CONNECTION_STRING
متغير البيئة). بعد ذلك، يجب أن تكون قادرا على تتبع بيانات تتبع الاستخدام يدويا ورؤيتها في مدخل Microsoft Azure.// You may use different options to create configuration as shown later in this article TelemetryConfiguration configuration = TelemetryConfiguration.CreateDefault(); configuration.ConnectionString = <Copy connection string from Application Insights Resource Overview>; var telemetryClient = new TelemetryClient(configuration); telemetryClient.TrackTrace("Hello World!");
ملاحظة
لا يتم إرسال بيانات تتبع الاستخدام على الفور. يتم تجميع العناصر وإرسالها بواسطة ApplicationInsights SDK. يتم إنهاء تطبيقات وحدة التحكم بعد استدعاء
Track()
الأساليب.قد لا يتم إرسال بيانات تتبع الاستخدام ما لم
Flush()
Delay
Sleep
/يتم ذلك قبل خروج التطبيق، كما هو موضح في المثال الكامل لاحقا في هذه المقالة.Sleep
غير مطلوب إذا كنت تستخدمInMemoryChannel
.قم بتثبيت أحدث إصدار من حزمة Microsoft.ApplicationInsights.DependencyCollector . يتتبع تلقائيا HTTP أو SQL أو بعض مكالمات التبعية الخارجية الأخرى.
يمكنك تهيئة وتكوين Application Insights من التعليمات البرمجية أو باستخدام ApplicationInsights.config
الملف. تأكد من حدوث التهيئة في أقرب وقت ممكن.
ملاحظة
لا تدعم تطبيقات .NET CoreApplicationInsights.config.
استخدام ملف التكوين
بالنسبة للتطبيقات المستندة إلى .NET Framework، بشكل افتراضي، يبحث Application Insights SDK عن ApplicationInsights.config
الملف في دليل العمل عند TelemetryConfiguration
الإنشاء. قراءة ملف التكوين غير مدعومة على .NET Core.
TelemetryConfiguration config = TelemetryConfiguration.Active; // Reads ApplicationInsights.config file if present
يمكنك أيضا تحديد مسار إلى ملف التكوين:
using System.IO;
TelemetryConfiguration configuration = TelemetryConfiguration.CreateFromConfiguration(File.ReadAllText("C:\\ApplicationInsights.config"));
var telemetryClient = new TelemetryClient(configuration);
يمكنك الحصول على مثال كامل لملف التكوين عن طريق تثبيت أحدث إصدار من حزمة Microsoft.ApplicationInsights.WindowsServer . فيما يلي الحد الأدنى من التكوين لمجموعة التبعية المكافئ لمثال التعليمات البرمجية:
<?xml version="1.0" encoding="utf-8"?>
<ApplicationInsights xmlns="http://schemas.microsoft.com/ApplicationInsights/2013/Settings">
<ConnectionString>"Copy connection string from Application Insights Resource Overview"</ConnectionString>
<TelemetryInitializers>
<Add Type="Microsoft.ApplicationInsights.DependencyCollector.HttpDependenciesParsingTelemetryInitializer, Microsoft.AI.DependencyCollector"/>
</TelemetryInitializers>
<TelemetryModules>
<Add Type="Microsoft.ApplicationInsights.DependencyCollector.DependencyTrackingTelemetryModule, Microsoft.AI.DependencyCollector">
<ExcludeComponentCorrelationHttpHeadersOnDomains>
<Add>core.windows.net</Add>
<Add>core.chinacloudapi.cn</Add>
<Add>core.cloudapi.de</Add>
<Add>core.usgovcloudapi.net</Add>
<Add>localhost</Add>
<Add>127.0.0.1</Add>
</ExcludeComponentCorrelationHttpHeadersOnDomains>
<IncludeDiagnosticSourceActivities>
<Add>Microsoft.Azure.ServiceBus</Add>
<Add>Microsoft.Azure.EventHubs</Add>
</IncludeDiagnosticSourceActivities>
</Add>
</TelemetryModules>
<TelemetryChannel Type="Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.ServerTelemetryChannel, Microsoft.AI.ServerTelemetryChannel"/>
</ApplicationInsights>
تكوين مجموعة بيانات تتبع الاستخدام من التعليمات البرمجية
ملاحظة
قراءة ملف التكوين غير مدعومة على .NET Core.
أثناء بدء تشغيل التطبيق، قم بإنشاء مثيل وتكوينه
DependencyTrackingTelemetryModule
. يجب أن يكون مفردا ويجب الاحتفاظ به طوال مدة بقاء التطبيق.var module = new DependencyTrackingTelemetryModule(); // prevent Correlation Id to be sent to certain endpoints. You may add other domains as needed. module.ExcludeComponentCorrelationHttpHeadersOnDomains.Add("core.windows.net"); //... // enable known dependency tracking, note that in future versions, we will extend this list. // please check default settings in https://github.com/Microsoft/ApplicationInsights-dotnet-server/blob/develop/Src/DependencyCollector/DependencyCollector/ApplicationInsights.config.install.xdt module.IncludeDiagnosticSourceActivities.Add("Microsoft.Azure.ServiceBus"); module.IncludeDiagnosticSourceActivities.Add("Microsoft.Azure.EventHubs"); //.... // initialize the module module.Initialize(configuration);
إضافة مهيئات بيانات تتبع الاستخدام الشائعة:
// ensures proper DependencyTelemetry.Type is set for Azure RESTful API calls configuration.TelemetryInitializers.Add(new HttpDependenciesParsingTelemetryInitializer());
إذا قمت بإنشاء تكوين باستخدام منشئ عادي
TelemetryConfiguration()
، فأنت بحاجة إلى تمكين دعم الارتباط بالإضافة إلى ذلك. لا يلزم ذلك إذا قرأت التكوين من ملف أو استخدمتهTelemetryConfiguration.CreateDefault()
أوTelemetryConfiguration.Active
.configuration.TelemetryInitializers.Add(new OperationCorrelationTelemetryInitializer());
قد ترغب أيضا في تثبيت وحدة مجمع عداد الأداء وتهيئتها كما هو موضح في موقع الويب هذا.
مثال كامل
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.DependencyCollector;
using Microsoft.ApplicationInsights.Extensibility;
using System.Net.Http;
using System.Threading.Tasks;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
TelemetryConfiguration configuration = TelemetryConfiguration.CreateDefault();
configuration.ConnectionString = "removed";
configuration.TelemetryInitializers.Add(new HttpDependenciesParsingTelemetryInitializer());
var telemetryClient = new TelemetryClient(configuration);
using (InitializeDependencyTracking(configuration))
{
// run app...
telemetryClient.TrackTrace("Hello World!");
using (var httpClient = new HttpClient())
{
// Http dependency is automatically tracked!
httpClient.GetAsync("https://microsoft.com").Wait();
}
}
// before exit, flush the remaining data
telemetryClient.Flush();
// Console apps should use the WorkerService package.
// This uses ServerTelemetryChannel which does not have synchronous flushing.
// For this reason we add a short 5s delay in this sample.
Task.Delay(5000).Wait();
// If you're using InMemoryChannel, Flush() is synchronous and the short delay is not required.
}
static DependencyTrackingTelemetryModule InitializeDependencyTracking(TelemetryConfiguration configuration)
{
var module = new DependencyTrackingTelemetryModule();
// prevent Correlation Id to be sent to certain endpoints. You may add other domains as needed.
module.ExcludeComponentCorrelationHttpHeadersOnDomains.Add("core.windows.net");
module.ExcludeComponentCorrelationHttpHeadersOnDomains.Add("core.chinacloudapi.cn");
module.ExcludeComponentCorrelationHttpHeadersOnDomains.Add("core.cloudapi.de");
module.ExcludeComponentCorrelationHttpHeadersOnDomains.Add("core.usgovcloudapi.net");
module.ExcludeComponentCorrelationHttpHeadersOnDomains.Add("localhost");
module.ExcludeComponentCorrelationHttpHeadersOnDomains.Add("127.0.0.1");
// enable known dependency tracking, note that in future versions, we will extend this list.
// please check default settings in https://github.com/microsoft/ApplicationInsights-dotnet-server/blob/develop/WEB/Src/DependencyCollector/DependencyCollector/ApplicationInsights.config.install.xdt
module.IncludeDiagnosticSourceActivities.Add("Microsoft.Azure.ServiceBus");
module.IncludeDiagnosticSourceActivities.Add("Microsoft.Azure.EventHubs");
// initialize the module
module.Initialize(configuration);
return module;
}
}
}