记录服务日志

 protected override void OnStart(string[] args)  
        {  
            this.WriteLog("\n当前时间:" + DateTime.Now.ToString("yyyy-MM-dd hh-mm-ss") + "\n");  
            this.WriteLog("客户端数据同步服务:【服务启动】");  
        }  
 
        protected override void OnStop()  
        {  
            this.WriteLog("\n当前时间:" + DateTime.Now.ToString("yyyy-MM-dd hh-mm-ss")+ "\n");  
            this.WriteLog("客户端数据同步服务:【服务停止】");  
        }  
        protected override void OnShutdown()  
        {  
            this.WriteLog("\n当前时间:" + DateTime.Now.ToString("yyyy-MM-dd hh-mm-ss") + "\n");  
            this.WriteLog("客户端数据同步服务:【计算机关闭】");  
        }  
{ ok = false, rst = "未能加载文件或程序集“Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed”或它的某一个依赖项。找到的程序集清单定义与程序集引用不匹配。 (异常来自 HRESULT:0x80131040)" }
        #region 记录日志  
        /// <summary>  
        /// 记录日志  
        /// </summary>  
        /// <param name="msg"></param>  
        private void WriteLog(string msg)  
        {  
 
            //string path = @"C:\log.txt";  
 
            //该日志文件会存在windows服务程序目录下  
            string path = AppDomain.CurrentDomain.BaseDirectory + "\\log.txt";  
            FileInfo file = new FileInfo(path);  
            if (!file.Exists)  
            {  
                FileStream fs;  
                fs = File.Create(path);  
                fs.Close();  
            }  
 
            using (FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write))  
            {  
                using (StreamWriter sw = new StreamWriter(fs))  
                {  
                    sw.WriteLine(DateTime.Now.ToString() + "   " + msg);  
                }  
            }  
        }  
        #endregion 

posted @ 2018-07-02 14:40  苏苏苏en  阅读(153)  评论(0编辑  收藏  举报