Ejemplo n.º 1
0
        public static void StartSave()
        {
            try
            {
                if (_saveTimer == null)
                {
                    var conf = new ConfigurationBuilder()
                               .AddJsonFile("AppConfig.json")
                               .Build();
                    _localLogPath = conf.GetSection("AppConfig:LocalLogPath").Value;

                    _saveTimer = new Timer(new TimerCallback(async d =>
                    {
                        if (!_isSaveing)
                        {
                            _isSaveing = true;

                            StringBuilder sb = new StringBuilder();

                            while (_logQueue.Count > 0)
                            {
                                logInfo l = _logQueue.Dequeue();
                                sb.AppendFormat("{0} {1} {2}\r\n", l.Time.ToString(), l.Type, l.Msg);
                            }

                            string logStr = sb.ToString();

                            if (!string.IsNullOrWhiteSpace(logStr))
                            {
                                string dateLocalLogPath = string.Format("{0}_{1}.txt", _localLogPath, DateTime.Now.ToString("yyyyMMdd"));

                                FileStream fs   = new FileStream(dateLocalLogPath, FileMode.Append, FileAccess.Write);
                                StreamWriter sr = new StreamWriter(fs);

                                await sr.WriteLineAsync(logStr);
                                Console.WriteLine(logStr);

                                sr.Dispose();
                                fs.Dispose();
                            }

                            _isSaveing = false;
                        }
                    }), new object(), 0, 5000);
                }
            }
            catch (Exception)
            {
            }
        }
Ejemplo n.º 2
0
        private static void AddLocalLog(string type, string msg)
        {
            try
            {
                logInfo loginfo = new logInfo
                {
                    Type = type,
                    Msg  = msg,
                    Time = DateTime.Now
                };

                Task.Run(() => {
                    lock (_logQueue)
                    {
                        _logQueue.Enqueue(loginfo);
                    }
                });
            }
            catch (Exception)
            {
            }
        }