您的位置:首页 > 编程语言 > C#

C#实现的简单实用日志

2009-07-26 12:19 603 查看
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace LogServcie
{
    public static  class Log
    {
        public static string LOGMSGPATH ="LogMsg";
        public static string ERRMSGPATH = "ErrMsg";
      
        /// <summary>
        /// 用于记录用户的操作流程并记录下操作时间
        /// </summary>
        /// <param name="Logmsg"></param>
        public static void writeLog(string Logmsg)
        {
            string path = System.Environment.CurrentDirectory + "//" + LOGMSGPATH;
            CreaterPath(path);

            using (StreamWriter sw = new StreamWriter(path + "//log" + DateTime.Now.ToShortDateString() + ".txt", true, Encoding.Default))
            {
               // Add some text to the logfile.
                sw.WriteLine(Logmsg+"   "+DateTime.Now);
            }
        }

        /// <summary>
        /// 记录系统运行过程中出现的错误
        /// </summary>
        /// <param name="Errmsg"></param>
        /// <param name="e"></param>
        public static void WirteErr(string Errmsg,Exception e)
        {

            string path = System.Environment.CurrentDirectory + "//" + ERRMSGPATH;

            CreaterPath(path);

            using (StreamWriter sw = new StreamWriter(path + "//err"+ DateTime.Now.ToShortDateString() + ".txt", true, Encoding.Default))
            {
                // Add some text to the errfile.
                sw.WriteLine(Errmsg);
                sw.WriteLine("出错原因: "+e.Message);
                sw.Close();
            }
        }

        /// <summary>
        /// 创建制定的文档路径
        /// </summary>
        /// <param name="path"></param>
        private static void CreaterPath(string path)
        {
            DirectoryInfo dirInfo = new DirectoryInfo(path);

            if (!dirInfo.Exists)
            {
                dirInfo.Create();
            }
        }

   
    }
}

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐