Esempio n. 1
0
        public async Task <IEnumerable <GameEvent> > ReadEventsFromLog(long fileSizeDiff, long startPosition)
        {
            // allocate the bytes for the new log lines
            List <string> logLines = new List <string>();

            // open the file as a stream
            using (FileStream fs = new FileStream(_logFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                byte[] buff = new byte[fileSizeDiff];
                fs.Seek(startPosition, SeekOrigin.Begin);
                await fs.ReadAsync(buff, 0, (int)fileSizeDiff);

                var    stringBuilder = new StringBuilder();
                char[] charBuff      = Utilities.EncodingType.GetChars(buff);

                foreach (char c in charBuff)
                {
                    if (c == '\n')
                    {
                        logLines.Add(stringBuilder.ToString());
                        stringBuilder = new StringBuilder();
                    }

                    else if (c != '\r')
                    {
                        stringBuilder.Append(c);
                    }
                }

                if (stringBuilder.Length > 0)
                {
                    logLines.Add(stringBuilder.ToString());
                }
            }

            List <GameEvent> events = new List <GameEvent>();

            // parse each line
            foreach (string eventLine in logLines.Where(_line => _line.Length > 0))
            {
                try
                {
                    var gameEvent = _parser.GenerateGameEvent(eventLine);
                    events.Add(gameEvent);
                }

                catch (Exception e)
                {
                    _logger.WriteWarning("Could not properly parse event line");
                    _logger.WriteDebug(e.Message);
                    _logger.WriteDebug(eventLine);
                }
            }

            return(events);
        }
Esempio n. 2
0
        public async Task <IEnumerable <GameEvent> > ReadEventsFromLog(long fileSizeDiff, long startPosition)
        {
            var events   = new List <GameEvent>();
            var response = await _logServerApi.Log(_safeLogPath, lastKey);

            lastKey = response.NextKey;

            if (!response.Success && string.IsNullOrEmpty(lastKey))
            {
                _logger.WriteError($"Could not get log server info of {_safeLogPath}");
                return(events);
            }

            else if (!string.IsNullOrWhiteSpace(response.Data))
            {
                // parse each line
                var lines = response.Data
                            .Split(Environment.NewLine)
                            .Where(_line => _line.Length > 0);

                foreach (string eventLine in lines)
                {
                    try
                    {
                        // this trim end should hopefully fix the nasty runaway regex
                        var gameEvent = _eventParser.GenerateGameEvent(eventLine.TrimEnd('\r'));
                        events.Add(gameEvent);
                    }

                    catch (Exception e)
                    {
                        _logger.WriteError("Could not properly parse event line from http");
                        _logger.WriteDebug(e.Message);
                        _logger.WriteDebug(eventLine);
                    }
                }
            }

            return(events);
        }