Example #1
0
        public void WriteException(Exception exception, string description)
        {
            if (_helper.ShouldLog)
            {
                var stackTrace = new StackTrace();

                var stackTraceInfo = StackTraceParser.Parse(
                    stackTrace.ToString(),
                    (f, t, m, pl, ps, fn, ln) => new
                {
                    Frame         = f,
                    Type          = t,
                    Method        = m,
                    ParameterList = pl,
                    Parameters    = ps,
                    File          = fn,
                    Line          = ln,
                });
                string methodName = null;
                string frame      = null;

                bool @break = false;
                foreach (var stackTraceEntry in stackTraceInfo)
                {
                    if (@break)
                    {
                        methodName = stackTraceEntry.Method;
                        frame      = stackTraceEntry.Frame;

                        break;
                    }

                    var type = stackTraceEntry.Type;
                    if (string.IsNullOrEmpty(type))
                    {
                        continue;
                    }

                    var @typeof = typeof(TraceStepper).Name;
                    if (type.EndsWith(@typeof))
                    {
                        @break = true;
                    }
                }

                var exceptionName  = exception.GetType().Name;
                var exceptionTrace = ExceptionReporting.GetExceptionReport(exception);
                _tracer.WriteException(methodName, frame, exceptionTrace, description, exceptionName);
            }
        }
        public void TestSimpleException()
        {
            try
            {
                throw new Exception("Test exception");
            }
            catch (Exception ex)
            {
                var s = ExceptionReporting.GetExceptionReport(ex);
                Console.WriteLine(s);

                Assert.IsNotNull(s);
                StringAssert.StartsWith(ex.GetType().FullName + ": Test exception", s);

                var match = @"System\.Exception: Test exception\r\n" +
                            @"\s+at ProductionStackTrace\.Test!0x[0-9a-f]+!ProductionStackTrace\.Test\.TestExceptionReporting\.TestSimpleException\(\) \+0x[0-9a-f]+\r\n" +
                            @"==========\r\n" +
                            @"MODULE: ProductionStackTrace\.Test => ProductionStackTrace\.Test, Version=1\.0\.0\.0, Culture=neutral, PublicKeyToken=null; G:[0-9a-f]+; A:[0-9]+\r\n";
                StringAssert.IsMatch(match, s);
            }
        }