0
我有一個WCF服務,在這裏我使用Application Insights SDK以這種方式測量時間。如何注入Application Insights代碼來監視所有方法中的時間點
[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
正如你可以看到我的方法開始啓動秒錶,然後我在方法結束髮送事件的應用見解。
爲web服務上的所有10種方法做這件事並不是什麼大事,我已經做到了。
然而,這些方法在其他類中調用實用程序方法,找到瓶頸的唯一方法是測量每種方法。
你的建議是什麼?
請注意,trackEvent有一個屬性字段,有時我使用它,有時我只是發送空。
THX