/// <summary> /// 往服务日志中写入服务运行状态 /// </summary> /// <param name="PHMevent">要记录的服务事件</param> /// <param name="newlogpath">PHM执行失败的日志地址,只有服务事件为“failed”的时候才有</param> public static void WriteLog(string PHMevent, string newlogpath) { //输入的字符串startorStop只能为"start"或"stop" //Append the text to the sample file. string logPath = CallUserExe.getFullFilePath("phm_home", @"\Csharp代码\ServicePHM.log"); StreamWriter writer = File.AppendText(logPath); switch (PHMevent) { case "start": start = DateTime.Now; writer.WriteLine("ServicePHM Start!\t" + DateTime.Now.ToString()); writer.WriteLine("----------------------------------------------------------"); break; case "stop": TimeSpan runtime = DateTime.Now.Subtract(start); writer.WriteLine(""); writer.WriteLine("<此次服务共运行{0}天{1}小时{2}分钟{3}秒,累计诊断{4}次,诊断失败{5}次;累计预警{6}次,预警失败{7}次>", runtime.Days, runtime.Hours, runtime.Minutes, runtime.Seconds, ServicePHM.n_diag, ServicePHM.n_failed_diag, ServicePHM.n_predict, ServicePHM.n_failed_predict); writer.WriteLine("----------------------------------------------------------"); writer.WriteLine("ServicePHM Stop!\t" + DateTime.Now.ToString()); writer.WriteLine(""); writer.WriteLine(""); break; case "diag": writer.WriteLine("启动PHM系统...进行一次故障诊断\t\t" + DateTime.Now.ToString()); break; case "predict": writer.WriteLine("启动一次故障预警...\t\t\t" + DateTime.Now.ToString()); break; case "success": string ev = ServicePHM.hasPredicted ? "预警" : "诊断"; writer.WriteLine("执行" + ev + "成功!\t\t\t\t" + DateTime.Now.ToString()); break; case "alarm": writer.WriteLine("故障警报已发送!\t\t\t" + DateTime.Now.ToString()); writer.WriteLine(""); break; case "failed": // 事件为failed时,若没给出新日志地址,说明原日志没找到 if (newlogpath == null) { writer.WriteLine("找不到诊断日志文件 {0}\n", ServicePHM.logpath); break; } writer.WriteLine("执行过程出错!\t\t\t\t" + DateTime.Now.ToString()); writer.WriteLine("\n请查看日志文件 " + newlogpath); writer.WriteLine(""); break; } writer.Close(); }
private void runPredict() { ServiceLog.WriteLog("predict", null); n_predict++; CallUserExe.call("phm_home", exePredictPath, null); hasDiaged = false; hasPredicted = true; }
private void runDiag() { ServiceLog.WriteLog("diag", null); n_diag++; // 定时器默认会新开一个线程,无需手动写 CallUserExe.call("phm_home", exeDiagPath, null); hasDiaged = true; hasPredicted = false; }
// ------------------ 开始结束触发事件 --------------------- // protected override void OnStart(string[] args) { File.Delete(done_flag_path); string failed_dir = CallUserExe.getFullFilePath("phm_home", @"\Csharp代码\PHM运行失败日志"); if (!Directory.Exists(failed_dir)) { Directory.CreateDirectory(failed_dir); } ServiceLog.WriteLog("start", null); }