Liens
|
// Reads ApplicationInsights.config file in the working directory
TelemetryConfiguration config = TelemetryConfiguration.Active;
// Reads a specific ApplicationInsights.config file
TelemetryConfiguration configuration = TelemetryConfiguration.CreateFromConfiguration(File.ReadAllText("C:\\ApplicationInsights.config"));
var telemetryClient = new TelemetryClient(configuration);
|
Dependency injection
Prism WPF application
App.xaml.cs
|
// classe de type PrismApplication qui hérite de PrismApplicationBase (Prism.DryIoc)
public partial class App
{
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
var config = TelemetryConfiguration.Active; // Reads ApplicationInsights.config file if present
var telemetryClient = new TelemetryClient(config);
containerRegistry.RegisterInstance(telemetryClient);
}
|
Send telemetry to Application Insights
|
// create a dependency
using (telemetryClient.StartOperation<DependencyTelemetry>("Operation name"))
{
}
// log a trace
telemetryClient.TrackTrace("log message");
|
- Adding the NuGet package Microsoft.ApplicationInsights.DependencyCollector will propagate the W3C Trace-Context from the caller (client) to the service (controller) link.
- Adding the NuGet package Microsoft.ApplicationInsights.Web will automatically log all the incoming Request.
- Add the NuGet package Microsoft.ApplicationInsights.DependencyCollector
ApplicationInsights.conf
|
<?xml version="1.0" encoding="utf-8"?>
<ApplicationInsights xmlns="http://schemas.microsoft.com/ApplicationInsights/2013/Settings">
<TelemetryModules>
<Add Type="Microsoft.ApplicationInsights.DependencyCollector.DependencyTrackingTelemetryModule, Microsoft.AI.DependencyCollector">
<EnableSqlCommandTextInstrumentation>true</EnableSqlCommandTextInstrumentation>
</Add>
|
App_Start\WebApiConfig.cs
|
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
/* ... */
config.Services.Add(typeof(IExceptionLogger), new AiExceptionLogger());
|
AiExceptionLogger.cs
|
public class AiExceptionLogger : ExceptionLogger
{
public TelemetryClient AppInsights { get; set; }
public AiExceptionLogger()
{
AppInsights = new TelemetryClient();
}
public override void Log(ExceptionLoggerContext context)
{
if (context != null && context.Exception != null)
{
AppInsights.TrackException(context.Exception);
}
base.Log(context);
|
Visualiser les données dans visual studio
Cloud Explorer → My Subcription → Application Insights → clique-droit sur My Application Insight → Search
Installation
- Automatique: Azure web app → Monitoring → Application Insights → No data? Automatically instrument your ASP.NET app (restart required)
- Manuel: Azure web app → Extensions → Add → Application Insights
- Tous: Clique-droit sur le projet → Add → Application Insights Telemetry
- Exception tracking: Microsoft.ApplicationInsights.Web
ApplicationInsights.config
|
<?xml version="1.0" encoding="utf-8"?>
<ApplicationInsights xmlns="http://schemas.microsoft.com/ApplicationInsights/2013/Settings">
<TelemetryInitializers>
<!-- ... -->
</TelemetryInitializers>
<TelemetryModules>
<!-- ... -->
</TelemetryModules>
<TelemetryProcessors>
<!-- ... -->
</TelemetryProcessors>
<TelemetryChannel Type="Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.ServerTelemetryChannel, Microsoft.AI.ServerTelemetryChannel"/>
</ApplicationInsights>
|
Modules
|
Description
|
Package Nuget
|
Dependency Tracking |
appels au bdd et services externes |
Microsoft.ApplicationInsights.DependencyCollector
|
Performance collector |
CPU, mémoire, charge réseau |
Microsoft.ApplicationInsights.PerfCounterCollector
|
Application Insights Diagnostics Telemetry |
erreur dans l'App Insight elle-même |
Microsoft.ApplicationInsights
|
Developer Mode |
force l'envoie des données immédiatement lorsque le debugger est attaché |
Microsoft.ApplicationInsights.WindowsServer
|
Web Request Tracking |
code et temps de réponse des requêtes HTTP |
Microsoft.ApplicationInsights.Web
|
Exception tracking |
exceptions non-gérées |
Microsoft.ApplicationInsights.Web
|
EventSource Tracking |
envoyer les EventSource comme des traces |
Microsoft.ApplicationInsights.EventSourceListener
|
ETW Event Tracking |
envoyer les événements des ETW providers comme des traces |
Microsoft.ApplicationInsights.EtwCollector
|
Microsoft.ApplicationInsights |
Core API pour le SDK |
Microsoft.ApplicationInsights
|
Erreurs
These warnings are expected as computer level counters are not accessible from the Azure Web App environment. There's no way to turn off any particular counter from the default counters collection, so please safely ignore them as it doesn't affect anything.
AI: Server telemetry channel was not initialized
AI: Server telemetry channel was not initialized. So persistent storage is turned off. You need to call ServerTelemetryChannel.Initialize(). Currently monitoring will continue but if telemetry cannot be sent it will be dropped.
Le fichier ApplicationInsights.config est manquant.