通过本文主要向大家介绍了c#示例,c#代码示例,c#程序示例,c#日志类,c#写日志等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
将异常写到日志文件中,可以在调试程序的时候知道程序发生过哪些异常,并且可以知道异常发生的位置。这点对需要进行长时间运行并调试的程序尤为有效。
//把异常信息输出到文件
StreamWriter fs = new StreamWriter(LogAddress, true);
fs.WriteLine("当前时间:" + DateTime.Now.ToString());
fs.WriteLine("异常信息:" + ex.Message);
fs.WriteLine("异常对象:" + ex.Source);
fs.WriteLine("调用堆栈:\n" + ex.StackTrace.Trim());
fs.WriteLine("触发方法:" + ex.TargetSite);
fs.WriteLine();
fs.Close();
}
</div>