public void IsEnabledReturnsTrueIfTelemetryTrackingIsEnabledInConfiguration()
        {
            var configuration = new TelemetryConfiguration { DisableTelemetry = false };
            var client = new TelemetryClient(configuration);

            Assert.True(client.IsEnabled());
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Initialize telemetry for ApplicationInsights in Azure
        /// </summary>
        public static void InitializeTelemetry()
        {
            Microsoft.ApplicationInsights.WindowsAppInitializer.InitializeAsync(
                WindowsCollectors.Metadata | WindowsCollectors.Session | WindowsCollectors.UnhandledException);

            telemetry = new Microsoft.ApplicationInsights.TelemetryClient();
            telemetry.InstrumentationKey = appInsightsKey;
            TelemetryEnabled             = telemetry.IsEnabled();
        }
        public void IsEnabledReturnsTrueIfTelemetryTrackingIsEnabledInConfiguration()
        {
            var configuration = new TelemetryConfiguration {
                DisableTelemetry = false
            };
            var client = new TelemetryClient(configuration);

            Assert.True(client.IsEnabled());
        }
        /// <summary>
        /// Implement OnException method of IExceptionFilter which will be invoked
        /// for all unhandled exceptions
        /// </summary>
        /// <param name="context"></param>
        public void OnException(ExceptionContext context)
        {
            this._logger.LogError("MatterCenterExceptionFilter", context.Exception);
            var stackTrace = new StackTrace(context.Exception, true);
            StackFrame stackFrameInstance = null;

            if(stackTrace.GetFrames().Length>0)
            {
                for(int i=0; i< stackTrace.GetFrames().Length; i++)
                {
                    if(stackTrace.GetFrames()[i].ToString().Contains("Microsoft.Legal.Matter"))
                    {
                        stackFrameInstance = stackTrace.GetFrames()[i];
                        break;
                    }
                }
            }
            //Create custom exception response that needs to be send to client
            var response = new ErrorResponse()
            {
                Message = context.Exception.Message,
                StackTrace = context.Exception.ToString(),
                Description = "Error occured in the system. Please contact the administrator",
                //Exception = context.Exception.ToString(),
                LineNumber = stackFrameInstance?.GetFileLineNumber(),
                MethodName = stackFrameInstance?.GetMethod().Name,
                ClassName = stackFrameInstance?.GetMethod().DeclaringType.Name,
                ErrorCode = ((int)HttpStatusCode.InternalServerError).ToString()
            };

            //Create properties that need to be added to application insights
            var properties = new Dictionary<string, string>();
            properties.Add("StackTrace", response.StackTrace);
            properties.Add("LineNumber", response.LineNumber.ToString());
            properties.Add("MethodName", response.MethodName.ToString());
            properties.Add("ClassName", response.ClassName.ToString());
            properties.Add("ErrorCode", response.ErrorCode.ToString());           

            //Create Telemetry object to add exception to the application insights
            var ai = new TelemetryClient();
            ai.InstrumentationKey = instrumentationKey;
            if(ai.IsEnabled())
            {
                //add exception to the Application Insights
                ai.TrackException(context.Exception, properties);
            }           
            
            //Send the exceptin object to the client
            context.Result = new ObjectResult(response)
            {
                StatusCode = (int)HttpStatusCode.InternalServerError,
                DeclaredType = typeof(ErrorResponse)                
            };
        }