将错误日志记录在txt文本里
引言
对于已经部署的系统一旦出错对于我们开发人员来说是比较痛苦的事情,因为我们不能跟踪到错误信息,不能
很快的定位到我们的错误位置在哪,这时候如果能像开发环境一样记录一些堆栈信息就可以了,这时候我们就需要将
错误信息捕捉到然后输出到一个我们可以看到的地方就可以了,这时候我们比较简单的做法就是将一些错误信息输出
到txt文本中。下面就和大家分享一个记录日志的工具类。
效果展示:
类代码:
- 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;
- }
- }
- }
- }
- }

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;
}
}
}
}
}
配置文件:
- <?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>

<?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>
调用代码:
- static void Main(string[] args)
- {
- try
- {
- var i = 0;
- var j = 1 / i;
- }
- catch (Exception ex)
- {
- WriteLog.WriteError(ex.ToString());
- throw;
- }
- }

static void Main(string[] args)
{
try
{
var i = 0;
var j = 1 / i;
}
catch (Exception ex)
{
WriteLog.WriteError(ex.ToString());
throw;
}
}
小结
上面就是我们一个简单实用的错误日志记录类,在此分享给大家希望能给各位提供帮助!
将错误日志记录在txt文本里的更多相关文章
- 点滴积累【C#】---错误日志记录到txt文本里。
效果: 描述:将系统中的错误信息,try catch到日志里面. 代码: [后端代码] using System; using System.Collections.Generic; using Sy ...
- PHP错误日志记录:display_errors与log_errors的区别
我们所做的东西,无论在开发环境还是在生产环境都可能会出现一些问题. 开发环境下,我们会要求错误尽可能详细的呈现出来,错误提示信息越详细越好,越详细越能帮助开发人员确定问题所在并从根本上解决他们. 生产 ...
- .Net Core中间件和过滤器实现错误日志记录
1.中间件的概念 ASP.NET Core的处理流程是一个管道,中间件是组装到应用程序管道中用来处理请求和响应的组件. 每个中间件可以: 选择是否将请求传递给管道中的下一个组件. 可以在调用管道中的下 ...
- .net错误日志记录(log4)
Log4 web.config <!--这段放前面--> <configSections> <section name="log4net" type= ...
- 利用Image对象,建立Javascript前台错误日志记录
手记:摘自Javascript高级程序设计(第三版),利用Image对象发送请求,确实有很多优点,有时候这也许就是一个创意点,再次做个笔记供自己和大家参考. 原文: 开发 Web 应用程序过程中的一种 ...
- wpf 全局异常捕捉+错误日志记录+自动创建桌面图标
/// /// 创建桌面图标 /// public static void CreateShortcutOnDesktop(string LnkName) { String shortcutPath ...
- asp.net Web项目中使用Log4Net进行错误日志记录
使用log4net可以很方便地为应用添加日志功能.应用Log4net,开发者可以很精确地控制日志信息的输出,减少了多余信息,提高了日志记录性能.同时,通过外部配置文件,用户可以不用重新编译程序就能 ...
- PHP错误日志记录文件位置确定
1.确定web服务器 ( IIS, APACHE, NGINX 等) 以哪一种方式支持PHP,通常是有下面2种方式 通过模块加载的方式, 适用于apache 通过 CGI/fastCGI 模式, 该模 ...
- python错误日志记录工具,解决项目排错问题
我们写项目的时候难免会遇到代码报错的问题,遇到这样的问题了如何快速的定位问题并解决问题呢? 我今天来整理了利用python只带的工具来解决这个问题,我能需要使用的库有: logging os 这些都是 ...
随机推荐
- 545E. Paths and Trees
题目链接 题意:给定一个无向图和一个点u,找出若干条边组成一个子图,要求这个子图中u到其他个点的最短距离与在原图中的相等,并且要求子图所有边的权重之和最小,求出最小值并输出子图的边号. 思路:先求一遍 ...
- iOS 开发--添加工程
文/Bison(简书作者)原文链接:http://www.jianshu.com/p/dd71e15df5d0著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 第一部分,配置项目 在此只 ...
- JavaWeb项目开发案例精粹-第6章报价管理系统-05Action层
0. <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC &quo ...
- Geoprocessor 使用
在AO中使用Geoprocessor(ESRI.ArcGIS.Geoprocessor) 1.观察arcmap中的使用方法,明确各参数意义. 2.arctoolbox中参数对应为features/fe ...
- 怎样做出优秀的扁平化设计风格 PPT 或 Keynote 幻灯片演示文稿?(装)
不知道你有没有想过,为什么很人多的扁平化 PPT 是这个样子: 或者是这样: 然而,还有一小撮人的扁平化 PPT 却拥有那么高颜值: 为什么会产生这么大的差距呢?丑逼 PPT 应该如何逆袭成为帅逼呢? ...
- (八)ASP.NET自定义用户控件(1)
http://blog.csdn.net/laodao1/article/details/5897366 ASP.NET自定义控件组件开发 第一章:从一个简单的控件谈起 起始开发ASP.NET自定义控 ...
- B/S 和 C/S
B/S最大优势为客户端免维护,适用于用户群庞大,或客户需求经长发生变化的情况. C/S功能强大,可以减轻服务器端压力,如果用户的需求特别复杂,用C/S. 全面: Client/Server是建立在局域 ...
- github创建repo,本地导入git项目到github
一般地,在注册好github账号之后,你需要做的事情就是在github上创建一个repo,该repo将成为你的origin(central)repo,随后你就可以将本地的项目git repo导入到这个 ...
- 如何添加真机调试的iOS设备
注意点: 有时需要同意协议什么的,很多时候刷新出来都是白屏,解决办法: 对于不能确认新协议的问题,我发现了一个解决方法:登陆后,直接在浏览器的地址框访问:https://developer.apple ...
- Linux C double linked for any data type
/************************************************************************** * Linux C double linked ...