C# 深入学习 之 Winform记录日志
2010-03-21 00:06
295 查看
using System; using System.Windows.Forms; using ST = System.Threading; using SWF = System.Windows.Forms; using T = System.Diagnostics.Trace; namespace WindowsFormsApplication1 { static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(OnAppException); Application.ThreadExit += new EventHandler(OnAppExit); string logFile = string.Format("{0}{1}", AppDomain.CurrentDomain.BaseDirectory, "Applog.txt"); System.IO.TextWriter log = new System.IO.StreamWriter(logFile); if (true) { System.Diagnostics.TextWriterTraceListener logger; logger = new System.Diagnostics.TextWriterTraceListener(log); System.Diagnostics.Trace.Listeners.Add(logger); System.Diagnostics.Trace.WriteLine("App starting:" + DateTime.Now); } Application.Run(new Form1()); } /// <summary> /// Generic exception handler for application. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private static void OnAppException(object sender, ST.ThreadExceptionEventArgs e) { Exception ex; ex = e.Exception; // inform user... SWF.MessageBox.Show("Halting due to error: " + ex.Message, "Bank Customer App", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error); // log it (the entire exception chain)... T.WriteLine("Generic Application Exception Handler:"); while (ex != null) { T.WriteLine(ex.GetType().FullName); T.Indent(); T.WriteLine("Msg: " + ex.Message); T.WriteLine("Trace: " + ex.StackTrace); T.Unindent(); T.Flush(); ex = ex.InnerException; }//while // halt app T.Close(); SWF.Application.Exit(); } /// <summary> /// Called during normal application termination. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private static void OnAppExit(object sender, System.EventArgs e) { T.Close(); // close trace file } } }
相关文章推荐
- C#使用Log4Net记录日志(想直接转载的,但是不支持转到这上面来,复制来的,作者勿怪,学习用)
- C# Winform 运用Log4Net记录日志
- C# 自动查找文件内容(正则使用、获取目录下所有文件、多线程、日志记录,文件操作)
- Java系列:《Java核心技术 卷一》学习笔记,chapter11 记录日志
- C#创建创建文本文件写入读取,可以用来做系统日志或程序操作日志或者错误记录
- C# WinForm TextBox猜想输入和历史记录输入(源码)
- C# 使用Log4Net记录日志(基础篇)
- C#深入学习:泛型修饰符in,out、逆变委托类型和协变委托类型
- 实现log4.NET记录日志功能【WinForm】
- C# 简易日志记录类
- unity之C#学习记录
- Java与C#的比较学习记录(1)
- openerp学习笔记 跟踪状态,记录日志,发送消息
- c# winForm TableLayoutPanel学习 合并行的布局及动态增加删除内部控件
- C#原生邮件发送+发送日志记录
- 深入C#学习:序列化(Serialize)、…
- C#使用Log4Net记录日志
- Python模块学习 ---- logging 日志记录(一)
- C#使用Log4Net记录日志
- 自我学习log4net日志记录