引言

对于已经部署的系统一旦出错对于我们开发人员来说是比较痛苦的事情,因为我们不能跟踪到错误信息,不能

很快的定位到我们的错误位置在哪,这时候如果能像开发环境一样记录一些堆栈信息就可以了,这时候我们就需要将

错误信息捕捉到然后输出到一个我们可以看到的地方就可以了,这时候我们比较简单的做法就是将一些错误信息输出

到txt文本中。下面就和大家分享一个记录日志的工具类。

效果展示:

类代码:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Web;
  7. namespace GetLog
  8. {
  9. public class WriteLog
  10. {
  11. private static StreamWriter streamWriter; //写文件
  12. public static void WriteError(string message)
  13. {
  14. try
  15. {
  16. //DateTime dt = new DateTime();
  17. string directPath = ConfigurationManager.AppSettings["LogFilePath"].ToString().Trim();    //在获得文件夹路径
  18. if (!Directory.Exists(directPath))   //判断文件夹是否存在,如果不存在则创建
  19. {
  20. Directory.CreateDirectory(directPath);
  21. }
  22. directPath += string.Format(@"\{0}.log", DateTime.Now.ToString("yyyy-MM-dd"));
  23. if (streamWriter == null)
  24. {
  25. streamWriter = !File.Exists(directPath) ? File.CreateText(directPath) : File.AppendText(directPath);    //判断文件是否存在如果不存在则创建,如果存在则添加。
  26. }
  27. streamWriter.WriteLine("***********************************************************************");
  28. streamWriter.WriteLine(DateTime.Now.ToString("HH:mm:ss"));
  29. streamWriter.WriteLine("输出信息:错误信息");
  30. if (message != null)
  31. {
  32. streamWriter.WriteLine("异常信息:\r\n" + message);
  33. }
  34. }
  35. finally
  36. {
  37. if (streamWriter != null)
  38. {
  39. streamWriter.Flush();
  40. streamWriter.Dispose();
  41. streamWriter = null;
  42. }
  43. }
  44. }
  45. }
  46. }
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Web; namespace GetLog
{
public class WriteLog
{
private static StreamWriter streamWriter; //写文件 public static void WriteError(string message)
{
try
{
//DateTime dt = new DateTime();
string directPath = ConfigurationManager.AppSettings["LogFilePath"].ToString().Trim(); //在获得文件夹路径
if (!Directory.Exists(directPath)) //判断文件夹是否存在,如果不存在则创建
{
Directory.CreateDirectory(directPath);
}
directPath += string.Format(@"\{0}.log", DateTime.Now.ToString("yyyy-MM-dd"));
if (streamWriter == null)
{
streamWriter = !File.Exists(directPath) ? File.CreateText(directPath) : File.AppendText(directPath); //判断文件是否存在如果不存在则创建,如果存在则添加。
}
streamWriter.WriteLine("***********************************************************************");
streamWriter.WriteLine(DateTime.Now.ToString("HH:mm:ss"));
streamWriter.WriteLine("输出信息:错误信息");
if (message != null)
{
streamWriter.WriteLine("异常信息:\r\n" + message);
}
}
finally
{
if (streamWriter != null)
{
streamWriter.Flush();
streamWriter.Dispose();
streamWriter = null;
}
}
}
}
}

配置文件:

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <configuration>
  3. <startup>
  4. <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  5. </startup>
  6. <appSettings>
  7. <!-- 系统日志保存路径-->
  8. <add key="LogFilePath" value="D://ErrorLog" />
  9. </appSettings>
  10. </configuration>
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<appSettings>
<!-- 系统日志保存路径-->
<add key="LogFilePath" value="D://ErrorLog" /> </appSettings>
</configuration>

调用代码:

  1. static void Main(string[] args)
  2. {
  3. try
  4. {
  5. var i = 0;
  6. var j = 1 / i;
  7. }
  8. catch (Exception ex)
  9. {
  10. WriteLog.WriteError(ex.ToString());
  11. throw;
  12. }
  13. }
static void Main(string[] args)
{
try
{
var i = 0;
var j = 1 / i;
}
catch (Exception ex)
{
WriteLog.WriteError(ex.ToString());
throw;
}
}

小结

上面就是我们一个简单实用的错误日志记录类,在此分享给大家希望能给各位提供帮助!

将错误日志记录在txt文本里的更多相关文章

  1. 点滴积累【C#】---错误日志记录到txt文本里。

    效果: 描述:将系统中的错误信息,try catch到日志里面. 代码: [后端代码] using System; using System.Collections.Generic; using Sy ...

  2. PHP错误日志记录:display_errors与log_errors的区别

    我们所做的东西,无论在开发环境还是在生产环境都可能会出现一些问题. 开发环境下,我们会要求错误尽可能详细的呈现出来,错误提示信息越详细越好,越详细越能帮助开发人员确定问题所在并从根本上解决他们. 生产 ...

  3. .Net Core中间件和过滤器实现错误日志记录

    1.中间件的概念 ASP.NET Core的处理流程是一个管道,中间件是组装到应用程序管道中用来处理请求和响应的组件. 每个中间件可以: 选择是否将请求传递给管道中的下一个组件. 可以在调用管道中的下 ...

  4. .net错误日志记录(log4)

    Log4 web.config <!--这段放前面--> <configSections> <section name="log4net" type= ...

  5. 利用Image对象,建立Javascript前台错误日志记录

    手记:摘自Javascript高级程序设计(第三版),利用Image对象发送请求,确实有很多优点,有时候这也许就是一个创意点,再次做个笔记供自己和大家参考. 原文: 开发 Web 应用程序过程中的一种 ...

  6. wpf 全局异常捕捉+错误日志记录+自动创建桌面图标

    /// /// 创建桌面图标 /// public static void CreateShortcutOnDesktop(string LnkName) { String shortcutPath ...

  7. asp.net Web项目中使用Log4Net进行错误日志记录

      使用log4net可以很方便地为应用添加日志功能.应用Log4net,开发者可以很精确地控制日志信息的输出,减少了多余信息,提高了日志记录性能.同时,通过外部配置文件,用户可以不用重新编译程序就能 ...

  8. PHP错误日志记录文件位置确定

    1.确定web服务器 ( IIS, APACHE, NGINX 等) 以哪一种方式支持PHP,通常是有下面2种方式 通过模块加载的方式, 适用于apache 通过 CGI/fastCGI 模式, 该模 ...

  9. python错误日志记录工具,解决项目排错问题

    我们写项目的时候难免会遇到代码报错的问题,遇到这样的问题了如何快速的定位问题并解决问题呢? 我今天来整理了利用python只带的工具来解决这个问题,我能需要使用的库有: logging os 这些都是 ...

随机推荐

  1. Outlook与Hotmail的设置

    最近研究邮件备份,首先要使用客户端下载邮件,碰到不少问题:1. HOTMAIL GMAIL SINA的POP/IMAP默认居然都是关闭的,必须改成开放才行. GMAIL改成开放以后还是没有成功,好像还 ...

  2. chrome开发配置(二)获取源代码

    1.下载源代码,这里使用最简单的下载,chrome打包好的共享在百度云盘,点击下载 2.建议下载7z压缩工具解压压缩包,大概1个多小时. 3.至于以后怎么更新到最新版不能,等编译生成成功了,我们再具体 ...

  3. C#AutoResetEvent和ManualResetEvent的区别

    一:终止状态和非终止状态 首先说说线程的终止状态和非终止状态.AutoResetEvent和ManualResetEvent的构造函数中,都有bool变量来指明线程的终止状态和非终止状态.true表示 ...

  4. Fedora 15 KDE中如何打开software management及如何应用

    Fedora 15 KDE中如何打开software management级如何应用 software management中有转载和卸载软件(Get and remove software)的功能 ...

  5. dojo 十 ajax dojo/_base/xhr

    官方教程:Ajax with DojoAjax功能:1.从服务器加载静态数据2.从web服务中访问xml或json类型的数据3.将表单(form)数据发送到服务器4.刷新页面内容....Ajax在RI ...

  6. openwrt(路由器)的源码地址

    https://dev.openwrt.org/wiki/GetSource 路由器的源码地址

  7. [原]POJ-1631-Bridging signals-( 水LIS-O(nlogn) -DP)

    题目大意:求最长上升子序列(LIS)长度,序列最大数不超过40000.因为只有上升排列的接口才不相交. 思路:普通的 O(n^2)的做法肯定会超时:因此,dp[ ] 记录长度为 i+1 的子序列中最末 ...

  8. H5移动前端完美布局之padding

    序上次的提到了H5移动前端完美布局之-margin百分比的使用margin-top(left,right,bottom)的百分比在移动页面布局中对上下左右距离的处理,攻下城外再攘城内,今天看看padd ...

  9. MTK6515 android打版软件配置(DrvGen.exe 使用)

    1 一.配置GPIO 2 二.配置emmc 3 三.配置LCM 3.1 1.增加LCM驱动文件 3.2 2.配置驱动文件 3.3 3.配置背光 4 四.配置touch panel 4.1 1.通过dc ...

  10. HDU 4747 Mex(线段树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4747 题意:给出一个数列A.计算所有的mex(i,j)之和.1<=i<=j<=n. ...