Beispiel #1
0
        /// <summary>
        /// Converts a System.Diagnostics.StackFrame to a Microsoft.ApplicationInsights.Extensibility.Implementation.TelemetryTypes.StackFrame.
        /// </summary>
        private static External.StackFrame GetStackFrame(StackFrame stackFrame, int frameId)
        {
            var convertedStackFrame = new External.StackFrame()
            {
                level = frameId
            };

            var    methodInfo = stackFrame.GetMethod();
            string fullName;

            if (methodInfo.DeclaringType != null)
            {
                fullName = methodInfo.DeclaringType.FullName + "." + methodInfo.Name;
            }
            else
            {
                fullName = methodInfo.Name;
            }

            convertedStackFrame.method   = fullName;
            convertedStackFrame.assembly = methodInfo.Module.Assembly.FullName;
            convertedStackFrame.fileName = stackFrame.GetFileName();

            // 0 means it is unavailable
            int line = stackFrame.GetFileLineNumber();

            if (line != 0)
            {
                convertedStackFrame.line = line;
            }

            return(convertedStackFrame);
        }
Beispiel #2
0
        /// <summary>
        /// Gets the stack frame length for only the strings in the stack frame.
        /// </summary>
        private static int GetStackFrameLength(External.StackFrame stackFrame)
        {
            var stackFrameLength = (stackFrame.method == null ? 0 : stackFrame.method.Length)
                                   + (stackFrame.assembly == null ? 0 : stackFrame.assembly.Length)
                                   + (stackFrame.fileName == null ? 0 : stackFrame.fileName.Length);

            return(stackFrameLength);
        }
        public void TestNullMethodInfoInStack()
        {
            var frameMock = new Mock <System.Diagnostics.StackFrame>(null, 0, 0);

            frameMock.Setup(x => x.GetMethod()).Returns((MethodBase)null);

            External.StackFrame stackFrame = null;

            try
            {
                stackFrame = ExceptionConverter.GetStackFrame(frameMock.Object, 0);
            }
            catch (Exception e)
            {
                Assert.Fail("GetStackFrame threw " + e);
            }

            Assert.AreEqual("unknown", stackFrame.assembly);
            Assert.AreEqual(null, stackFrame.fileName);
            Assert.AreEqual("unknown", stackFrame.method);
            Assert.AreEqual(0, stackFrame.line);
        }
Beispiel #4
0
        /// <summary>
        /// Create handler for exception telemetry.
        /// </summary>
        private Action <ITelemetry> CreateHandlerForExceptionTelemetry(EventSource eventSource, MethodInfo writeGenericMethod, Type eventSourceOptionsType, PropertyInfo eventSourceOptionsKeywordsProperty)
        {
            var eventSourceOptions = Activator.CreateInstance(eventSourceOptionsType);
            var keywords           = Keywords.Exceptions;

            eventSourceOptionsKeywordsProperty.SetValue(eventSourceOptions, keywords);
            var dummyExceptionData    = new ExceptionData();
            var dummyExceptionDetails = new ExceptionDetails();
            var dummyStackFrame       = new External.StackFrame();
            var writeMethod           = writeGenericMethod.MakeGenericMethod(new
            {
                PartA_iKey          = this.dummyPartAiKeyValue,
                PartA_Tags          = this.dummyPartATagsValue,
                PartB_ExceptionData = new
                {
                    // The properties and layout should be the same as ExceptionData_types.cs
                    dummyExceptionData.ver,
                    exceptions = new[]
                    {
                        new
                        {
                            // The properties and layout should be the same as ExceptionDetails_types.cs
                            dummyExceptionDetails.id,
                            dummyExceptionDetails.outerId,
                            dummyExceptionDetails.typeName,
                            dummyExceptionDetails.message,
                            dummyExceptionDetails.hasFullStack,
                            dummyExceptionDetails.stack,
                            parsedStack = new[]
                            {
                                new
                                {
                                    // The properties and layout should be the same as StackFrame_types.cs
                                    dummyStackFrame.level,
                                    dummyStackFrame.method,
                                    dummyStackFrame.assembly,
                                    dummyStackFrame.fileName,
                                    dummyStackFrame.line,
                                },
                            }.AsEnumerable(),
                        },
                    }.AsEnumerable(),
                    dummyExceptionData.severityLevel,
                    dummyExceptionData.problemId,
                    dummyExceptionData.properties,
                    dummyExceptionData.measurements,
                },
                PartA_flags = this.dummyPartAFlagsValue,
            }.GetType());

            return((item) =>
            {
                if (this.EventSourceInternal.IsEnabled(EventLevel.Verbose, keywords))
                {
                    var telemetryItem = item as ExceptionTelemetry;
                    CopyGlobalPropertiesIfRequired(item, telemetryItem.Properties);
                    item.Sanitize();
                    var data = telemetryItem.Data.Data;
                    var extendedData = new
                    {
                        // The properties and layout should be the same as the anonymous type in the above MakeGenericMethod
                        PartA_iKey = telemetryItem.Context.InstrumentationKey,
                        PartA_Tags = telemetryItem.Context.SanitizedTags,
                        PartB_ExceptionData = new
                        {
                            data.ver,
                            exceptions = data.exceptions.Select(i => new
                            {
                                i.id,
                                i.outerId,
                                i.typeName,
                                i.message,
                                i.hasFullStack,
                                i.stack,
                                parsedStack = i.parsedStack.Select(j => new
                                {
                                    j.level,
                                    j.method,
                                    j.assembly,
                                    j.fileName,
                                    j.line,
                                }),
                            }),
                            data.severityLevel,
                            data.problemId,
                            data.properties,
                            data.measurements,
                        },
                        PartA_flags = telemetryItem.Context.Flags,
                    };

                    writeMethod.Invoke(eventSource, new object[] { ExceptionTelemetry.TelemetryName, eventSourceOptions, extendedData });
                }
            });
        }
        private static void SerializeStackFrame(StackFrame frame, IJsonWriter writer)
        {
            writer.WriteProperty("level", frame.level);
            writer.WriteProperty(
                "method",
                Utils.PopulateRequiredStringValue(frame.method, "StackFrameMethod", typeof(ExceptionTelemetry).FullName));
            writer.WriteProperty("assembly", frame.assembly);
            writer.WriteProperty("fileName", frame.fileName);

            // 0 means it is unavailable
            if (frame.line != 0)
            {
                writer.WriteProperty("line", frame.line);
            }
        }