码迷,mamicode.com
首页 > 其他好文 > 详细

lOG

时间:2014-11-28 15:41:23      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   ar   color   os   sp   on   div   

 public class WriteLog
    {
        public void WritelogDetail(string type, string Content)
        {
            try
            {
                string directory = System.AppDomain.CurrentDomain.BaseDirectory + "LogFile\\";
                string filePath = directory + type + "_" + Guid.NewGuid().ToString() + ".txt";
                if (!Directory.Exists(directory))
                {
                    Directory.CreateDirectory(directory);
                }
                if (!File.Exists(filePath))
                {
                    FileStream fs = File.Create(filePath);
                    fs.Close();
                }               
                using (StreamWriter sw = new StreamWriter(filePath))
                {
                    sw.WriteLine("--------System Log-----------");
                    sw.WriteLine(Environment.NewLine);
                    sw.Write(Content + Environment.NewLine);
                    sw.WriteLine("-------------------");
                    sw.WriteLine(DateTime.Now);                              
                }
            }
            catch(Exception e)
            {
            }
        }
        public void WritelogDetailDal(string type, string Content)
        {
            try
            {
                string directory = System.AppDomain.CurrentDomain.BaseDirectory + "LogFile\\DalLog\\";
                string filePath = directory + type + "_" + Guid.NewGuid().ToString() + ".txt";
                if (!Directory.Exists(directory))
                {
                    Directory.CreateDirectory(directory);
                }
                if (!File.Exists(filePath))
                {
                    FileStream fs = File.Create(filePath);
                    fs.Close();
                }
                using (StreamWriter sw = new StreamWriter(filePath))
                {
                    sw.WriteLine("--------System Log-----------");
                    sw.Write(Content + Environment.NewLine);
                    sw.WriteLine("-------------------");
                    sw.WriteLine(DateTime.Now);
                }
            }
            catch (Exception e)
            {
            }
        }
        public string DateDiff(DateTime dtStart, DateTime dtEnd)
        {
            string dateDiff = null;
            try
            {
                TimeSpan ts1 = new TimeSpan(dtEnd.Ticks);
                TimeSpan ts2 = new TimeSpan(dtStart.Ticks);
                TimeSpan ts = ts1.Subtract(ts2).Duration();
                dateDiff = ts.Days.ToString() + ""
                        + ts.Hours.ToString() + "小时"
                        + ts.Minutes.ToString() + "分钟"
                        + ts.Seconds.ToString() + ""
                        + ts.Milliseconds.ToString() + "毫秒";
            }
            catch
            {

            }
            return dateDiff;
        }

    }

 

lOG

标签:style   blog   io   ar   color   os   sp   on   div   

原文地址:http://www.cnblogs.com/jianmo/p/4128360.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!