csharp 在Migraged Azure移动服务应用程序上启用应用程序洞察

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 在Migraged Azure移动服务应用程序上启用应用程序洞察相关的知识,希望对你有一定的参考价值。

internal static class RequestTrackingConstants
  {
      /// <summary>
      /// Name of the HttpContext item containing RequestTelemetry object.
      /// </summary>
      internal const string RequestTelemetryItemName = "Microsoft.ApplicationInsights.RequestTelemetry";

      internal const string EndRequestCallFlag = "Microsoft.ApplicationInsights.EndRequestCallFlag";

      /// <summary>
      /// Type name for the transfer handler. This handler is used to enable extension(less) URI 
      /// and it produces extra request, which should not be counted.
      /// </summary>
      internal const string TransferHandlerType = "System.Web.Handlers.TransferRequestHandler";

      /// <summary>
      /// The name of the cookie which holds authenticated user context information.
      /// </summary>
      internal const string WebAuthenticatedUserCookieName = "ai_authUser";
  }

  public class AppInsightsFilter : System.Web.Http.Filters.ActionFilterAttribute
  {
      private static TelemetryConfiguration ApplicationInsightsConfig = null;
      private RequestTrackingTelemetryModule requestModule;
      private ExceptionTrackingTelemetryModule exceptionModule;

      /// <summary>
      /// Indicates if module initialized successfully.
      /// </summary>
      private bool isEnabled = true;

      private static void CreateConfig(string iKey)
      {
          if (ApplicationInsightsConfig != null)
          {
              return;
          }
          TelemetryModules.Instance.Modules.Add(new DependencyTrackingTelemetryModule());
          TelemetryModules.Instance.Modules.Add(new PerformanceCollectorModule());
          TelemetryModules.Instance.Modules.Add(new QuickPulseTelemetryModule());
          TelemetryModules.Instance.Modules.Add(new UnhandledExceptionTelemetryModule());

          var reqTrackingModule = new RequestTrackingTelemetryModule();
          reqTrackingModule.Handlers.Add("System.Web.Handlers.TransferRequestHandler");
          reqTrackingModule.Handlers.Add("Microsoft.VisualStudio.Web.PageInspector.Runtime.Tracing.RequestDataHttpHandler");
          reqTrackingModule.Handlers.Add("System.Web.StaticFileHandler");
          reqTrackingModule.Handlers.Add("System.Web.Handlers.AssemblyResourceLoader");
          reqTrackingModule.Handlers.Add("System.Web.Optimization.BundleHandler");
          reqTrackingModule.Handlers.Add("System.Web.Script.Services.ScriptHandlerFactory");
          reqTrackingModule.Handlers.Add("System.Web.Handlers.TraceHandler");
          reqTrackingModule.Handlers.Add("System.Web.Services.Discovery.DiscoveryRequestHandler");
          reqTrackingModule.Handlers.Add("System.Web.HttpDebugHandler");
          TelemetryModules.Instance.Modules.Add(reqTrackingModule);

          TelemetryModules.Instance.Modules.Add(new ExceptionTrackingTelemetryModule());


          ApplicationInsightsConfig = new TelemetryConfiguration();

          ApplicationInsightsConfig.TelemetryInitializers.Add(new HttpDependenciesParsingTelemetryInitializer());
          ApplicationInsightsConfig.TelemetryInitializers.Add(new AzureRoleEnvironmentTelemetryInitializer());
          ApplicationInsightsConfig.TelemetryInitializers.Add(new AzureWebAppRoleEnvironmentTelemetryInitializer());
          ApplicationInsightsConfig.TelemetryInitializers.Add(new BuildInfoConfigComponentVersionTelemetryInitializer());
          ApplicationInsightsConfig.TelemetryInitializers.Add(new WebTestTelemetryInitializer());

          var syntheticAgentIntializer = new SyntheticUserAgentTelemetryInitializer();
          syntheticAgentIntializer.Filters = "search|spider|crawl|Bot|Monitor|BrowserMob|BingPreview|PagePeeker|WebThumb|URL2PNG|ZooShot|GomezA|Google SketchUp|Read Later|KTXN|KHTE|Keynote|Pingdom|AlwaysOn|zao|borg|oegp|silk|Xenu|zeal|NING|htdig|lycos|slurp|teoma|voila|yahoo|Sogou|CiBra|Nutch|Java|JNLP|Daumoa|Genieo|ichiro|larbin|pompos|Scrapy|snappy|speedy|vortex|favicon|indexer|Riddler|scooter|scraper|scrubby|WhatWeb|WinHTTP|voyager|archiver|Icarus6j|mogimogi|Netvibes|altavista|charlotte|findlinks|Retreiver|TLSProber|WordPress|wsr-agent|http client|Python-urllib|AppEngine-Google|semanticdiscovery|facebookexternalhit|web/snippet|Google-HTTP-Java-Client";
          ApplicationInsightsConfig.TelemetryInitializers.Add(syntheticAgentIntializer);

          ApplicationInsightsConfig.TelemetryInitializers.Add(new ClientIpHeaderTelemetryInitializer());
          ApplicationInsightsConfig.TelemetryInitializers.Add(new OperationNameTelemetryInitializer());
          ApplicationInsightsConfig.TelemetryInitializers.Add(new Microsoft.ApplicationInsights.Web.OperationCorrelationTelemetryInitializer());
          ApplicationInsightsConfig.TelemetryInitializers.Add(new UserTelemetryInitializer());
          ApplicationInsightsConfig.TelemetryInitializers.Add(new AuthenticatedUserIdTelemetryInitializer());
          ApplicationInsightsConfig.TelemetryInitializers.Add(new AccountIdTelemetryInitializer());
          ApplicationInsightsConfig.TelemetryInitializers.Add(new SessionTelemetryInitializer());

          ApplicationInsightsConfig.TelemetryChannel = new ServerTelemetryChannel();

          ApplicationInsightsConfig.TelemetryProcessorChainBuilder.Use((next) =>
          {
              var qpproc = new QuickPulseTelemetryProcessor(next);
              qpproc.Initialize(ApplicationInsightsConfig);
              return qpproc;
          });

          ApplicationInsightsConfig.TelemetryProcessorChainBuilder.Use((next) =>
          {
              var qpproc = new AdaptiveSamplingTelemetryProcessor(next);
              qpproc.MaxTelemetryItemsPerSecond = 16000; // MAX value allowed
              return qpproc;
          });


          foreach (var item in TelemetryModules.Instance.Modules)
          {
              item.Initialize(ApplicationInsightsConfig);
          }

          ApplicationInsightsConfig.InstrumentationKey = iKey;

      }
      public AppInsightsFilter(string iKey)
      {
          try
          {
              CreateConfig(iKey);
              foreach (var module in TelemetryModules.Instance.Modules)
              {
                  if (module is RequestTrackingTelemetryModule)
                  {
                      this.requestModule = (RequestTrackingTelemetryModule)module;
                  }
                  else
                  {
                      if (module is ExceptionTrackingTelemetryModule)
                      {
                          this.exceptionModule = (ExceptionTrackingTelemetryModule)module;
                      }
                  }
              }
          }
          catch (Exception exc)
          {
              this.isEnabled = false;
              // TODO Log this exception
              // In the SDK we log this to our logger
              // This exception means that something went wrong when trying to access the TelemetryModules instance
          }

      }

      public override void OnActionExecuting(HttpActionContext actionContext)
      {
          if (this.isEnabled)
          {
              if (this.requestModule != null)
              {
                  this.requestModule.OnBeginRequest(System.Web.HttpContext.Current);
              }
          }
          base.OnActionExecuting(actionContext);
      }

      public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
      {
          if (this.isEnabled)
          {

              if (this.IsFirstRequest(System.Web.HttpContext.Current))
              {
                  if (this.exceptionModule != null)
                  {
                      this.exceptionModule.OnError(System.Web.HttpContext.Current);
                  }

                  if (this.requestModule != null)
                  {
                      this.requestModule.OnEndRequest(System.Web.HttpContext.Current);
                  }
              }
              else
              {
                  // Request was filtered out by the config file
              }
          }

          base.OnActionExecuted(actionExecutedContext);
      }

      private bool IsFirstRequest(System.Web.HttpContext application)
      {
          var firstRequest = true;
          try
          {
              if (application != null)
              {
                  firstRequest = application.Items[RequestTrackingConstants.EndRequestCallFlag] == null;
                  if (firstRequest)
                  {
                      application.Items.Add(RequestTrackingConstants.EndRequestCallFlag, true);
                  }
              }
          }
          catch (Exception exc)
          {
              // TODO Do something with this exception
              // This means that 
          }

          return firstRequest;
      }

  }

以上是关于csharp 在Migraged Azure移动服务应用程序上启用应用程序洞察的主要内容,如果未能解决你的问题,请参考以下文章

csharp Azure事件中心

csharp Azure Batch

csharp Azure功能

csharp Azure搜索服务

csharp Azure搜索服务

csharp Azure存储Blob