生如夏花

这是一个多美丽又遗憾的世界,我们就这样抱着笑着还流着泪 我从远方赶来赴你一面之约,痴迷流连人间我为她而狂野
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

关于记录错误信息到文本文件的几点问题讨论

Posted on 2007-08-17 22:16  陈欠扁  阅读(244)  评论(0)    收藏  举报

最近在项目中因为各种需要必须把出错信息及相关数据记录起来,由于考虑到记录到系统日志,不便于收集,所以综合起来,还是决定用文件记录出错信息,这样,如果客户使用程序过程中出现错误,可以直接将日志文件发送过来以便我们寻找问题的根源

1. 先写个日志记录类,主要代码如下
using System;
using System.Text;
using System.IO;

        /// <summary>
        /// 记录错误日志到文本文件
        /// </summary>
        /// <param name="text">日志内容</param>
        static public void WriteLog(string text)
        {
            string folderPath = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\My Tests\\Log1";//记录到当前登陆用户文件夹下,保证了用户的权限问题

            if (!Directory.Exists(folderPath))
            {
                Directory.CreateDirectory(folderPath);
            }
            string filePath = folderPath + "\\Log.txt";

            LogToFile(text, filePath);
        }

        #region 记录文本到文本文件
        /// <summary>
        /// 记录文本到文本文件
        /// </summary>
        /// <param name="text">记录内容</param>
        /// <param name="filePath">文件路径</param>
        static private void LogToFile(string text, string filePath)
        {
            //-------------------
            StreamWriter sw = null;
            try
            {
                if (!File.Exists(filePath))
                {
                    sw = File.CreateText(filePath);
                }
                else
                {
                    sw = File.AppendText(filePath);
                }
                string msg = string.Format("---------Log Time:{0}--------", System.DateTime.Now.ToString());
                sw.WriteLine(msg);
                sw.WriteLine(text);
            }
            finally
            {
                if (sw != null)
                {
                    sw.Close();
                    sw = null;
                }
            }
        }
        #endregion

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

/****************
 * 原本MSDN2005代码
// This text is added only once to the file.
// Create a file to write to.
using (StreamWriter sw = File.CreateText(filePath))
{
    string msg = string.Format("---------Log Time:{0}--------", System.DateTime.Now.ToString());
    sw.WriteLine(msg);
    sw.WriteLine(text);
}
****************/


2. 调用代码如下
string[] sList ={
"11111111111",
"22222222222",
"33333333333",
"44444444444",
"55555555555",
"66666666666",
"77777777777",
"88888888888",
"99999999999",
"AAAAAAAAAAA",
"BBBBBBBBBBB",
"CCCCCCCCCCC",
"DDDDDDDDDDD",
"EEEEEEEEEEE"
};
foreach (string s in sList)
{
LogMsg.WriteLog(s);
}
3. 测试,能正确记录内容
现在主要有以下几个疑问,希望大家能一起讨论
1. 假如在设置保存文件的路径时设置如下
            string filePath = System.Windows.Forms.Application.ExecutablePath + ".log";
在本机上使用系统帐户登陆,能正常记录日志内容

如果使用Guest组用户(用户无在此目录修改文件的权限),这时无法记录日志内容

2. 系统使用UTF8(File.CreateText(filePath))编码保存文本内容,不知在其他操作系统下是否会有乱码或是编码不兼容问题

3.
string msg = string.Format("---------Log Time:{0}--------", System.DateTime.Now.ToString());
sw.WriteLine(msg);
sw.WriteLine(text);//执行了两次

string msg = string.Format("---------Log Time:{0}--------", System.DateTime.Now.ToString());
string msgtext=msg+ System.Environment.NewLine+text;
sw.WriteLine(msgtext);//仅执行一次

哪种好点,哪种效率高

4.
//方式1
// 使用using方式,参考微软MSDN2005帮助文档System.IO.File.AppendText()提供的示例
using (StreamWriter sw = File.CreateText(filePath))
{
    string msg = string.Format("---------Log Time:{0}--------", System.DateTime.Now.ToString());
    sw.WriteLine(msg);
    sw.WriteLine(text);
}
//?这里不知是否会关闭sw对象

//方式2
//使用try catch方式
StreamWriter sw = null;
try
{
if (!File.Exists(filePath))
{
    sw = File.CreateText(filePath);
}
else
{
    sw = File.AppendText(filePath);
}
string msg = string.Format("---------Log Time:{0}--------", System.DateTime.Now.ToString());
sw.WriteLine(msg);
sw.WriteLine(text);
}
finally
{
if (sw != null)
{
    sw.Close();
    sw = null;
}
}

哪种好点,哪种效率高,不知有什么不同
项目文件如下

/Files/chenaspx/FileLogDemo.rar