wpf 全局异常捕捉+错误日志记录+自动创建桌面图标

///


/// 创建桌面图标
///

public static void CreateShortcutOnDesktop(string LnkName)
{

        String shortcutPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), LnkName + ".lnk");
        if (!System.IO.File.Exists(shortcutPath))
        {
            string AppName = System.IO.Path.GetFileName(System.Reflection.Assembly.GetEntryAssembly().GetName().Name);
            // 获取当前应用程序目录地址
            String exePath = AppDomain.CurrentDomain.BaseDirectory + AppName + ".exe";
            IW.IWshShell shell = new IW.WshShell();
            // 确定是否已经创建的快捷键被改名了
            foreach (var item in Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "*.lnk"))
            {
                IW.WshShortcut tempShortcut = (IW.WshShortcut)shell.CreateShortcut(item);
                if (tempShortcut.TargetPath == exePath)
                {
                    return;
                }
            }
            IW.WshShortcut shortcut = (IW.WshShortcut)shell.CreateShortcut(shortcutPath);
            shortcut.TargetPath = exePath;
            shortcut.Arguments = "";// 参数  
            shortcut.Description = AppName + exePath;
            shortcut.WorkingDirectory = Environment.CurrentDirectory;//程序所在文件夹,在快捷方式图标点击右键可以看到此属性  
            shortcut.IconLocation = exePath;//图标,该图标是应用程序的资源文件  
            //shortcut.Hotkey = "CTRL+SHIFT+W";//热键,发现没作用,大概需要注册一下  
            shortcut.WindowStyle = 1;
            shortcut.Save();
            MessageBox.Show("桌面快捷方式已创建!");
        }
    }

----分界线----
public partial class App : Application
{
public App()
{
//UI线程异常
this.DispatcherUnhandledException += App_DispatcherUnhandledException;
//非UI线程异常
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

    }
    private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
    {
        //可以记录日志并转向错误bug窗口友好提示用户
        e.Handled = true;
        try
        {
            PLogs.Error(e.Exception.StackTrace.ToString(), e.Exception.Message);
           
        }
        catch (Exception)
        {


        }
    MessageBox.Show("抱歉给您带来不便!消息:" + e.Exception.Message, "系统错误",MessageBoxButton.OK,MessageBoxImage.Error);

    }
    private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
        //可以记录日志并转向错误bug窗口友好提示用户
        if (e.ExceptionObject is System.Exception)
        {

            Exception ex = (System.Exception)e.ExceptionObject;
            try
            {
                PLogs.Error(ex.StackTrace.ToString(), ex.Message);
           
            }
            catch (Exception)
            {


            }
            MessageBox.Show("抱歉给您带来不便!消息:" + ex.Message, "系统错误", MessageBoxButton.OK, MessageBoxImage.Error);
      
        }
    }
}

class PLogs
{
    /// <summary>
    /// 普通日志
    /// </summary>
    /// <param name="className">类名</param>
    /// <param name="info">日志记录</param>
    public static void Info(string className, string info)
    {
        WriteLog("INFO", className, info);
    }

    /// <summary>
    /// 警告日志
    /// </summary>
    /// <param name="className">类名</param>
    /// <param name="info">日志记录</param>
    public static void Warn(string className, string info)
    {
        WriteLog("WARN", className, info);
    }

    /// <summary>
    /// 错误日志
    /// </summary>
    /// <param name="className">类名</param>
    /// <param name="info">日志记录</param>
    public static void Error(string className, string info)
    {
        WriteLog("ERROE", className, info);
    }

    /// <summary>
    /// 写入日志
    /// </summary>
    /// <param name="className">类名</param>
    /// <param name="infoLevel">日志级别</param>
    /// <param name="info">日志记录</param>
    private static void WriteLog(string className, string infoLevel, string info)
    {
        string logFilePath = AppDomain.CurrentDomain.BaseDirectory + "/logs";
        if (!Directory.Exists(logFilePath))
        {
            Directory.CreateDirectory(logFilePath);
        }
        string logFileName = logFilePath + "/" + "log_" + DateTime.Now.ToString("yyyyMMdd") + ".log";
        if (!File.Exists(logFileName))
        {
            File.Create(logFileName).Close();
        }
        string logFormat = string.Format("[ {0} ] {1}  {2}  {3}",
                                       DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                                       className, infoLevel, info);
        StreamWriter sw = File.AppendText(logFileName);
        sw.WriteLine(logFormat);
        sw.Flush();
        sw.Close();
    }
}
posted @ 2020-11-13 14:11  BaiPiaoD  阅读(99)  评论(1编辑  收藏