Ejemplo n.º 1
0
        public async Task <Response <string> > SetLogText(EnumLogCategory oLogCategory, string sName, string sMessage)
        {
            var oResponse = new Response <string>();

            try
            {
                if (!Directory.Exists(this._settings.Path))
                {
                    Directory.CreateDirectory(this._settings.Path);
                }

                DateTime dtCurrentDate = DateTime.Now;

                string sNewPathFolder = $@"{this._settings.Path}\LOG_{dtCurrentDate.Year}";

                if (!Directory.Exists(sNewPathFolder))
                {
                    Directory.CreateDirectory(sNewPathFolder);
                }

                string sFilename = $@"{sNewPathFolder}\LOG_{dtCurrentDate.Day.ToString("00")}-{dtCurrentDate.Month.ToString("00")}-{dtCurrentDate.Year}.log";
                File.AppendAllText(sFilename, sMessage, Encoding.UTF8);

                oResponse.IsSuccess = true;
            }
            catch (Exception ex)
            {
                oResponse.Message = $"[SetLogText-1]: {ex.Message}";
            }
            return(oResponse);
        }
Ejemplo n.º 2
0
        private string GetExceptionMessage(EnumLogCategory oLogCategory, string sName, Exception oException, string sData = null)
        {
            string sLogType = "UNKNOWN";

            if (oLogCategory == EnumLogCategory.INFORMATION)
            {
                sLogType = "INFORMATION";
            }
            else if (oLogCategory == EnumLogCategory.WARNING)
            {
                sLogType = "WARNING";
            }
            else if (oLogCategory == EnumLogCategory.ERROR)
            {
                sLogType = "ERROR";
            }

            StringBuilder sb = new StringBuilder();

            sb.AppendLine($"---------------------------------------------");
            sb.AppendLine($"[Type]: {sLogType}");
            sb.AppendLine($"[Date]: {DateTime.Now.ToString("dd/MM/yyyy - HH:mm:ss")}");
            sb.AppendLine($"[Name]: {sName}");
            sb.AppendLine($"[Message]: {oException.Message}");
            sb.AppendLine($"[StackTrace]: {oException.StackTrace}");
            sb.AppendLine($"[InnerException]: {oException.InnerException}");

            if (sData != null)
            {
                sb.AppendLine($"[Data]: {sData}");
            }

            return(sb.ToString());
        }
Ejemplo n.º 3
0
        public async Task <Response <string> > SetLog(EnumLogType oLogType, EnumLogCategory oLogCategory, string sName, Exception oException, params object[] oData)
        {
            var oResponse = new Response <string>();

            try
            {
                if (oLogType == EnumLogType.TEXT || oLogType == EnumLogType.TEXT_N_EMAIL)
                {
                    await this.SetLogText(oLogCategory, sName, oException, oData);
                }

                if (oLogType == EnumLogType.EMAIL || oLogType == EnumLogType.TEXT_N_EMAIL)
                {
                    await this.SetLogEmail(oLogCategory, sName, oException, oData);
                }

                oResponse.IsSuccess = true;
                return(oResponse);
            }
            catch (Exception ex)
            {
                oResponse.Message = $"[SetLogTextAndEmail-2]: {ex.Message}";
            }
            return(oResponse);
        }
Ejemplo n.º 4
0
 private void OnSaveLog(EnumLogCategory logCate, EnumLogType logType, string logContent)
 {
     if (this.eventSaveLog != null)
     {
         LogEventArgs logArgs = new LogEventArgs();
         logArgs.LogCate    = logCate;
         logArgs.LogType    = logType;
         logArgs.LogContent = logContent;
         logArgs.LogTime    = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
         this.BeginInvoke(eventSaveLog, this, logArgs);
     }
 }
Ejemplo n.º 5
0
 private void OnSaveErrorLog(EnumLogCategory logCate, EnumLogType logType, string logContent, int errorCode)
 {
     if (this.eventSaveLog != null)
     {
         ECAMSErrorEventArgs logArgs = new ECAMSErrorEventArgs();
         logArgs.LogCate    = logCate;
         logArgs.LogType    = logType;
         logArgs.LogContent = logContent;
         logArgs.ErrorCode  = errorCode;
         logArgs.LogTime    = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd HH:MM:ss"));
         this.eventSaveErrorLog.Invoke(this, logArgs);
     }
 }
Ejemplo n.º 6
0
        /// <summary>
        /// 添加日志
        /// </summary>
        /// <param name="EnumLogType"></param>
        /// <param name="logContent"></param>
        public void AddLog(EnumLogCategory logcate, EnumLogType EnumLogType, string logContent)
        {
            if (this.lastLogContent == logContent)
            {
                return;
            }

            if (showLog == true)
            {
                if (this.ListView_Log.InvokeRequired)
                {
                    AddLogInvoke addLogInvoke = new AddLogInvoke(AddLog);
                    this.Invoke(addLogInvoke, new object[3] {
                        logcate, EnumLogType, logContent
                    });
                }
                else
                {
                    ListViewItem viewItem = new ListViewItem();
                    if (EnumLogType == EnumLogType.错误)//错误用红色显示
                    {
                        viewItem.BackColor = Color.Red;
                    }
                    viewItem.SubItems.Add(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    viewItem.SubItems.Add(logContent);
                    viewItem.ToolTipText = logContent;
                    viewItem.SubItems.Add(logcate.ToString());
                    viewItem.SubItems.Add(EnumLogType.ToString());
                    viewItem.ImageIndex = 0;
                    this.lastLogContent = logContent;
                    if (this.cb_LogType.Text == EnumLogType.ToString() || this.cb_LogType.Text == EnumLogType.所有.ToString())
                    {
                        this.ListView_Log.Items.Add(viewItem);
                        this.ListView_Log.Items[this.ListView_Log.Items.Count - 1].EnsureVisible();
                        if (this.ListView_Log.Items.Count > 200)
                        {
                            this.ListView_Log.Items.RemoveAt(0);
                        }
                    }
                    Application.DoEvents();

                    //modify by zwx,防止重复添加日志到数据库
                    //OnSaveLog(logcate, EnumLogType, logContent);
                }
            }
            //else
            //{
            //    OnSaveLog(logcate, EnumLogType, logContent);
            //}
        }
Ejemplo n.º 7
0
        public async Task <Response <string> > SetLogText(EnumLogCategory oLogCategory, string sName, Exception oException, params object[] oData)
        {
            var oResponse = new Response <string>();

            try
            {
                oResponse = await this.SetLogText(oLogCategory, sName, this.GetExceptionMessageObject(oLogCategory, sName, oException, oData));
            }
            catch (Exception ex)
            {
                oResponse.Message = $"[SetLogText-2]: {ex.Message}";
            }
            return(oResponse);
        }
Ejemplo n.º 8
0
        public void AddLogErrorCode(EnumLogCategory logcate, EnumLogType EnumLogType, string logContent, int errorCode)
        {
            if (this.lastErrorLogContent == logContent)
            {
                return;
            }

            if (showLog == true)
            {
                if (this.ListView_Log.InvokeRequired)
                {
                    AddErrorLogInvoke addErrorLogInvoke = new AddErrorLogInvoke(AddLogErrorCode);
                    this.Invoke(addErrorLogInvoke, new object[4] {
                        logcate, EnumLogType, logContent, errorCode
                    });
                }
                else
                {
                    ListViewItem viewItem = new ListViewItem();
                    if (EnumLogType == EnumLogType.错误)//错误用红色显示
                    {
                        viewItem.BackColor = Color.Red;
                    }
                    viewItem.SubItems.Add(DateTime.Now.ToString());
                    viewItem.SubItems.Add(logContent);
                    viewItem.ToolTipText = logContent;
                    viewItem.SubItems.Add(logcate.ToString());
                    viewItem.SubItems.Add(EnumLogType.ToString());
                    viewItem.SubItems.Add(errorCode.ToString());
                    viewItem.ImageIndex      = 0;
                    this.lastErrorLogContent = logContent;
                    if (this.cb_LogType.Text == EnumLogType.ToString() || this.cb_LogType.Text == EnumLogType.所有.ToString())
                    {
                        this.ListView_Log.Items.Add(viewItem);
                        this.ListView_Log.Items[this.ListView_Log.Items.Count - 1].EnsureVisible();
                        if (this.ListView_Log.Items.Count > 200)//保留200条日志
                        {
                            this.ListView_Log.Items.RemoveAt(0);
                        }
                    }
                    Application.DoEvents();
                    //OnSaveErrorLog(logcate, EnumLogType, logContent, errorCode);
                }
            }
            //else
            //{
            //    OnSaveErrorLog(logcate, EnumLogType, logContent, errorCode);
            //}
        }
Ejemplo n.º 9
0
        private string GetExceptionMessageObject(EnumLogCategory oLogCategory, string sName, Exception oException, params object[] oData)
        {
            if (oData == null)
            {
                return(this.GetExceptionMessage(oLogCategory, sName, oException));
            }

            StringBuilder sb = new StringBuilder();
            int           n  = 0;

            foreach (object obj in oData)
            {
                sb.AppendLine();
                sb.AppendLine($"({++n}):");

                if (obj == null)
                {
                    sb.AppendLine("null");
                    continue;
                }

                try
                {
                    if (obj.GetType().IsClass)
                    {
                        sb.AppendLine(JsonConvert.SerializeObject(obj));
                    }
                    else
                    {
                        sb.AppendLine(obj.ToString());
                    }
                }
                catch (Exception ex)
                {
                    sb.AppendLine($"Exception: {ex.Message}");
                }
            }
            return(this.GetExceptionMessage(oLogCategory, sName, oException, sb.ToString()));
        }
Ejemplo n.º 10
0
        public async Task <Response <string> > SetLogEmail(EnumLogCategory oLogCategory, string sName, string sMessage)
        {
            var oResponse = new Response <string>();

            try
            {
                var oEmail = new EmailDto.Request();
                oEmail.sSendTo  = this._settings.EmailSendTo;
                oEmail.sReplyTo = this._settings.EmailReplyTo;
                oEmail.sSubject = string.Format(this._settings.EmailSubject, sName);
                oEmail.sMessage = sMessage.Replace("\r\n", "<br>");

                _ = this._emailApplication.SendEmail(oEmail);

                oResponse.IsSuccess = true;
            }
            catch (Exception ex)
            {
                oResponse.Message = $"[SetLogEmail-1]: {ex.Message}";
                _ = this.SetLogText(EnumLogCategory.ERROR, "SetLogEmail", ex);
            }
            return(oResponse);
        }