Created
March 9, 2017 17:53
-
-
Save debugthings/e0da4b3384a77a73959744bf9a695eca to your computer and use it in GitHub Desktop.
Implement Application Insights in Code as the HttpModule does
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Constants stolen from | |
| // https://github.com/Microsoft/ApplicationInsights-dotnet-server/blob/v2.2.0/Src/Web/Web.Shared.Net/Implementation/RequestTrackingConstants.cs | |
| 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 MvcApplication : System.Web.HttpApplication | |
| { | |
| private RequestTrackingTelemetryModule requestModule; | |
| private ExceptionTrackingTelemetryModule exceptionModule; | |
| /// <summary> | |
| /// Indicates if module initialized successfully. | |
| /// </summary> | |
| private bool isEnabled = true; | |
| protected void Application_Start() | |
| { | |
| try | |
| { | |
| // The call initializes TelemetryConfiguration that will create and Intialize modules | |
| TelemetryConfiguration configuration = TelemetryConfiguration.Active; | |
| 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 | |
| } | |
| } | |
| protected void Application_BeginRequest(object sender, EventArgs e) | |
| { | |
| // Set the stream filter to be your custom filter | |
| if (this.isEnabled) | |
| { | |
| HttpApplication httpApplication = (HttpApplication)sender; | |
| this.TraceCallback("OnBegin", httpApplication); | |
| if (this.requestModule != null) | |
| { | |
| this.requestModule.OnBeginRequest(httpApplication.Context); | |
| } | |
| } | |
| } | |
| protected void Application_EndRequest(object sender, EventArgs eventArgs) | |
| { | |
| if (this.isEnabled) | |
| { | |
| var httpApplication = (HttpApplication)sender; | |
| this.TraceCallback("OnEndRequest", httpApplication); | |
| if (this.IsFirstRequest(httpApplication)) | |
| { | |
| if (this.exceptionModule != null) | |
| { | |
| this.exceptionModule.OnError(httpApplication.Context); | |
| } | |
| if (this.requestModule != null) | |
| { | |
| this.requestModule.OnEndRequest(httpApplication.Context); | |
| } | |
| } | |
| else | |
| { | |
| // Request was filtered out by the config file | |
| } | |
| } | |
| } | |
| private bool IsFirstRequest(HttpApplication application) | |
| { | |
| var firstRequest = true; | |
| try | |
| { | |
| if (application.Context != null) | |
| { | |
| firstRequest = application.Context.Items[RequestTrackingConstants.EndRequestCallFlag] == null; | |
| if (firstRequest) | |
| { | |
| application.Context.Items.Add(RequestTrackingConstants.EndRequestCallFlag, true); | |
| } | |
| } | |
| } | |
| catch (Exception exc) | |
| { | |
| // TODO Do something with this exception | |
| // This means that | |
| } | |
| return firstRequest; | |
| } | |
| private void TraceCallback(string callback, HttpApplication application) | |
| { | |
| // TODO Implement Some Local Tracing | |
| // In the acutal AI SDK They log to their implemented trace logger | |
| // The logger is sealed for some and internal for others | |
| // https://github.com/Microsoft/ApplicationInsights-dotnet-server/blob/v2.2.0/Src/Web/Web.Shared.Net/Implementation/WebEventSource.cs | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment