错误日志记录类

项目中经常需要使用到对软件发生错误的日志记录,关于此,很多公司及组织已经做了很多工作,比如可以使用Microsoft提供的 Microsoft Enterprise Library January 2006(最新版为2007APril,但是个人现在习惯于使用2006版,版 本为2.0)中的日志记录模块或是开源项目NLOG等,这些东东一般来说功能都比较强大,但是上手可能并不是那么容易,其实一般来说我们主要需要记录的信息无非是发生错误的时间及出错原因及其他一些方便我们查找程序错误所需要的内容,我们可以让它记录到一个文本文件中,这样即时软件发布到客户手中使用时,如果在使用过程中发生什么错误,我们可以要求客户提供此错误日志供我们查错使用,下面说说我在项目中使用的错误日志记录功能的一些实现方法,
1.先定义一个记录错误日志的类,类内容如下
using  System.IO;

namespace  CSST.WMS.UI.Utility
{
    
/**//// <summary>
    
/// 文本日志记录类,成员函数均为静态函数,直接调用即可
    
/// </summary>

    public class LogMsg
    
{
        
常量定义#region 常量定义
        
/**//// <summary>
        
/// 程序目录名
        
/// (一般根据需要修改此变量值即可)
        
/// </summary>

        private const string FolderName = "EventLog";

        
/**//// <summary>
        
/// 日志文件名
        
/// (全名或后缀名,按日分类时为后缀名)
        
/// </summary>

        private const string logFileName = "Log.Log";
        
#endregion


        
构造函数#region 构造函数
        
/**//// <summary>
        
/// 私有构造函数,不允许直接实例化
        
/// </summary>

        private LogMsg()
        
{
            
//
        }

        
#endregion


        
记录错误日志到文本文件到我的文档目录#region 记录错误日志到文本文件到我的文档目录

        
/**//// <summary>
        
/// 记录错误日志到文本文件到我的文档目录
        
/// 按月分,每月产生一个日志文件
        
/// </summary>
        
/// <param name="text">日志内容</param>

        static public void WriteLogToApplicationFolderByMonth(string text)
        
{
            
//string folderPath = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\My Tests\\Log1";


            
//格式化文件夹字符串
            string folderPath = string.Format("{0}\\{1}\\Year_{2}", System.Windows.Forms.Application.StartupPath, FolderName, System.DateTime.Now.ToString("yyyy"));

            
if (!Directory.Exists(folderPath))
            
{
                Directory.CreateDirectory(folderPath);
            }


            
//格式化文件路径字符串
            string filePath = string.Format("{0}\\Month_{1}_{2}", folderPath, System.DateTime.Today.ToString("MM"), logFileName);

            LogToFile(filePath, text);
        }


        
#endregion


        
记录文本到文本文件#region 记录文本到文本文件
        
/**//// <summary>
        
/// 记录文本到文本文件(根据微软MSDN2005帮助文档System.IO.File.AppendText()提供的示例修改)
        
/// </summary>
        
/// <param name="filePath">文件路径</param>
        
/// <param name="text">记录内容</param>

        static private void LogToFile(string filePath, string text)
        
{
            
//-------------------
            StreamWriter sw = null;
            
try
            
{
                
if (!File.Exists(filePath))
                
{
                    sw 
= File.CreateText(filePath);
                }

                
else
                
{
                    sw 
= File.AppendText(filePath);
                }


                
//设置写入文件的文本
                
//string msg = string.Format("{0}---------Log Time:{1}--------{0}{2}", System.Environment.NewLine, System.DateTime.Now.ToString(), text);

                
string msg = string.Format("\r\n---------Log Time:{0}--------\r\n{1}", System.DateTime.Now.ToString(), text);

                sw.WriteLine(msg);
                
//sw.WriteLine(text);

            }

            
finally
            
{
                
if (sw != null)
                
{
                    sw.Close();
                    sw 
= null;
                }

            }

        }

        
#endregion


        
//-------------
    }

}


记录错误内容同时记录下出错时间,方便程序查错

2.在确认可能产生错误地方使用以下语捕获错误并记录到日志
             try
            
{
                dt 
= CSST.WMS.DAL.DealerDAL.SelectAll().Tables[0];
                
this.dataGridView.DataSource = dt;
            }

            
catch  (SqlException ex)
            
{
                Utility.LogMsg.WriteLogToApplicationFolderByMonth(ex.ToString());
            }

            
catch  (Exception ex)
            
{
                Utility.LogMsg.WriteLogToApplicationFolderByMonth(ex.ToString());
            }

3.一般来说并不是所有的错误我们都可以捕获得到,这种情况下,我们可以为Application的ThreadException事件编写错误日志记录功能来实记录下所有在运行时发生的错误...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值