コード例 #1
0
ファイル: WindowsServiceBase.cs プロジェクト: ngnam/ZDevTools
        /// <summary>
        /// 写入报告(该方法允许多线程调用)
        /// </summary>
        /// <param name="serviceReport">服务报告</param>
        void writeReport(ServiceReport serviceReport)
        {
            if (_disposed) //记录日志功能失效
            {
                return;
            }

            lock (ReportLocker)
            {
                if (_reportStream == null)
                {
                    string reportsFolder = GetReportsFolder();

                    string reportFullName = Path.Combine(reportsFolder, ServiceName + ".log");

                    bool fileExists = File.Exists(reportFullName);

                    _reportStream       = File.Open(reportFullName, FileMode.Append, FileAccess.Write, FileShare.Read);
                    _reportStreamWriter = new StreamWriter(_reportStream);

                    if (!fileExists) //write log header
                    {
                        _reportStreamWriter.WriteLine("时间\t\t\t错误\t消息\t\t\t消息组");
                    }
                }

                _reportStreamWriter.WriteLine($"{FormatDateTime(serviceReport.UpdateTime)}\t{(serviceReport.HasError ? "[有]" : "[无]")}\t{serviceReport.Message}\t{(serviceReport.MessageArray == null ? null : string.Join("、", serviceReport.MessageArray))}");

                _reportStreamWriter.Flush();
            }
        }
コード例 #2
0
        public void ReportStatus(string message)
        {
            ServiceReport report = new ServiceReport();

            report.Message = message;

            reportStatus(report);
        }
コード例 #3
0
        public void ReportError(string message)
        {
            ServiceReport report = new ServiceReport();

            report.HasError = true;
            report.Message  = message;

            reportStatus(report);
        }
コード例 #4
0
        public void ReportStatus(ExecutionExtraInfo executionExtraInfo)
        {
            ServiceReport report = new ServiceReport();

            if (executionExtraInfo != null)
            {
                report.Message      = executionExtraInfo.WarnMessage;
                report.MessageArray = executionExtraInfo.WarnMessageArray;
            }

            reportStatus(report);
        }
コード例 #5
0
        public void ReportError(string message, Exception exception)
        {
            ServiceReport report = new ServiceReport();

            report.HasError     = true;
            report.Message      = message;
            report.MessageArray = new List <string>()
            {
                exception.Message
            };

            reportStatus(report);
        }
コード例 #6
0
        private void saveReportHistory(ServiceReport serviceReport)
        {
            string saveFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ServiceBase.ServiceReportsFolder);

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

            string reportFullName = Path.Combine(saveFolder, ServiceName + ".log");

            if (!File.Exists(reportFullName))
            {
                File.WriteAllText(reportFullName, "时间\t\t\t错误\t消息\t\t\t消息组\r\n");
            }

            File.AppendAllText(reportFullName, $"{ServiceBase.FormatDateTime(serviceReport.UpdateTime)}\t{(serviceReport.HasError ? "[有]" : "[无]")}\t{serviceReport.Message}\t{(serviceReport.MessageArray == null ? null : string.Join("、", serviceReport.MessageArray))}\r\n");
        }
コード例 #7
0
        void reportStatus(ServiceReport serviceReport)
        {
            try
            {
                serviceReport.ServiceName = DisplayName;
                serviceReport.UpdateTime  = DateTime.Now;

                saveReportHistory(serviceReport);

                if (ServiceBase.RedisManagerPool != null)
                {
                    using (var client = ServiceBase.RedisManagerPool.GetClient())
                    {
                        client.SetEntryInHash(RedisKeys.ServiceReports, ServiceName, JsonConvert.SerializeObject(serviceReport));
                        client.PublishMessage(RedisKeys.ServiceReports, ServiceName);
                    }
                }
            }
            catch (Exception ex) { LogWarn("状态汇报出错,错误:" + ex.Message, ex); }
        }