Zdravim,
existuje v .Net 3.5 nejaka moznost ako logovat neosetrene vynimky?
Fórum › .NET
Logovanie vynimiek

Nikto nic, tak si odpoviem sam, mozno sa to dakomu zide:
Do triedy Program je potrebne pridat handler na odchytavanie vsetkych neosetrenych vynimiek, nieco ako:
[STAThread]
static void Main()
{
Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new App());
}
public static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
Logger log = new Logger(e.Exception);
}
V triede Logger, ktorej instancia sa vytvara vzdy pri zachyteni neosetrenej vynimky sa vynimka spracuje, nieco ako:
public class Logger
{
public Logger(Exception exception)
{
DialogResult result = DialogResult.Abort;
try
{
result = MessageBox.Show("Hups. Neosetrena chyba!", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop);
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("./chyby"))
Directory.CreateDirectory("./chyby");
using (TextWriter tw = new StreamWriter("./chyby/" + 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;
}
}
}
Vytvori sa subor obsahujuci popis chyby spolu so stack trace informaciami ako napriklad riadok kodu, ktory vynimku vyvolal. Tieto chybove subory viete napr. automaticky zbalit do archivu a odoslat na svoj mail.

Přidej příspěvek
Toto téma je starší jak čtvrt roku – přidej svůj příspěvek jen tehdy, máš-li k tématu opravdu co říct!
Ano, opravdu chci reagovat → zobrazí formulář pro přidání příspěvku
×Vložení zdrojáku
×Vložení obrázku
×Vložení videa
Aktuálně jsou podporována videa ze serverů YouTube, Vimeo a Dailymotion.
×






























































Uživatelé prohlížející si toto vlákno
Uživatelé on-line: 0 registrovaných, 30 hostů
Moderátoři diskuze