Example #1
0
        public LogEvent ReadLogEvent(ExtendedBinaryReader reader)
        {
            var evt = new LogEvent
            {
                TransactionId        = GetTextById(reader.Read7BitEncodedInt()),
                Text                 = reader.ReadString(),
                Severity             = (LogSeverity)reader.ReadByte(),
                ProcessInvocationUID = reader.ReadNullableInt32()
            };

            var argCount = reader.Read7BitEncodedInt();

            if (argCount > 0)
            {
                evt.Arguments = new KeyValuePair <string, object> [argCount];
                for (var i = 0; i < argCount; i++)
                {
                    var key   = GetTextById(reader.Read7BitEncodedInt());
                    var value = reader.ReadObject();
                    evt.Arguments[i] = new KeyValuePair <string, object>(key, value);
                }
            }

            return(evt);
        }
Example #2
0
 public RowOwnerChangedEvent ReadRowOwnerChangedEvent(ExtendedBinaryReader reader)
 {
     return(new RowOwnerChangedEvent
     {
         RowUid = reader.Read7BitEncodedInt(),
         PreviousProcessInvocationUid = reader.Read7BitEncodedInt(),
         NewProcessInvocationUid = reader.ReadNullableInt32()
     });
 }
Example #3
0
        public RowStoreStartedEvent ReadRowStoreStartedEvent(ExtendedBinaryReader reader)
        {
            var evt = new RowStoreStartedEvent
            {
                UID      = reader.Read7BitEncodedInt(),
                Location = GetTextById(reader.Read7BitEncodedInt()),
                Path     = GetTextById(reader.Read7BitEncodedInt()),
            };

            return(evt);
        }
Example #4
0
 public ProcessInvocationStartEvent ReadProcessInvocationStartEvent(ExtendedBinaryReader reader)
 {
     return(new ProcessInvocationStartEvent
     {
         InvocationUID = reader.Read7BitEncodedInt(),
         InstanceUID = reader.Read7BitEncodedInt(),
         InvocationCounter = reader.Read7BitEncodedInt(),
         Type = reader.ReadString(),
         Kind = (ProcessKind)reader.ReadByte(),
         Name = reader.ReadString(),
         Topic = reader.ReadNullableString(),
         CallerInvocationUID = reader.ReadNullableInt32()
     });
 }
Example #5
0
        public IoCommandStartEvent ReadIoCommandStartEvent(ExtendedBinaryReader reader)
        {
            var evt = new IoCommandStartEvent
            {
                Uid = reader.Read7BitEncodedInt(),
                ProcessInvocationUid = reader.Read7BitEncodedInt(),
                Kind           = (IoCommandKind)reader.ReadByte(),
                Location       = GetTextById(reader.Read7BitEncodedInt()),
                Path           = GetTextById(reader.Read7BitEncodedInt()),
                TimeoutSeconds = reader.ReadNullableInt32(),
                Command        = reader.ReadNullableString(),
                TransactionId  = GetTextById(reader.Read7BitEncodedInt()),
            };

            var argCount = reader.Read7BitEncodedInt();

            if (argCount > 0)
            {
                evt.Arguments = new KeyValuePair <string, object> [argCount];
                for (var i = 0; i < argCount; i++)
                {
                    var name  = GetTextById(reader.Read7BitEncodedInt());
                    var value = reader.ReadObject();
                    evt.Arguments[i] = new KeyValuePair <string, object>(name, value);
                }
            }

            return(evt);
        }
Example #6
0
 public ProcessInvocationEndEvent ReadProcessInvocationEndEvent(ExtendedBinaryReader reader)
 {
     return(new ProcessInvocationEndEvent
     {
         InvocationUID = reader.Read7BitEncodedInt(),
         ElapsedMilliseconds = reader.ReadInt64(),
         NetTimeMilliseconds = reader.ReadNullableInt64(),
     });
 }
Example #7
0
        public IoCommandEndEvent ReadIoCommandEndEvent(ExtendedBinaryReader reader)
        {
            var evt = new IoCommandEndEvent
            {
                Uid = reader.Read7BitEncodedInt(),
                AffectedDataCount = reader.ReadNullableInt32(),
                ErrorMessage      = reader.ReadNullableString(),
            };

            return(evt);
        }
Example #8
0
        public RowCreatedEvent ReadRowCreatedEvent(ExtendedBinaryReader reader)
        {
            var evt = new RowCreatedEvent
            {
                ProcessInvocationUid = reader.Read7BitEncodedInt(),
                RowUid = reader.Read7BitEncodedInt()
            };

            var columnCount = reader.Read7BitEncodedInt();

            if (columnCount > 0)
            {
                evt.Values = new KeyValuePair <string, object> [columnCount];
                for (var i = 0; i < columnCount; i++)
                {
                    var column = GetTextById(reader.Read7BitEncodedInt());
                    var value  = reader.ReadObject();
                    evt.Values[i] = new KeyValuePair <string, object>(column, value);
                }
            }

            return(evt);
        }
Example #9
0
        public HashSet <int> GetProcessRowMap(int processInvocationUid)
        {
            var result = new HashSet <int>();

            var fileName = GetProcessRowMapFileName(processInvocationUid);

            if (!File.Exists(fileName))
            {
                return(result);
            }

            lock (_processRowMapWritersLock)
            {
                if (_processRowMapWriters.TryGetValue(processInvocationUid, out var writer))
                {
                    writer.Flush();
                }
            }

            using (var fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                using (var memoryCache = new MemoryStream())
                {
                    fileStream.CopyTo(memoryCache);
                    memoryCache.Position = 0;

                    using (var reader = new ExtendedBinaryReader(memoryCache, Encoding.UTF8))
                    {
                        var length = memoryCache.Length;
                        while (memoryCache.Position < length)
                        {
                            try
                            {
                                var rowUid = reader.Read7BitEncodedInt();
                                result.Add(rowUid);
                            }
                            catch (Exception)
                            {
                            }
                        }
                    }
                }

            return(result);
        }
Example #10
0
        public List <AbstractEvent> Append(MemoryStream input)
        {
            var length = input.Length;

            if (_lastMainFileSize + length > 1024 * 1024 * 25)
            {
                _lastMainFileIndex++;
                _lastMainFileSize = 0;
            }

            var mainFileName = GetMainFileName(_lastMainFileIndex);

            using (var fw = new FileStream(mainFileName, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
            {
                input.CopyTo(fw);
                _lastMainFileSize += length;
            }

            input.Position = 0;

            var events = new List <AbstractEvent>();

            using (var reader = new ExtendedBinaryReader(input, Encoding.UTF8))
            {
                while (reader.BaseStream.Position < length)
                {
                    var startPosition = input.Position;
                    var eventKind     = (DiagnosticsEventKind)reader.ReadByte();

                    var eventDataPosition = input.Position;
                    var eventDataSize     = reader.ReadInt32(); // eventDataSize

                    if (eventKind == DiagnosticsEventKind.TextDictionaryKeyAdded)
                    {
                        var key  = reader.Read7BitEncodedInt();
                        var text = reader.ReadNullableString();
                        _eventParser.AddText(key, text);
                        continue;
                    }

                    var timestamp = reader.ReadInt64();

                    if (eventKind == DiagnosticsEventKind.ContextEnded)
                    {
                        foreach (var stream in _openStoreWriterStreams.Values)
                        {
                            stream.Flush();
                            stream.Dispose();
                        }

                        _openStoreWriterStreams.Clear();

                        if (_rowEventStream != null)
                        {
                            _rowEventStream.Flush();
                            _rowEventStream.Dispose();
                            _rowEventStream = null;
                        }

                        var writers = _processRowMapWriters.Values.ToList();
                        _processRowMapWriters.Clear();

                        foreach (var writer in writers)
                        {
                            writer.Flush();
                            writer.Dispose();
                        }

                        EndedOn = new DateTime(timestamp);
                        continue;
                    }

                    var evt = eventKind switch
                    {
                        DiagnosticsEventKind.Log => (AbstractEvent)_eventParser.ReadLogEvent(reader),
                        DiagnosticsEventKind.RowCreated => _eventParser.ReadRowCreatedEvent(reader),
                        DiagnosticsEventKind.RowOwnerChanged => _eventParser.ReadRowOwnerChangedEvent(reader),
                        DiagnosticsEventKind.RowValueChanged => _eventParser.ReadRowValueChangedEvent(reader),
                        DiagnosticsEventKind.RowStoreStarted => _eventParser.ReadRowStoreStartedEvent(reader),
                        DiagnosticsEventKind.RowStored => _eventParser.ReadRowStoredEvent(reader),
                        DiagnosticsEventKind.ProcessInvocationStart => _eventParser.ReadProcessInvocationStartEvent(reader),
                        DiagnosticsEventKind.ProcessInvocationEnd => _eventParser.ReadProcessInvocationEndEvent(reader),
                        DiagnosticsEventKind.IoCommandStart => _eventParser.ReadIoCommandStartEvent(reader),
                        DiagnosticsEventKind.IoCommandEnd => _eventParser.ReadIoCommandEndEvent(reader),
                        _ => null,
                    };

                    evt.Timestamp = timestamp;
                    events.Add(evt);

                    if (evt is RowStoredEvent rse)
                    {
                        var eventBytes = input.ReadFrom(eventDataPosition, (int)(input.Position - eventDataPosition));

                        lock (_openStoreWriterStreamsLock)
                        {
                            if (!_openStoreWriterStreams.TryGetValue(rse.StoreUid, out var storeWriterStream))
                            {
                                var storeFileName = GetStoreFileName(rse.StoreUid);
                                storeWriterStream = new FileStream(storeFileName, FileMode.Append, FileAccess.Write, FileShare.ReadWrite, 512 * 1024);
                                _openStoreWriterStreams.Add(rse.StoreUid, storeWriterStream);
                            }

                            storeWriterStream.Write(eventBytes, 0, eventBytes.Length);
                        }
                    }
                    else if (evt is RowCreatedEvent || evt is RowValueChangedEvent || evt is RowOwnerChangedEvent)
                    {
                        var eventBytes = input.ReadFrom(startPosition, (int)(input.Position - startPosition));

                        lock (_rowEventStreamLock)
                        {
                            if (_lastRowEventFileSize + eventBytes.Length > 1024 * 1024 * 25)
                            {
                                _lastRowEventFileIndex++;
                                _lastRowEventFileSize = 0;
                                if (_rowEventStream != null)
                                {
                                    _rowEventStream.Flush();
                                    _rowEventStream.Dispose();
                                    _rowEventStream = null;
                                }
                            }

                            if (_rowEventStream == null)
                            {
                                var rowEventfileName = GetRowEventFileName(_lastRowEventFileIndex);
                                _rowEventStream = new FileStream(rowEventfileName, FileMode.Append, FileAccess.Write, FileShare.ReadWrite, 512 * 1024);
                            }
                        }

                        _rowEventStream.Write(eventBytes, 0, eventBytes.Length);
                        _lastRowEventFileSize += eventBytes.Length;

                        var involvedProcessUid = evt is RowCreatedEvent rce
                            ? rce.ProcessInvocationUid
                            : (evt is RowOwnerChangedEvent roce)
                                ? roce.NewProcessInvocationUid
                                : null;

                        if (involvedProcessUid != null)
                        {
                            lock (_processRowMapWritersLock)
                            {
                                if (!_processRowMapWriters.TryGetValue(involvedProcessUid.Value, out var writer))
                                {
                                    var processRowMapFileName = GetProcessRowMapFileName(involvedProcessUid.Value);
                                    var stream = new FileStream(processRowMapFileName, FileMode.Append, FileAccess.Write, FileShare.ReadWrite, 128 * 1024);
                                    writer = new ExtendedBinaryWriter(stream, Encoding.UTF8);
                                    _processRowMapWriters.Add(involvedProcessUid.Value, writer);
                                }

                                writer.Write7BitEncodedInt((evt as AbstractRowEvent).RowUid);
                            }
                        }
                    }
                }
            }

            return(events);
        }