Error log

zdroj/source(user manro): http://programujte.com/?akce=diskuze&kam=vlakno&tema=16794-logovanie-vynimiek#

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }

        public static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            Logger log = new Logger(e.Exception);
        }
    }
}

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.Diagnostics;
using System.IO;  

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Convert.ToInt64("test");
        }
    }

    public class Logger
    {
        public Logger(Exception exception)
        {
            DialogResult result = DialogResult.Abort;
            try
            {
                result = MessageBox.Show("Neosetrena chyba!");

                StringBuilder error = new StringBuilder();

                error.AppendLine("Aplikácia:              " + Application.ProductName);
                error.AppendLine("Verzia:                 " + Application.ProductVersion);
                error.AppendLine("Dátum a čas:            " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"));
                error.AppendLine("Názov počítača:         " + SystemInformation.ComputerName);
                error.AppendLine("Užívateľ:               " + SystemInformation.UserName);
                error.AppendLine("OS:                     " + Environment.OSVersion.ToString());
                error.AppendLine("Regionálne nastavenie:  " + CultureInfo.CurrentCulture.Name);
                error.AppendLine("Rozlíšenie:             " + SystemInformation.PrimaryMonitorSize.ToString());
                error.AppendLine("Čas behu systému:       " + GetSystemUpTime());
                error.AppendLine("Čas behu aplikácie:     " + (DateTime.Now - Process.GetCurrentProcess().StartTime).ToString());

                error.AppendLine("");

                error.AppendLine("Trieda výnimky:   ");
                error.Append(GetExceptionTypeStack(exception));
                error.AppendLine("");
                error.AppendLine("Správa výnimky: ");
                error.Append(GetExceptionMessageStack(exception));

                error.AppendLine("");
                error.AppendLine("Stack Traces:");
                error.Append(GetExceptionCallStack(exception));

                if (!Directory.Exists("./error"))
                    Directory.CreateDirectory("./error");

                using (TextWriter tw = new StreamWriter("./error/" + DateTime.Now.ToString("dd-MM-yyyy--HH-mm-ss") + ".txt"))
                {
                    tw.Write(error);
                }
            }
            finally
            {
                if (result == DialogResult.Abort)
                {
                    Application.Exit();
                }
            }
        }

        private static TimeSpan GetSystemUpTime()
        {
            PerformanceCounter upTime = new PerformanceCounter("System", "System Up Time");
            upTime.NextValue();
            return TimeSpan.FromSeconds(upTime.NextValue());
        }

        private static string GetExceptionTypeStack(Exception e)
        {
            if (e.InnerException != null)
            {
                StringBuilder message = new StringBuilder();
                message.AppendLine(GetExceptionTypeStack(e.InnerException));
                message.AppendLine("   " + e.GetType().ToString());
                return (message.ToString());
            }
            else
            {
                return "   " + e.GetType().ToString();
            }
        }

        private static string GetExceptionMessageStack(Exception e)
        {
            if (e.InnerException != null)
            {
                StringBuilder message = new StringBuilder();
                message.AppendLine(GetExceptionMessageStack(e.InnerException));
                message.AppendLine("   " + e.Message);
                return (message.ToString());
            }
            else
            {
                return "   " + e.Message;
            }
        }

        private static string GetExceptionCallStack(Exception e)
        {
            if (e.InnerException != null)
            {
                StringBuilder message = new StringBuilder();
                message.AppendLine(GetExceptionCallStack(e.InnerException));
                message.AppendLine("--- Next Call Stack:");
                message.AppendLine(e.StackTrace);
                return (message.ToString());
            }
            else
            {
                return e.StackTrace;
            }
        }
    }
}
 

Leave a Reply