写入log事件

来源:互联网 发布:思科查看端口流量命令 编辑:程序博客网 时间:2024/06/06 10:43
  public static void Write_Log(string strLog,string type)
        {
            string sFilePath = System.AppDomain.CurrentDomain.BaseDirectory + "log";
            string sFileName = type + ".log";          
            sFileName = sFilePath + "\\" + sFileName; //文件的绝对路径
            if (!Directory.Exists(sFilePath))//验证路径是否存在
            {
                Directory.CreateDirectory(sFilePath);
                //不存在则创建
            }
            FileStream fs;
            StreamWriter sw;
            if (File.Exists(sFileName))
            //验证文件是否存在,有则追加,无则创建
            {
                fs = new FileStream(sFileName, FileMode.Append, FileAccess.Write);
            }
            else
            {
                fs = new FileStream(sFileName, FileMode.Create, FileAccess.Write);
            }
            sw = new StreamWriter(fs);
            sw.WriteLine(type + " :" + strLog + "  " + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));
            sw.Close();
            fs.Close();
        }
    }
原创粉丝点击