wpf 全局异常捕获处理
2021-01-03 18:28
标签:ide 捕获异常 pre linq bsp uri str compile generate wpf 全局异常捕获处理 标签:ide 捕获异常 pre linq bsp uri str compile generate 原文地址:https://www.cnblogs.com/Fooo/p/13201072.htmlnamespace WpfGridChange
{
using System;
using System.CodeDom.Compiler;
using System.Diagnostics;
using System.Windows;
using System.Threading.Tasks;
public class App : Application
{
[DebuggerNonUserCode, GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
public void InitializeComponent()
{
base.StartupUri = new Uri("../Resources/xaml/WinWellcome.xaml", UriKind.Relative);
}
protected override void OnStartup(StartupEventArgs e)
{
RegisterEvents();
base.OnStartup(e);
}
///
{
App app = new App();
app.InitializeComponent();
app.Run();
}
}
}using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net;
using System.Threading;
namespace WpfGridChange
{
public class Utils
{
//读写锁,当资源处于写入模式时,其他线程写入需要等待本次写入结束之后才能继续写入
private static readonly ReaderWriterLockSlim LogWriteLock = new ReaderWriterLockSlim();
public static void LogWrite(Exception ex)
{
if (!Directory.Exists("Log"))
{
Directory.CreateDirectory("Log");
}
var now = DateTime.Now;
var logpath = @"Log\" + now.Year + "" + now.Month + "" + now.Day + ".log";
var log = "\r\n----------------------" + DateTime.Now + " --------------------------\r\n"
+ ex.Message
+ "\r\n"
+ ex.InnerException
+ "\r\n"
+ ex.StackTrace
+ "\r\n----------------------footer--------------------------\r\n";
try
{
//设置读写锁为写入模式独占资源,其他写入请求需要等待本次写入结束之后才能继续写入
LogWriteLock.EnterWriteLock();
File.AppendAllText(logpath, log);
}
finally
{
//退出写入模式,释放资源占用
LogWriteLock.ExitWriteLock();
}
}
}
}
上一篇:简单聊下c#异常处理
下一篇:盗版windows洗白之路