Esempio n. 1
0
    internal static void LogUnhandledException(Exception exception)
    {
        try
        {
            StringBuilder str = new StringBuilder();

            // MTU info
            Mtu mtu = Singleton.Get.Action.CurrentMtu;

            str.AppendLine(DateTime.Now.ToString());
            str.AppendLine("Unhandled Exception");

            str.AppendLine("");
            str.AppendLine("MTU");
            str.AppendLine("---");
            str.AppendLine(string.Format("{0,-50} : {1}", "MTU", mtu.Id));
            str.AppendLine(string.Format("{0,-50} : {1}", "SpecialSet", mtu.SpecialSet));
            str.AppendLine(string.Format("{0,-50} : {1}", "HexNumber", mtu.HexNum));
            str.AppendLine(string.Format("{0,-50} : {1}", "Num.Ports", mtu.Ports.Count));

            // Action info
            MTUComm.Action action = Singleton.Get.Action;
            str.AppendLine("");
            str.AppendLine("Action");
            str.AppendLine("------");
            str.AppendLine(string.Format("{0,-50} : {1}", "Type", action.type));
            str.AppendLine(string.Format("{0,-50} : {1}", "User", action.user));

            // Add current values in Global.xml
            str.AppendLine("");
            str.AppendLine("Global.XML");
            str.AppendLine("----------");
            Global global = Singleton.Get.Configuration.Global;
            foreach (var property in global.GetType().GetProperties())
            {
                if (!property.GetType().IsArray&&
                    property.CanRead)
                {
                    var value = property.GetValue(global);
                    if (!(value is null))
                    {
                        str.AppendLine(string.Format("{0,-50} : {1}", property.Name, value));
                    }
                }
            }

            str.AppendLine("");
            str.AppendLine("Exception");
            str.AppendLine("---------");

            StackTrace traces = new StackTrace(exception.InnerException, true);

            var capturedTraces = typeof(StackTrace).GetField("captured_traces", BindingFlags.Instance | BindingFlags.NonPublic)
                                 .GetValue(traces) as StackTrace[];

            string traces2 = exception.InnerException.StackTrace;
            foreach (StackTrace trace in capturedTraces)
            {
                foreach (StackFrame frame in trace.GetFrames())
                {
                    str.AppendLine(frame.GetFileName() + ".." + Environment.NewLine +
                                   frame.GetMethod() + " at line " + frame.GetFileLineNumber() + ", column " + frame.GetFileColumnNumber());
                }
            }

            str.AppendLine("");
            str.AppendLine("---------");
            str.AppendLine(exception.InnerException.ToString());

            string errorFileName = string.Format("{0}_{1}_{2}.txt", "Exception", action.type, DateTime.Now.ToString("MM-dd-yyyy_HH-mm"));
            var    libraryPath   = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            var    errorFilePath = Path.Combine(libraryPath, errorFileName);
            File.WriteAllText(errorFilePath, str.ToString());

            str.Clear();
            str = null;
        }
        catch
        {
            // just suppress any error logging exceptions
        }
    }