c#简单自定义异常处理日志辅助类

    简单写了一个错误日志记录辅助类,记录在此。

    Loghelper类

   

 1 using System;
 2 using System.Collections.Generic;
 3 using System.IO;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 
 8 namespace LogHelper
 9 {
10     public static class LogHelper
11     {
12         //拼接日志目录
13         static string appLogPath = AppDomain.CurrentDomain.BaseDirectory + "log/";
14         /// <summary>
15         /// 写入日志
16         /// </summary>
17         /// <param name="ex">异常对象</param>
18         public static void WriteLog(Exception ex)
19         {
20             //日志目录是否存在 不存在创建
21             if (!Directory.Exists(appLogPath))
22             {
23                 Directory.CreateDirectory(appLogPath);
24             }
25             StringBuilder logInfo = new StringBuilder("");
26             string currentTime = System.DateTime.Now.ToString("[yyyy-MM-dd HH:mm:ss]");
27             if (ex != null)
28             {
29                 logInfo.Append("
");
30                 logInfo.Append(currentTime + "
");
31                 //获取描述当前的异常的信息
32                 logInfo.Append(ex.Message + "
");
33                 //获取当前实例的运行时类型
34                 logInfo.Append(ex.GetType() + "
");
35                 //获取或设置导致错误的应用程序或对象的名称
36                 logInfo.Append(ex.Source + "
");
37                 //获取引发当前异常的方法
38                 logInfo.Append(ex.TargetSite + "
");
39                 //获取调用堆栈上直接桢的字符串表示形式
40                 logInfo.Append( ex.StackTrace + "
");
41             }
42             System.IO.File.AppendAllText(appLogPath + DateTime.Now.ToString("yyyy-MM-dd") + ".log", logInfo.ToString());
43         }
44 
45     }
46 }

测试代码:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace LogHelper
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             try
14             {
15                 while (true)
16                 {
17                     int a = Convert.ToInt32(Console.ReadLine());
18                     Console.WriteLine("您输入的是:" + a);
19                 }
20 
21             }
22             catch (Exception ex)
23             {
24 
25                 LogHelper.WriteLog(ex);
26             }
27             Console.Write("OVER");
28             Console.Read();
29         }
30     }
31 }

日志文件结果:

1 [2013-09-24 11:15:45]
2 输入字符串的格式不正确。
3 System.FormatException
4 mscorlib
5 Void StringToNumber(System.String, System.Globalization.NumberStyles, NumberBuffer ByRef, System.Globalization.NumberFormatInfo, Boolean)
6    在 System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
7    在 System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
8    在 System.Convert.ToInt32(String value)
9    在 LogHelper.Program.Main(String[] args) 位置 f:SUN.TESTLogHelperLogHelperProgram.cs:行号 17

      简单,可以满足日常需要。有一点就是日志文件按照日期命名,会不会随着时间越来越多。需再考虑......

原文地址:https://www.cnblogs.com/wolf-sun/p/3336533.html