Skip to content

Commit

Permalink
捕获 UI 意外事件并写入日志
Browse files Browse the repository at this point in the history
  • Loading branch information
STBBRD committed Oct 19, 2024
1 parent 398ebb8 commit ccf2a76
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
3 changes: 2 additions & 1 deletion ZongziTEK_Blackboard_Sticker/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ZongziTEK_Blackboard_Sticker"
StartupUri="MainWindow.xaml"
xmlns:ui="http://schemas.inkore.net/lib/ui/wpf/modern" xmlns:ikw="http://schemas.inkore.net/lib/ui/wpf">
xmlns:ui="http://schemas.inkore.net/lib/ui/wpf/modern" xmlns:ikw="http://schemas.inkore.net/lib/ui/wpf"
DispatcherUnhandledException="Application_DispatcherUnhandledException">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
Expand Down
13 changes: 12 additions & 1 deletion ZongziTEK_Blackboard_Sticker/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using MessageBox = iNKORE.UI.WPF.Modern.Controls.MessageBox;
using AutoUpdaterDotNET;
using System.Globalization;
using ZongziTEK_Blackboard_Sticker.Helpers;

namespace ZongziTEK_Blackboard_Sticker
{
Expand All @@ -32,6 +33,16 @@ void App_Startup(object sender, StartupEventArgs e)
Environment.Exit(0);
}
}

private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
try
{
e.Handled = true;
LogHelper.WriteLogToFile(e.Exception.Message, LogHelper.LogType.Error);
}
catch { }
}
}

[ValueConversion(typeof(bool), typeof(bool))]
Expand Down Expand Up @@ -98,7 +109,7 @@ public object Convert(object value, Type targetType, object parameter, CultureIn

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
throw new NotImplementedException();
}
}
}
57 changes: 57 additions & 0 deletions ZongziTEK_Blackboard_Sticker/Helpers/LogHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ZongziTEK_Blackboard_Sticker.Helpers
{
class LogHelper
{
public static string LogFile = "Log.txt";

public static void NewLog(string str)
{
WriteLogToFile(str, LogType.Info);
}

public static void NewLog(Exception ex)
{

}

public static void WriteLogToFile(string str, LogType logType = LogType.Info)
{
string strLogType = "Info";
switch (logType)
{
case LogType.Event:
strLogType = "Event";
break;
case LogType.Trace:
strLogType = "Trace";
break;
case LogType.Error:
strLogType = "Error";
break;
}
try
{
var file = MainWindow.GetDataPath() + LogFile;
StreamWriter sw = new StreamWriter(file, true);
sw.WriteLine(string.Format("{0} [{1}] {2}", DateTime.Now.ToString("O"), strLogType, str));
sw.Close();
}
catch { }
}

public enum LogType
{
Info,
Trace,
Error,
Event
}
}
}

0 comments on commit ccf2a76

Please sign in to comment.