2017-09-15 1 views
0

Ich habe einen WCF-Dienst, wo ich Timings mit Application Insights SDK auf diese Weise messe.Wie injiziere ich Application Insights-Code, um das Timing in allen Methoden zu überwachen

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)] 
    [ServiceBehavior(IncludeExceptionDetailInFaults = true)] 
    [BasicHttpBindingServiceMetadataExchangeEndpoint] 
    public class DMSWebService : IDMSWebService 
    { 
     public static readonly string LoggingPrefix = "DMSWebService"; 
     private TelemetryClient telemetry; 

     //Defines if Application Insights must be enabled or not 
     public Boolean EnableApplicationInsights { get; set; } 

     //Application Insights Instrumentation Key 
     public string ApplicationInsightsMonitoringKey { get; set; } 

     //Constructor to get the property bag values only once. 
     public DMSWebService() 
     { 
      InitializeApplicationInsights(); 
      telemetry= new TelemetryClient {InstrumentationKey = ApplicationInsightsMonitoringKey}; 
     } 

     //Method to initialize ApplicationInsightSettings 
     private void InitializeApplicationInsights() 
     { 
      bool enableApplicationInsights = false; 
      using (var billingSite = BillingManagement.GetBillingSite()) 
      { 
       enableApplicationInsights = Convert.ToBoolean(billingSite.WebApplication.GetProperty(Constants.WebApplicationSettings.EnableApplicationInsights)); 
       if(enableApplicationInsights) ApplicationInsightsMonitoringKey = billingSite.WebApplication.GetProperty(Constants.WebApplicationSettings.ApplicationInsightsKey); 
      } 

      EnableApplicationInsights = enableApplicationInsights; 
     } 
     #region Billing 

     #region Archiving 

     // GET 
     public DMSServiceResult ArchiveBillCycle(string listItemId) 
     { 
      var stopwatch = System.Diagnostics.Stopwatch.StartNew(); 

      using (var withDMSServiceResult = new WithDMSServiceResult(LoggingPrefix, "ArchiveBillCycle")) 
      { 
       try 
       { 
        withDMSServiceResult.InputParameters["listItemId"] = listItemId; 

        var listItemIdAsInt = Convert.ToInt32(listItemId); 

        using (var billingSite = BillingManagement.GetBillingSite()) 
        { 
         // HACK: Necessary to disable form digest validation, which we don't need. 
         using (var continueWithoutSPContext = new ContinueWithoutSPContext()) 
         { 
          withDMSServiceResult.RequestSucceeded = BillingRepository.ArchiveBillCycle(billingSite.RootWeb, listItemIdAsInt); 
         } 
        } 
       } 
       catch (Exception ex) 
       { 
        telemetry.TrackException(ex); 
        withDMSServiceResult.HandleError(ex); 
       } 

       stopwatch.Stop(); 
       var metrics = new Dictionary <string, double>{{"processingTime", stopwatch.Elapsed.TotalMilliseconds}}; 
       // Set up some properties: 
       var properties = new Dictionary <string, string>{{"listItemId", withDMSServiceResult.InputParameters["listItemId"]}}; 
       if(EnableApplicationInsights) telemetry.TrackEvent("ArchiveBillCycle", properties, metrics); 

       return withDMSServiceResult.Result; 
      } 
     } 

     #endregion 

Wie man sehen kann ich eine Stoppuhr am Anfang des Verfahrens beginnen und dann schicke ich die Veranstaltung Praktische Anwendung am Ende des Verfahrens.

Doing dies für alle 10 Methoden auf dem Web-Service ist keine große Sache, ich habe es schon getan.

Diese Methoden rufen jedoch Dienstprogramme-Methoden in anderen Klassen auf, und der einzige Weg, den Engpass zu finden, besteht darin, jede der Methoden zu messen.

Was wäre Ihr Vorschlag?

Bitte beachten Sie, dass das TrackEvent ein Eigenschaftsfeld hat, das ich manchmal benutze, manchmal sende ich einfach null.

Thx

Antwort

1

Suchen Sie nach einem AOP (Aspect Oriented Programming) Framework wie PostSharp (gegen Entgelt) oder so etwas wie Aspectize oder Castle DynamicProxy verwenden. There Frameworks ermöglicht Ihnen, einige benutzerdefinierte Logik einmal zu schreiben und sie zur Laufzeit oder Kompilierungszeit für alle angegebenen Methoden anzuwenden. Ein Beispiel finden Sie in this post.

Für WCF können Sie es einfacher machen, da die Unterstützung für das Abfangen von Anrufen unter Verwendung von beispielsweise Message Inspectors integriert ist.

Verwandte Themen