C#实现的简单实用日志

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();
            }
        }

   
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值