代码比较简单,仅提供一种思路

/// <summary>
/// 将异常打印到LOG文件
/// </summary>
/// <param name="ex">异常</param>
/// <param name="LogAddress">日志文件地址</param>
public static void WriteLog(Exception ex, string LogAddress = "")
{
//如果日志文件为空,则默认在Debug目录下新建 YYYY-mm-dd_Log.log文件
if (LogAddress == "")
{
LogAddress = Environment.CurrentDirectory + '\\' +
DateTime.Now.Year + '-' +
DateTime.Now.Month + '-' +
DateTime.Now.Day + "_Log.log";
} //把异常信息输出到文件
StreamWriter sw = new StreamWriter(LogAddress, true);
sw.WriteLine("当前时间:" + DateTime.Now.ToString());
sw.WriteLine("异常信息:" + ex.Message);
sw.WriteLine("异常对象:" + ex.Source);
sw.WriteLine("调用堆栈:\n" + ex.StackTrace.Trim());
sw.WriteLine("触发方法:" + ex.TargetSite);
sw.WriteLine();
sw.Close();
}

最新文章

  1. 【JUC】JDK1.8源码分析之Semaphore(六)
  2. UART Explained(转载)
  3. Latex 学习
  4. moment.js 的简单应用
  5. 使用SQL字符串反转函数REVERSE巧妙实现lastindexof功能
  6. textarea中限制输入字符长度
  7. DUBBO安装配置注意事项
  8. RPC服务的发布订阅实现Thrift
  9. Immutable 详解及 React 中实践
  10. 作为函数的mixin
  11. Kafka的基本概念与安装指南(单机+集群同步)
  12. &quot;Cache-control”常见的取值private、no-cache、max-age、must-revalidate及其用意
  13. 关于python3.6上传文件时报错:HTTPSConnectionPool(host=&#39;***.org&#39;, port=443): Max retries exceeded with url: /post (Caused by SSLError(SSLError(1, &#39;[SSL: CERTIFICATE_VERIFY_FAIL解决办法
  14. python 对 sqlite3的简单使用
  15. asp.net mvc5中使用Swagger 自动生成WebApi文档笔记
  16. 运行Python出错,提示“丢失api-ms-win-crt-runtime-l1-1-0.dll”
  17. Spring@Autowired注解与自动装配(转发)
  18. postgre 导出单表和导入
  19. 记一次RAID阵列的迁移经历
  20. 揭开Future的神秘面纱——任务取消

热门文章

  1. python UnicodeEncodeError: &#39;gbk&#39; codec can&#39;t encode character ...
  2. html5酷炫效果链接收集
  3. .net WebApi使用swagger 美化接口文档
  4. application详解
  5. vue项目1-pizza点餐系统9-axios实现数据存储
  6. Vue实现二级菜单的显示与隐藏
  7. UITableViewCell选中后子View背景色被Clear
  8. 安装sysbench,报错&quot;Could not resolve &#39;ports.ubuntu.com&#39;&quot;
  9. PAT Advanced 1046 Shortest Distance (20 分) (知识点:贪心算法)
  10. java File获取字节流