コード例 #1
0
        public static string GetDescription(this SerializableException exception)
        {
            var text = string.Empty;

            while (exception != null)
            {
                text     += exception.GetType().Name + ": " + exception.Message + "\r\n";
                text     += exception.StackTrace + "\r\n\r\n";
                exception = exception.InnerException;
            }
            return(text.Trim());
        }
コード例 #2
0
        public void SerializeToXMLTest()
        {
            var sx = new SerializableException();

            sx["Type"]        = "System.Exception";
            sx["DateAndTime"] = DateTime.Now.ToString();
            sx["Message"]     = "Just a Test";

            var sx2 = new SerializableException();

            sx2["Type"]        = "System.Exception2";
            sx2["DateAndTime"] = DateTime.Now.ToString();
            sx2["Message"]     = "Just a Test2";

            sx["InnerException"] = sx2;

            var sx3 = new SerializableException();

            sx3["Type"]        = "System.Exception3";
            sx3["DateAndTime"] = DateTime.Now.ToString();
            sx3["Message"]     = "Just a Test3";

            sx2["InnerException"] = sx3;

            var buf        = "";
            var serializer = new DataContractSerializer(sx.GetType());

            using (var sw = new StringWriter())
            {
                using (var writer = new XmlTextWriter(sw))
                {
                    writer.Formatting = Formatting.Indented;
                    serializer.WriteObject(writer, sx);
                    writer.Flush();
                    buf = sw.ToString();
                }
            }
            buf.Should().Contain("Just a Test");
            buf.Should().Contain("Just a Test2");
            buf.Should().Contain("Just a Test3");
        }
コード例 #3
0
        protected override async Task <bool> UploadExceptionAsync(SerializableException exception)
        {
            using (new InvariantCulture())
            {
                try
                {
                    using (var writer = new StringWriter())
                    {
                        writer.WriteLine("Package: " + PackageName);
                        writer.WriteLine("Version: " + Version);

                        writer.WriteLine("OS: Windows");
                        writer.WriteLine("Windows: " + Environment.OSVersion.Version.ToString());

                        if (!string.IsNullOrEmpty(Manufacturer))
                        {
                            writer.WriteLine("Manufacturer: " + Manufacturer);
                        }

                        if (!string.IsNullOrEmpty(Model))
                        {
                            writer.WriteLine("Model: " + Model);
                        }

                        writer.WriteLine("Date: " + DateTime.Now.ToUniversalTime().ToString("O"));
                        writer.WriteLine("CrashReporter Key: " + InstallationId);

                        var exceptionItem = exception;
                        while (exceptionItem != null)
                        {
                            writer.WriteLine();
                            writer.WriteLine(exception.GetType().FullName + ": " + exception.Message);

                            var stackTrace  = exception.StackTrace ?? new StackTrace().ToString();
                            var stackFrames = StackItem.FromStackTrace(stackTrace);

                            if (stackFrames.Length > 0)
                            {
                                foreach (var stackItem in stackFrames)
                                {
                                    writer.Write($"  at {stackItem.MethodName}");
                                    if (stackItem.SourceFile != null)
                                    {
                                        writer.WriteLine($"({Path.GetFileName(stackItem.SourceFile)}:{stackItem.Line})");
                                    }
                                    else
                                    {
                                        writer.WriteLine();
                                    }
                                }
                            }
                            else
                            {
                                //In case the regex fails somehow
                                writer.WriteLine(stackTrace);
                                writer.WriteLine("(could not parse stacktrace)");
                            }

                            exceptionItem = exceptionItem.InnerException;
                        }

                        writer.Flush();

                        var text = $"raw={HttpUtility.UrlEncode(writer.ToString()).Replace("+", "%20")}" +
                                   "&sdk=HockeySDKWinWPF" +
                                   "&sdk_version=hockeysdk.wpf:4.1.6.1005" +
                                   "&userID=" + InstallationId;

                        var form = new StringContent(text);
                        form.Headers.ContentType.MediaType = "application/x-www-form-urlencoded";
                        form.Headers.ContentType.CharSet   = null;

                        var response = await _httpClient.PostAsync("crashes", form);

                        return(response.StatusCode == HttpStatusCode.Created);
                    }
                }
                catch
                {
                    return(false);
                }
            }
        }
コード例 #4
0
        /// <summary>
        /// Adds all defined routes on a HttpController type
        /// </summary>
        /// <typeparam name="T">Type of HttpControllerBase</typeparam>
        /// <returns>SimpleHttpServer instance</returns>
        public SimpleHttpServer AddHttpControllerRoutes <T>() where T : HttpControllerBase
        {
            var type    = typeof(T);
            var methods = type.GetRuntimeMethods();

            foreach (var m in methods)
            {
                var routes = m.GetCustomAttributes <HttpRouteAttribute>().ToArray();
                if (routes.Any())
                {
                    foreach (var route in routes)
                    {
                        AddRouteHandler(route.Method, route.Url, async context =>
                        {
                            var ctl     = Activator.CreateInstance <T>();
                            ctl.Context = context;
                            var mParams = m.GetParameters();
                            if (mParams.Length > 0)
                            {
                                ParameterInfo postObjectParam = null;
                                object postObject             = null;
                                var ivkParams = new List <object>();
                                if (context.Request.HasPostObject)
                                {
                                    postObjectParam = mParams.FirstOrDefault(p => p.GetCustomAttribute(typeof(PostObjectAttribute)) != null);
                                    if (postObjectParam is null && mParams.Length == 1)
                                    {
                                        postObjectParam = mParams[0];
                                    }
                                    if (postObjectParam != null)
                                    {
                                        postObject = Try.Do(() => context.Request.GetPostObject(postObjectParam.ParameterType));
                                    }
                                }

                                var dictionary = context.Route.GetRouteParameters(context.Request.Url.AbsolutePath);
                                foreach (var mParam in mParams)
                                {
                                    if (mParam == postObjectParam)
                                    {
                                        ivkParams.Add(postObject);
                                    }
                                    else if (dictionary.ContainsKey(mParam.Name))
                                    {
                                        ivkParams.Add(dictionary[mParam.Name]);
                                    }
                                    else
                                    {
                                        ivkParams.Add(null);
                                    }
                                }

                                var response = m.Invoke(ctl, ivkParams.ToArray());
                                if (response is null)
                                {
                                    return;
                                }
                                var serializer = SerializerManager.GetByMimeType(ctl.Context.Response.ContentType);
                                switch (serializer)
                                {
                                case null when response is string:
                                    await ctl.Context.Response.WriteAsync((string)response).ConfigureAwait(false);
                                    break;

                                case null when response is ValueType:
                                    ctl.Context.Response.Write(response.ToString());
                                    break;

                                case null:
                                    serializer = SerializerManager.GetByMimeType(SerializerMimeTypes.Json);
                                    break;
                                }

                                if (serializer != null)
                                {
                                    Try.Do(() => serializer.Serialize(response, response.GetType(), ctl.Context.Response.OutputStream), ex =>
                                    {
                                        var sEx = new SerializableException(ex);
                                        serializer.Serialize(sEx, sEx.GetType(), ctl.Context.Response.OutputStream);
                                    });
                                    await ctl.Context.Response.OutputStream.FlushAsync().ConfigureAwait(false);
                                }
                            }
                            else
                            {
                                var response = m.Invoke(ctl, new object[0]);
                                if (response is null)
                                {
                                    return;
                                }
                                var serializer = SerializerManager.GetByMimeType(ctl.Context.Response.ContentType);
                                switch (serializer)
                                {
                                case null when response is string:
                                    await ctl.Context.Response.WriteAsync((string)response).ConfigureAwait(false);
                                    break;

                                case null when response is ValueType:
                                    ctl.Context.Response.Write(response.ToString());
                                    break;

                                case null:
                                    serializer = SerializerManager.GetByMimeType(SerializerMimeTypes.Json);
                                    break;
                                }

                                if (serializer != null)
                                {
                                    Try.Do(() => serializer.Serialize(response, response.GetType(), ctl.Context.Response.OutputStream), ex =>
                                    {
                                        var sEx = new SerializableException(ex);
                                        serializer.Serialize(sEx, sEx.GetType(), ctl.Context.Response.OutputStream);
                                    });
                                    await ctl.Context.Response.OutputStream.FlushAsync().ConfigureAwait(false);
                                }
                            }
                        });
                    }
                }
            }
            ;
            return(this);
        }