Пример #1
0
        /// <summary>
        /// 監視なしで、単一のログファイルを読み込む。
        /// </summary>
        /// <param name="instDir"></param>
        /// <param name="date"></param>
        /// <param name="receiver"></param>
        public static void CheckWholeFile(string instDir, DateTime date, TwLogReceiver receiver)
        {
            string logfile = String.Format(@"{0}\ChatLog\TWChatLog_{1}.html", instDir, date.ToString("yyyy_MM_dd"));

            if (!File.Exists(logfile))
            {
                return;
            }

            using (FileStream stream = new FileStream(logfile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                using (TextReader sr = new StreamReader(stream, Encoding.GetEncoding("Shift-JIS")))
                {
                    string line;
                    while ((line = sr.ReadLine()) != null)
                    {
                        TwChatLog log = TwChatLog.createChatLog(date.ToString("yyyy/MM/dd"), line);
                        if (log == null)
                        {
                            continue;
                        }
                        receiver.Receive(log);
                    }
                }
            }
        }
Пример #2
0
        private void WatchingThread()
        {
            DateTime dateTime   = DateTime.Now.Date;
            string   dateString = dateTime.ToString("yyyy/MM/dd");
            FileInfo fileInfo   = new FileInfo(String.Format(@"{0}\TWChatLog_{1}.html", LogDir, dateTime.ToString("yyyy_MM_dd")));
            long     prevSize   = fileInfo.Exists ? fileInfo.Length : 0;

            while (IsWatching)
            {
                try
                {
                    fileInfo.Refresh();
                    if (!fileInfo.Exists || prevSize == fileInfo.Length)
                    {
                        // サイズ変更ない場合、日付が変わったかもしれない
                        if (dateTime < DateTime.Now.Date)
                        {
                            dateTime   = DateTime.Now.Date;
                            dateString = dateTime.ToString("yyyy/MM/dd");
                            fileInfo   = new FileInfo(String.Format(@"{0}\TWChatLog_{1}.html", LogDir, dateTime.ToString("yyyy_MM_dd")));
                            prevSize   = 0;

                            Logger.Instance.PutMessage($"Change target log file to {fileInfo.FullName}");
                        }

                        continue;
                    }

                    using (FileStream fileStream = fileInfo.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                    {
                        fileStream.Position = prevSize;
                        using (StreamReader reader = new StreamReader(fileStream, Encoding.GetEncoding("shift_jis")))
                        {
                            string line;
                            while ((line = reader.ReadLine()) != null)
                            {
                                TwChatLog log = TwChatLog.createChatLog(dateString, line);
                                if (log == null)
                                {
                                    Logger.Instance.PutMessage($"Invalid format message: {line}");
                                    continue;
                                }
                                Receiver.Receive(log);
                            }
                            prevSize = fileStream.Position;
                        }
                    }
                }
                catch (Exception ex)
                {
                    Logger.Instance.PutMessage($"File Access Error: {ex.Message}");
                    Logger.Instance.PutMessage($"{ex.StackTrace}");
                }
                finally
                {
                    Thread.Sleep(1000);
                }
            }
        }