设置Log记录

static void Log(string str, Object o)
        {
            String message = String.Empty;
            if (o.GetType() == typeof(Exception))
            {
                message = ((Exception)o).Message;
            }
            else
            {
                message = o.ToString();
            }

            string strPath = @"C:\Mentoring";
            string fileName = "MentoringMatchLog";
            //得到物理路径中的文件
            string newPath = Path.Combine(strPath);
            if (!Directory.Exists(newPath)) //若文件夹不存在则新建文件夹   
            {
                Directory.CreateDirectory(newPath); //新建文件夹  
            }

            if (System.IO.File.Exists(strPath + "\\" + fileName + ".txt"))
            {
                using (System.IO.StreamWriter sw = new System.IO.StreamWriter(strPath + "\\" + fileName + ".txt", true))
                {
                    sw.WriteLine("DateTime:" + DateTime.Now + " ; " + str + ":" + message);
                    sw.Close();
                }
            }
            else
            {
                System.IO.File.Create(strPath + "\\" + fileName + ".txt").Close();
                using (System.IO.StreamWriter sw = new System.IO.StreamWriter(strPath + "\\" + fileName + ".txt", true))
                {
                    sw.WriteLine("DateTime:" + DateTime.Now + " ; " + str + ":" + message);
                    sw.Close();
                }
            }

        }

 

posted on 2019-04-17 13:24  赢在当下_Victor  阅读(147)  评论(0编辑  收藏  举报

导航