C#异常日志
2024-08-27 14:14:38
代码比较简单,仅提供一种思路
/// <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();
}
最新文章
- 【JUC】JDK1.8源码分析之Semaphore(六)
- UART Explained(转载)
- Latex 学习
- moment.js 的简单应用
- 使用SQL字符串反转函数REVERSE巧妙实现lastindexof功能
- textarea中限制输入字符长度
- DUBBO安装配置注意事项
- RPC服务的发布订阅实现Thrift
- Immutable 详解及 React 中实践
- 作为函数的mixin
- Kafka的基本概念与安装指南(单机+集群同步)
- ";Cache-control”常见的取值private、no-cache、max-age、must-revalidate及其用意
- 关于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解决办法
- python 对 sqlite3的简单使用
- asp.net mvc5中使用Swagger 自动生成WebApi文档笔记
- 运行Python出错,提示“丢失api-ms-win-crt-runtime-l1-1-0.dll”
- Spring@Autowired注解与自动装配(转发)
- postgre 导出单表和导入
- 记一次RAID阵列的迁移经历
- 揭开Future的神秘面纱——任务取消
热门文章
- python UnicodeEncodeError: &#39;gbk&#39; codec can&#39;t encode character ...
- html5酷炫效果链接收集
- .net WebApi使用swagger 美化接口文档
- application详解
- vue项目1-pizza点餐系统9-axios实现数据存储
- Vue实现二级菜单的显示与隐藏
- UITableViewCell选中后子View背景色被Clear
- 安装sysbench,报错";Could not resolve &#39;ports.ubuntu.com&#39;";
- PAT Advanced 1046 Shortest Distance (20 分) (知识点:贪心算法)
- java File获取字节流