1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5. using System.Diagnostics;
  6.  
  7. namespace UtilityClass
  8. {
  9. public class TraceLog : TraceListener
  10. {
  11. // 初始化时给定一个日志文件位置
  12. private string filePath;
  13.  
  14. public TraceLog()
  15. : this("")
  16. { }
  17.  
  18. public TraceLog(string filepath)
  19. {
  20. if (filepath.IndexOfAny(Path.GetInvalidFileNameChars()) > -)
  21. {
  22. ShowMsg.ShowErr(null, "指定路径无效!\r\n\r\n请重新设置日志文件路径!");
  23. Trace.Listeners.Clear();
  24. return;
  25. }
  26. filePath = ConvertX.IsNullOrEmpty(filepath) ? AppDomain.CurrentDomain.BaseDirectory + "\\Error.Log" : Path.GetFullPath(filepath);
  27. }
  28.  
  29. /// <summary>
  30. /// 保存 错误信息 到指定日志
  31. /// 此方法已重写 实际效果同 WriteLine
  32. /// </summary>
  33. public override void Write(string message)
  34. {
  35. File.AppendAllText(filePath, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + Environment.NewLine + message + Environment.NewLine);
  36. }
  37.  
  38. /// <summary>
  39. /// 保存 错误信息 到指定日志
  40. /// </summary>
  41. public override void WriteLine(string message)
  42. {
  43. File.AppendAllText(filePath, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + Environment.NewLine + message + Environment.NewLine);
  44. }
  45.  
  46. /// <summary>
  47. /// 输入一个 Exception 对象
  48. /// 它将在日志文件中保存 错误信息 和 堆栈信息
  49. /// </summary>
  50. public override void WriteLine(object o)
  51. {
  52. string msg = "";
  53. Exception ex = o as Exception;
  54. if (ex != null)
  55. {
  56. msg = ex.Message + Environment.NewLine;
  57. msg += ex.StackTrace;
  58. }
  59. else if (o != null)
  60. {
  61. msg = o.ToString();
  62. }
  63. WriteLine(msg);
  64. }
  65.  
  66. /// <summary>
  67. /// 输入一个 错误信息 和一个 分类名称
  68. /// 它将在日志文件中保存 错误信息
  69. /// </summary>
  70. public override void WriteLine(string message, string category)
  71. {
  72. string msg = "";
  73. if (!ConvertX.IsNullOrEmpty(category))
  74. {
  75. msg = category + ":";
  76. }
  77. msg += message;
  78. WriteLine(msg);
  79. }
  80.  
  81. /// <summary>
  82. /// 输入一个 Exception 对象和一个 分类名称
  83. /// 它将在日志文件中保存 错误信息 和 堆栈信息
  84. /// </summary>
  85. public override void WriteLine(object o, string category)
  86. {
  87. string msg = "";
  88. if (!ConvertX.IsNullOrEmpty(category))
  89. {
  90. msg = category + ":";
  91. }
  92. if (o is Exception)
  93. {
  94. var ex = (Exception)o;
  95. msg += ex.Message + Environment.NewLine;
  96. msg += ex.StackTrace;
  97. }
  98. else if (o != null)
  99. {
  100. msg = o.ToString();
  101. }
  102. WriteLine(msg);
  103. }
  104. }
  105. }

TraceLog.cs 累积 C#的更多相关文章

  1. Program.cs 累积_C#

    using System; using System.Diagnostics; using System.Threading; using System.Windows.Forms; using Ut ...

  2. Enterprise Library 6.0 参考源码索引

    http://www.projky.com/entlib/6.0/Diagnostics/Tracing/DiaLib.cs.htmlhttp://www.projky.com/entlib/6.0/ ...

  3. 修改AssemblyInfo.cs自动生成版本号

    一. 版本号自动生成方法 1.把 AssemblyInfo.cs文件中的[assembly:AssemblyVersion("1.0.0.0")]改成[assembly:Assem ...

  4. [C#] 剖析 AssemblyInfo.cs - 了解常用的特性 Attribute

    剖析 AssemblyInfo.cs - 了解常用的特性 Attribute [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/5944391.html 序 ...

  5. Atitit 软件架构方法的进化与演进cs bs soa roa  msa  attilax总结

    Atitit 软件架构方法的进化与演进cs bs soa roa  msa  attilax总结 1.1. 软件体系架构是沿着单机到 CS 架构,再到 BS 的三层架构甚至多层架构逐步发展过来的,关于 ...

  6. 从java文件和CS文件里查询方法使用次数工具

    前几天,领导让我找一下老系统(Java)里getRemoteUser方法都哪个文件用了,package是什么,方法被调用了多少次,当时因为着急,所以,直接人工找的,但是以后要是再出现,人工找就太讨厌了 ...

  7. 关于 WP 开发中.xaml 与.xaml.cs 的关系

    今天我们先来看一下在WP8.1开发中最长见到的几个文件之间的关系.比较论证,在看这个问题之前我们简单看看.NET平台其他两个不同的框架: Windows Forms 先看看Window Forms中的 ...

  8. .net 用户控件ascx.cs注册js脚本代码无效果

    在.net web项目中碰到一个比较奇怪的问题,网上没找到解决方案,先自己mark一下 问题描述: 添加一个用户控件ascx,在后端.cs添加js注册脚本,执行后没有弹出框 注册脚本为: this.P ...

  9. DateHelper.cs日期时间操作辅助类C#

    //==================================================================== //** Copyright © classbao.com ...

随机推荐

  1. 超级好用的C++万能头文件

    #include<bits/stdc++.h>包含了目前c++所包含的所有头文件 对比: #include <iostream> #include <cstdio> ...

  2. Windows XP系统服役13年今正式退休

    清明已过,服役13年的微软Windows XP系统也于今日正式“退休”.尽管这之后XP系统仍可以继续使用,但微软不再提供官方服务支持.对于中国数以亿计的XP用户来说,一方面是对已经使用了13年的操作系 ...

  3. JSP--TOMCAT-MYSQL web页面查询

    queryStudent.jsp代码如下 <%@ page language="java" contentType="text/html; charset=gb23 ...

  4. K - Strange Country II 暴力dfs判断有向图是否连通//lxm

    You want to visit a strange country. There are n cities in the country. Cities are numbered from 1 t ...

  5. 如何查看linux命令行操作的历史记录-linux

    前言 由于刚开始学习linux,对命令行不熟悉,可以查看使用过的命令行历史记录,熟悉命令行并熟练操作,对命令行进行深入地理解. 系统环境 OS:ubuntu16.04. 操作过程 在主文件夹目录即ho ...

  6. Finally什么时候会被执行

    PS:有return意味着程序结束,他一定会在程序结束前执行: PS:    return返回前 会把数据存储到指定的位置,基本类型是不会改变的.引用类型是会影响修改的值的

  7. 当php版本为5.6时的提示信息解决方法

    ecshop修饰符preg_replace/e不安全的几处改动 Strict standards: Only variables should be passed by reference in D: ...

  8. DOS批处理 - 函数教程

    DOS Batch - Function Tutorial What it is, why it`s important and how to write your own. Description: ...

  9. 解决 php提交表单到当前页面,刷新会重复提交 的问题

    http://blog.csdn.net/u012466451/article/details/68952280

  10. Oracle 表空间与数据文件

    -============================== --Oracle 表空间与数据文件 --============================== /* 一.概念 表空间:是一个或多 ...