zoukankan      html  css  js  c++  java
  • 将错误日志记录在txt文本里

    引言

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

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

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

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

      效果展示:

        

     类代码:

    [csharp] view plain copy
     
    print?在CODE上查看代码片派生到我的代码片
    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.   
    8. namespace GetLog  
    9. {  
    10.     public class WriteLog  
    11.     {  
    12.         private static StreamWriter streamWriter; //写文件    
    13.   
    14.         public static void WriteError(string message)  
    15.         {  
    16.             try  
    17.             {  
    18.                 //DateTime dt = new DateTime();  
    19.                 string directPath = ConfigurationManager.AppSettings["LogFilePath"].ToString().Trim();    //在获得文件夹路径  
    20.                 if (!Directory.Exists(directPath))   //判断文件夹是否存在,如果不存在则创建  
    21.                 {  
    22.                     Directory.CreateDirectory(directPath);  
    23.                 }  
    24.                 directPath += string.Format(@"{0}.log", DateTime.Now.ToString("yyyy-MM-dd"));  
    25.                 if (streamWriter == null)  
    26.                 {  
    27.                     streamWriter = !File.Exists(directPath) ? File.CreateText(directPath) : File.AppendText(directPath);    //判断文件是否存在如果不存在则创建,如果存在则添加。  
    28.                 }  
    29.                 streamWriter.WriteLine("***********************************************************************");  
    30.                 streamWriter.WriteLine(DateTime.Now.ToString("HH:mm:ss"));  
    31.                 streamWriter.WriteLine("输出信息:错误信息");  
    32.                 if (message != null)  
    33.                 {  
    34.                     streamWriter.WriteLine("异常信息: " + message);  
    35.                 }  
    36.             }  
    37.             finally  
    38.             {  
    39.                 if (streamWriter != null)  
    40.                 {  
    41.                     streamWriter.Flush();  
    42.                     streamWriter.Dispose();  
    43.                     streamWriter = null;  
    44.                 }  
    45.             }  
    46.         }  
    47.     }  
    48. }  
    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("异常信息:
    " + 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.   
    10.   </appSettings>  
    11. </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;
                }
            }

       小结

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

  • 相关阅读:
    2072=删数问题
    2872=M--二分查找
    4165=全排列问题
    2805=大家快来A水题
    4148=1.1联结词真值运算
    2748=第X大的数
    3479=青蛙过河
    1200=汉诺塔
    Leetcode92_反转链表II
    Leetcode206_反转链表
  • 原文地址:https://www.cnblogs.com/zhangxiaolei521/p/5803781.html
Copyright © 2011-2022 走看看