Ejemplo n.º 1
0
        //设置输出格式
        void _service_OnMessage(object Sender, MessageEventArgs Args)
        {
            if (tbx_Message.Text.Length > 30000) tbx_Message.Text = "";
            tbx_Message.AppendText(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

            string str = "";
            if (Args.MessageCode != 0) str += " 错误号:" + Args.MessageCode.ToString() + "\r\n";
            str += "消息内容:" + Args.Message + "\r\n";
            str += "\r\n";
            tbx_Message.AppendText(str);
        }
        private void SendMessage(int code, string mess)
        {
            if (_message != null)
            {
                MessageEventArgs e = new MessageEventArgs(code, mess);
                _message.Invoke(this, e);

                #region 将错误消息写入日志文件
                try
                {
                    string filename = _logfilepath;
                    if (!filename.EndsWith("\\")) filename += "\\";

                    if (!Directory.Exists(filename)) Directory.CreateDirectory(filename);

                    if (code == 0)
                        filename += "Log" + DateTime.Today.ToString("yyyyMMdd") + ".txt";
                    else
                        filename += "Error" + DateTime.Today.ToString("yyyyMMdd") + ".txt";

                    StreamWriter stream = null;
                    if (File.Exists(filename))
                        stream = File.AppendText(filename);
                    else
                        stream = File.CreateText(filename);

                    stream.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    if (code != 0) stream.WriteLine("错误号:" + code.ToString());
                    stream.WriteLine(mess);
                    stream.WriteLine();

                    stream.Close();
                }
                catch { }
                #endregion
            }
        }