예제 #1
0
 /// <summary>
 /// Called when the current file is closed.
 /// </summary>
 protected override void CloseCurrentFile()
 {
     _writer.Write((byte)0);
     base.CloseCurrentFile();
     _writer.Dispose();
     _writer = null;
 }
예제 #2
0
        public void reading_and_writing_CKExceptionData_with_BinaryWriter_and_BinaryReader()
        {
            var dataE0 = CKExceptionData.CreateFrom(ThrowAggregatedException());
            var dataE1 = CKExceptionData.CreateFrom(ThrowSimpleException("Test Message"));
            var dataE2 = CKExceptionData.CreateFrom(ThrowLoaderException());
            var dataE3 = CKExceptionData.CreateFrom(ThrowExceptionWithInner());
            var dataE4 = CKExceptionData.CreateFrom(ThrowTwoInnerExceptions());

            using (var mem = new MemoryStream())
            {
                CKBinaryWriter w = new CKBinaryWriter(mem);
                dataE0.Write(w);
                dataE1.Write(w);
                dataE2.Write(w);
                dataE3.Write(w);
                dataE4.Write(w);
                mem.Position = 0;
                var r     = new CKBinaryReader(mem);
                var data0 = new CKExceptionData(r, StringAndStringBuilderExtension.IsCRLF);
                data0.ToString().Should().Be(dataE0.ToString());
                var data1 = new CKExceptionData(r, StringAndStringBuilderExtension.IsCRLF);
                data1.ToString().Should().Be(dataE1.ToString());
                var data2 = new CKExceptionData(r, StringAndStringBuilderExtension.IsCRLF);
                data2.ToString().Should().Be(dataE2.ToString());
                var data3 = new CKExceptionData(r, StringAndStringBuilderExtension.IsCRLF);
                data3.ToString().Should().Be(dataE3.ToString());
                var data4 = new CKExceptionData(r, StringAndStringBuilderExtension.IsCRLF);
                data4.ToString().Should().Be(dataE4.ToString());
            }
        }
예제 #3
0
 static void DoWriteCloseGroup(CKBinaryWriter w, StreamLogType t, LogLevel level, DateTimeStamp closeTime, IReadOnlyList <ActivityLogGroupConclusion>?conclusions)
 {
     if (conclusions != null && conclusions.Count > 0)
     {
         t |= StreamLogType.HasConclusions;
     }
     if (closeTime.Uniquifier != 0)
     {
         t |= StreamLogType.HasUniquifier;
     }
     WriteLogTypeAndLevel(w, t, level);
     w.Write(closeTime.TimeUtc.ToBinary());
     if (closeTime.Uniquifier != 0)
     {
         w.Write(closeTime.Uniquifier);
     }
     if ((t & StreamLogType.HasConclusions) != 0)
     {
         Debug.Assert(conclusions != null);
         w.WriteNonNegativeSmallInt32(conclusions.Count);
         foreach (ActivityLogGroupConclusion c in conclusions)
         {
             w.Write(c.Tag.ToString());
             w.Write(c.Text);
         }
     }
 }
예제 #4
0
 /// <summary>
 /// Binary writes a log entry.
 /// </summary>
 /// <param name="w">Binary writer to use.</param>
 /// <param name="isOpenGroup">True if this the opening of a group. False for a line.</param>
 /// <param name="level">Log level of the log entry.</param>
 /// <param name="text">Text of the log entry.</param>
 /// <param name="logTime">Time stamp of the log entry.</param>
 /// <param name="tags">Tags of the log entry</param>
 /// <param name="ex">Exception of the log entry.</param>
 /// <param name="fileName">Source file name of the log entry</param>
 /// <param name="lineNumber">Source line number of the log entry</param>
 static public void WriteLog(CKBinaryWriter w, bool isOpenGroup, LogLevel level, DateTimeStamp logTime, string text, CKTrait tags, CKExceptionData ex, string fileName, int lineNumber)
 {
     if (w == null)
     {
         throw new ArgumentNullException("w");
     }
     DoWriteLog(w, isOpenGroup ? StreamLogType.TypeOpenGroup : StreamLogType.TypeLine, level, logTime, text, tags, ex, fileName, lineNumber);
 }
예제 #5
0
 /// <summary>
 /// Binary writes a closing entry.
 /// </summary>
 /// <param name="w">Binary writer to use.</param>
 /// <param name="level">Log level of the log entry.</param>
 /// <param name="closeTime">Time stamp of the group closing.</param>
 /// <param name="conclusions">Group conclusions.</param>
 static public void WriteCloseGroup(CKBinaryWriter w, LogLevel level, DateTimeStamp closeTime, IReadOnlyList <ActivityLogGroupConclusion> conclusions)
 {
     if (w == null)
     {
         throw new ArgumentNullException("w");
     }
     DoWriteCloseGroup(w, StreamLogType.TypeGroupClosed, level, closeTime, conclusions);
 }
예제 #6
0
        /// <summary>
        /// Called when a new file is created.
        /// </summary>
        /// <returns>The created stream.</returns>
        protected override Stream OpenNewFile()
        {
            Stream s = base.OpenNewFile();

            _writer = new CKBinaryWriter(s);
            _writer.Write(LogReader.FileHeader);
            _writer.Write(LogReader.CurrentStreamVersion);
            return(s);
        }
예제 #7
0
        public void basic_types_writing_and_reading()
        {
            using (var mem = new MemoryStream())
            {
                var sShared = Guid.NewGuid().ToString();
                using (var w = new CKBinaryWriter(mem, Encoding.UTF8, true))
                {
                    w.WriteNullableString(DefString);
                    w.Write(DefInt32);
                    w.Write(DefUInt32);
                    w.Write(DefInt64);
                    w.Write(DefUInt64);
                    w.Write(DefInt16);
                    w.Write(DefUInt16);
                    w.Write(DefByte);
                    w.Write(DefSByte);
                    w.Write(DefDateTime);
                    w.Write(DefTimeSpan);

                    w.WriteSharedString(sShared);

                    w.Write(DefDateTimeOffset);
                    w.Write(DefGuid);
                    w.Write(DefDouble);
                    w.Write(DefSingle);
                    w.Write(DefChar);
                    w.Write(DefBoolean);

                    w.WriteSharedString(sShared);
                }
                mem.Position = 0;
                using (var r = new CKBinaryReader(mem, Encoding.UTF8, true))
                {
                    r.ReadNullableString().Should().Be(DefString);
                    r.ReadInt32().Should().Be(DefInt32);
                    r.ReadUInt32().Should().Be(DefUInt32);
                    r.ReadInt64().Should().Be(DefInt64);
                    r.ReadUInt64().Should().Be(DefUInt64);
                    r.ReadInt16().Should().Be(DefInt16);
                    r.ReadUInt16().Should().Be(DefUInt16);
                    r.ReadByte().Should().Be(DefByte);
                    r.ReadSByte().Should().Be(DefSByte);
                    r.ReadDateTime().Should().Be(DefDateTime);
                    r.ReadTimeSpan().Should().Be(DefTimeSpan);

                    r.ReadSharedString().Should().Be(sShared);

                    r.ReadDateTimeOffset().Should().Be(DefDateTimeOffset);
                    r.ReadGuid().Should().Be(DefGuid);
                    r.ReadDouble().Should().Be(DefDouble);
                    r.ReadSingle().Should().Be(DefSingle);
                    r.ReadChar().Should().Be(DefChar);
                    r.ReadBoolean().Should().Be(DefBoolean);
                    r.ReadSharedString().Should().Be(sShared);
                }
            }
        }
예제 #8
0
        /// <summary>
        /// Binary writes a multicast closing entry.
        /// </summary>
        /// <param name="w">Binary writer to use.</param>
        /// <param name="monitorId">Identifier of the monitor.</param>
        /// <param name="previousEntryType">Log type of the previous entry in the monitor..</param>
        /// <param name="previousLogTime">Time stamp of the previous entry in the monitor.</param>
        /// <param name="depth">Depth of the group (number of opened groups above).</param>
        /// <param name="level">Log level of the log entry.</param>
        /// <param name="closeTime">Time stamp of the group closing.</param>
        /// <param name="conclusions">Group conclusions.</param>
        static public void WriteCloseGroup(CKBinaryWriter w, Guid monitorId, LogEntryType previousEntryType, DateTimeStamp previousLogTime, int depth, LogLevel level, DateTimeStamp closeTime, IReadOnlyList <ActivityLogGroupConclusion> conclusions)
        {
            if (w == null)
            {
                throw new ArgumentNullException("w");
            }
            StreamLogType type = StreamLogType.TypeGroupClosed | StreamLogType.IsMultiCast;

            type = UpdateTypeWithPrevious(type, previousEntryType, ref previousLogTime);
            DoWriteCloseGroup(w, type, level, closeTime, conclusions);
            WriteMulticastFooter(w, monitorId, previousEntryType, previousLogTime, depth);
        }
예제 #9
0
 void ComputeSize(GrandOutputEventInfo logEvent, bool increment)
 {
     using (MemoryStream m = new MemoryStream())
         using (CKBinaryWriter w = new CKBinaryWriter(m))
         {
             logEvent.Entry.WriteLogEntry(w);
             if (increment)
             {
                 Interlocked.Add(ref SizeHandled, (int)m.Position);
             }
         }
 }
예제 #10
0
        /// <summary>
        /// Binary writes a multicast log entry.
        /// </summary>
        /// <param name="w">Binary writer to use.</param>
        /// <param name="monitorId">Identifier of the monitor.</param>
        /// <param name="previousEntryType">Log type of the previous entry in the monitor..</param>
        /// <param name="previousLogTime">Time stamp of the previous entry in the monitor.</param>
        /// <param name="depth">Depth of the line (number of opened groups above).</param>
        /// <param name="isOpenGroup">True if this the opening of a group. False for a line.</param>
        /// <param name="text">Text of the log entry.</param>
        /// <param name="level">Log level of the log entry.</param>
        /// <param name="logTime">Time stamp of the log entry.</param>
        /// <param name="tags">Tags of the log entry</param>
        /// <param name="ex">Exception of the log entry.</param>
        /// <param name="fileName">Source file name of the log entry</param>
        /// <param name="lineNumber">Source line number of the log entry</param>
        static public void WriteLog(CKBinaryWriter w, Guid monitorId, LogEntryType previousEntryType, DateTimeStamp previousLogTime, int depth, bool isOpenGroup, LogLevel level, DateTimeStamp logTime, string text, CKTrait tags, CKExceptionData ex, string fileName, int lineNumber)
        {
            if (w == null)
            {
                throw new ArgumentNullException("w");
            }
            StreamLogType type = StreamLogType.IsMultiCast | (isOpenGroup ? StreamLogType.TypeOpenGroup : StreamLogType.TypeLine);

            type = UpdateTypeWithPrevious(type, previousEntryType, ref previousLogTime);
            DoWriteLog(w, type, level, logTime, text, tags, ex, fileName, lineNumber);
            WriteMulticastFooter(w, monitorId, previousEntryType, previousLogTime, depth);
        }
예제 #11
0
        public void LogEntry_write_and_read_back()
        {
            var exInner = new CKExceptionData("message", "typeof(exception)", "assemblyQualifiedName", "stackTrace", null, "fileName", "fusionLog", null, null);
            var ex2     = new CKExceptionData("message2", "typeof(exception2)", "assemblyQualifiedName2", "stackTrace2", exInner, "fileName2", "fusionLog2", null, null);
            var exL     = new CKExceptionData("loader-message", "typeof(loader-exception)", "loader-assemblyQualifiedName", "loader-stackTrace", null, "loader-fileName", "loader-fusionLog", null, null);
            var exAgg   = new CKExceptionData("agg-message", "typeof(agg-exception)", "agg-assemblyQualifiedName", "agg-stackTrace", ex2, "fileName", "fusionLog", null, new[] { ex2, exL });

            var       prevLog = DateTimeStamp.UtcNow;
            ILogEntry e1      = LogEntry.CreateLog("Text1", new DateTimeStamp(DateTime.UtcNow, 42), LogLevel.Info, "c:\\test.cs", 3712, ActivityMonitor.Tags.CreateDependentToken, exAgg);
            ILogEntry e2      = LogEntry.CreateMulticastLog("GOId", "3712", LogEntryType.Line, prevLog, 5, "Text2", DateTimeStamp.UtcNow, LogLevel.Fatal, null, 3712, ActivityMonitor.Tags.CreateDependentToken, exAgg);;

            using (var mem = new MemoryStream())
                using (var w = new CKBinaryWriter(mem))
                {
                    w.Write(LogReader.CurrentStreamVersion);
                    e1.WriteLogEntry(w);
                    e2.WriteLogEntry(w);
                    w.Write((byte)0);
                    w.Flush();

                    byte[] versionBytes = new byte[4];
                    mem.Position = 0;
                    mem.Read(versionBytes, 0, 4);
                    BitConverter.ToInt32(versionBytes, 0).Should().Be(LogReader.CurrentStreamVersion);

                    using (var reader = new LogReader(mem, LogReader.CurrentStreamVersion, 4))
                    {
                        reader.MoveNext().Should().BeTrue();
                        reader.Current.Text.Should().Be(e1.Text);
                        reader.Current.LogLevel.Should().Be(e1.LogLevel);
                        reader.Current.LogTime.Should().Be(e1.LogTime);
                        reader.Current.FileName.Should().Be(e1.FileName);
                        reader.Current.LineNumber.Should().Be(e1.LineNumber);
                        reader.Current.Exception.ExceptionTypeAssemblyQualifiedName.Should().Be(e1.Exception.ExceptionTypeAssemblyQualifiedName);
                        reader.Current.Exception.ToString().Should().Be(e1.Exception.ToString());

                        reader.MoveNext().Should().BeTrue();
                        reader.CurrentMulticast.GrandOutputId.Should().Be("GOId");
                        reader.CurrentMulticast.MonitorId.Should().Be("3712");
                        reader.CurrentMulticast.PreviousEntryType.Should().Be(LogEntryType.Line);
                        reader.CurrentMulticast.PreviousLogTime.Should().Be(prevLog);
                        reader.Current.Text.Should().Be(e2.Text);
                        reader.Current.LogTime.Should().Be(e2.LogTime);
                        reader.Current.FileName.Should().BeNull();
                        reader.Current.LineNumber.Should().Be(0, "Since no file name is set, line number is 0.");
                        reader.Current.Exception.ExceptionTypeAssemblyQualifiedName.Should().Be(e2.Exception.ExceptionTypeAssemblyQualifiedName);
                        reader.Current.Exception.ToString().Should().Be(e2.Exception.ToString());

                        reader.MoveNext().Should().BeFalse();
                        reader.BadEndOfFileMarker.Should().BeFalse();
                    }
                }
        }
예제 #12
0
        public void object_pool_work()
        {
            using (var mem = new MemoryStream())
            {
                var sA = new String('A', 100);
                var sB = new String('B', 100);
                using (var w = new CKBinaryWriter(mem, Encoding.UTF8, true))
                {
                    var pool = new CKBinaryWriter.ObjectPool <string>(w, StringComparer.InvariantCultureIgnoreCase);

                    var p = mem.Position;
                    p.Should().Be(0);

                    pool.MustWrite(sA).Should().BeTrue();
                    w.Write(sA);
                    pool.MustWrite(sB).Should().BeTrue();
                    w.Write(sB);

                    var delta = mem.Position - p;
                    p = mem.Position;
                    delta.Should().Be(1 + 1 + sA.Length + 1 + 1 + sB.Length, "Marker byte + small length + UTF8 ascii string");

                    for (int i = 0; i < 50; ++i)
                    {
                        pool.MustWrite(sA).Should().BeFalse();
                        pool.MustWrite(sB).Should().BeFalse();
                        pool.MustWrite(sA.ToLowerInvariant()).Should().BeFalse();
                        pool.MustWrite(sB.ToLowerInvariant()).Should().BeFalse();
                    }
                    delta = mem.Position - p;
                    delta.Should().Be(50 * 4 * (1 + 1), "Marker byte + NonNegativeSmallInt32 that is actuall one byte...");
                }
                mem.Position = 0;
                using (var r = new CKBinaryReader(mem, Encoding.UTF8, true))
                {
                    var    pool = new CKBinaryReader.ObjectPool <string>(r);
                    string rA   = pool.TryRead(out rA).SetReadResult(r.ReadString());
                    rA.Should().Be(sA);
                    string rB = pool.Read((state, reader) => reader.ReadString());
                    rB.Should().Be(sB);
                    for (int i = 0; i < 50; ++i)
                    {
                        pool.TryRead(out var rA2).Success.Should().BeTrue();
                        rA2.Should().Be(rA);
                        pool.Read((state, reader) => reader.ReadString()).Should().Be(rB);
                        pool.Read((state, reader) => reader.ReadString()).Should().Be(rA);
                        pool.Read((state, reader) => reader.ReadString()).Should().Be(rB);
                    }
                }
            }
        }
예제 #13
0
        public void LogEntryReadWrite()
        {
            var exInner = new CKExceptionData("message", "typeof(exception)", "assemblyQualifiedName", "stackTrace", null, "fileName", "fusionLog", null, null);
            var ex2     = new CKExceptionData("message2", "typeof(exception2)", "assemblyQualifiedName2", "stackTrace2", exInner, "fileName2", "fusionLog2", null, null);
            var exL     = new CKExceptionData("loader-message", "typeof(loader-exception)", "loader-assemblyQualifiedName", "loader-stackTrace", null, "loader-fileName", "loader-fusionLog", null, null);
            var exAgg   = new CKExceptionData("agg-message", "typeof(agg-exception)", "agg-assemblyQualifiedName", "agg-stackTrace", ex2, "fileName", "fusionLog", null, new[] { ex2, exL });

            var       prevLog = DateTimeStamp.UtcNow;
            ILogEntry e1      = LogEntry.CreateLog("Text1", new DateTimeStamp(DateTime.UtcNow, 42), LogLevel.Info, "c:\\test.cs", 3712, ActivityMonitor.Tags.CreateDependentActivity, exAgg);
            ILogEntry e2      = LogEntry.CreateMulticastLog(Guid.Empty, LogEntryType.Line, prevLog, 5, "Text2", DateTimeStamp.UtcNow, LogLevel.Fatal, null, 3712, ActivityMonitor.Tags.CreateDependentActivity, exAgg);

            using (var mem = new MemoryStream())
                using (var w = new CKBinaryWriter(mem))
                {
                    w.Write(LogReader.CurrentStreamVersion);
                    e1.WriteLogEntry(w);
                    e2.WriteLogEntry(w);
                    w.Write((byte)0);
                    w.Flush();

                    byte[] versionBytes = new byte[4];
                    mem.Position = 0;
                    mem.Read(versionBytes, 0, 4);
                    Assert.That(BitConverter.ToInt32(versionBytes, 0), Is.EqualTo(LogReader.CurrentStreamVersion));

                    using (var reader = new LogReader(mem, LogReader.CurrentStreamVersion, 4))
                    {
                        Assert.That(reader.MoveNext());
                        Assert.That(reader.Current.Text, Is.EqualTo(e1.Text));
                        Assert.That(reader.Current.LogLevel, Is.EqualTo(e1.LogLevel));
                        Assert.That(reader.Current.LogTime, Is.EqualTo(e1.LogTime));
                        Assert.That(reader.Current.FileName, Is.EqualTo(e1.FileName));
                        Assert.That(reader.Current.LineNumber, Is.EqualTo(e1.LineNumber));
                        Assert.That(reader.Current.Exception.ExceptionTypeAssemblyQualifiedName, Is.EqualTo(e1.Exception.ExceptionTypeAssemblyQualifiedName));
                        Assert.That(reader.Current.Exception.ToString(), Is.EqualTo(e1.Exception.ToString()));

                        Assert.That(reader.MoveNext());
                        Assert.That(reader.CurrentMulticast.PreviousEntryType, Is.EqualTo(LogEntryType.Line));
                        Assert.That(reader.CurrentMulticast.PreviousLogTime, Is.EqualTo(prevLog));
                        Assert.That(reader.Current.Text, Is.EqualTo(e2.Text));
                        Assert.That(reader.Current.LogTime, Is.EqualTo(e2.LogTime));
                        Assert.That(reader.Current.FileName, Is.Null);
                        Assert.That(reader.Current.LineNumber, Is.EqualTo(0), "Since no file name is set, line number is 0.");
                        Assert.That(reader.Current.Exception.ExceptionTypeAssemblyQualifiedName, Is.EqualTo(e2.Exception.ExceptionTypeAssemblyQualifiedName));
                        Assert.That(reader.Current.Exception.ToString(), Is.EqualTo(e2.Exception.ToString()));

                        Assert.That(reader.MoveNext(), Is.False);
                        Assert.That(reader.BadEndOfFileMarker, Is.False);
                    }
                }
        }
예제 #14
0
 static void WriteMulticastFooter(CKBinaryWriter w, Guid monitorId, LogEntryType previousEntryType, DateTimeStamp previousStamp, int depth)
 {
     w.Write(monitorId.ToByteArray());
     w.WriteNonNegativeSmallInt32(depth);
     if (previousStamp.IsKnown)
     {
         w.Write(previousStamp.TimeUtc.ToBinary());
         if (previousStamp.Uniquifier != 0)
         {
             w.Write(previousStamp.Uniquifier);
         }
         w.Write((byte)previousEntryType);
     }
 }
예제 #15
0
 static PackageDB CloneBySerialization(PackageDB db)
 {
     using (var m = new MemoryStream())
     {
         using (var w = new CKBinaryWriter(m, Encoding.UTF8, true))
         {
             db.Write(w);
         }
         m.Position = 0;
         using (var r = new CKBinaryReader(m, Encoding.UTF8, true))
         {
             return(new PackageDB(r));
         }
     }
 }
예제 #16
0
        static void DoWriteLog(CKBinaryWriter w, StreamLogType t, LogLevel level, DateTimeStamp logTime, string text, CKTrait tags, CKExceptionData?ex, string?fileName, int lineNumber)
        {
            if (tags != null && !tags.IsEmpty)
            {
                t |= StreamLogType.HasTags;
            }
            if (ex != null)
            {
                t |= StreamLogType.HasException;
                if (text == ex.Message)
                {
                    t |= StreamLogType.IsTextTheExceptionMessage;
                }
            }
            if (fileName != null)
            {
                t |= StreamLogType.HasFileName;
            }
            if (logTime.Uniquifier != 0)
            {
                t |= StreamLogType.HasUniquifier;
            }

            WriteLogTypeAndLevel(w, t, level);
            w.Write(logTime.TimeUtc.ToBinary());
            if (logTime.Uniquifier != 0)
            {
                w.Write(logTime.Uniquifier);
            }
            if ((t & StreamLogType.HasTags) != 0)
            {
                w.Write(tags !.ToString());
            }
            if ((t & StreamLogType.HasFileName) != 0)
            {
                Debug.Assert(fileName != null);
                w.Write(fileName);
                w.WriteNonNegativeSmallInt32(lineNumber);
            }
            if ((t & StreamLogType.HasException) != 0)
            {
                ex !.Write(w);
            }
            if ((t & StreamLogType.IsTextTheExceptionMessage) == 0)
            {
                w.Write(text);
            }
        }
예제 #17
0
        /// <summary>
        /// Binary writes a multicast closing entry.
        /// </summary>
        /// <param name="w">Binary writer to use.</param>
        /// <param name="grandOutputId">Identifier of the GrandOutput.</param>
        /// <param name="monitorId">Identifier of the monitor.</param>
        /// <param name="previousEntryType">Log type of the previous entry in the monitor..</param>
        /// <param name="previousLogTime">Time stamp of the previous entry in the monitor.</param>
        /// <param name="depth">Depth of the group (number of opened groups above).</param>
        /// <param name="level">Log level of the log entry.</param>
        /// <param name="closeTime">Time stamp of the group closing.</param>
        /// <param name="conclusions">Group conclusions.</param>
        static public void WriteCloseGroup(CKBinaryWriter w,
                                           string grandOutputId,
                                           string monitorId,
                                           LogEntryType previousEntryType,
                                           DateTimeStamp previousLogTime,
                                           int depth,
                                           LogLevel level,
                                           DateTimeStamp closeTime,
                                           IReadOnlyList <ActivityLogGroupConclusion>?conclusions)
        {
            Throw.CheckNotNullArgument(w);
            StreamLogType type = StreamLogType.TypeGroupClosed | StreamLogType.IsMultiCast;

            type = UpdateTypeWithPrevious(type, previousEntryType, ref previousLogTime);
            DoWriteCloseGroup(w, type, level, closeTime, conclusions);
            WriteMulticastFooter(w, grandOutputId, monitorId, previousEntryType, previousLogTime, depth);
        }
예제 #18
0
 public override void WriteLogEntry(CKBinaryWriter w)
 {
     Debug.Assert(Text != null, "Only LE(MC)CloseGroup has a null Text.");
     LogEntry.WriteLog(w,
                       _grandOutputId,
                       _monitorId,
                       _previousEntryType,
                       _previousLogTime,
                       _depth,
                       true,
                       LogLevel,
                       LogTime,
                       Text,
                       Tags,
                       Exception,
                       FileName,
                       LineNumber);
 }
예제 #19
0
 static int ReadWrite(Action <ICKBinaryWriter> writer, Action <ICKBinaryReader> reader = null)
 {
     using (var mem = new MemoryStream())
     {
         using (var w = new CKBinaryWriter(mem, Encoding.UTF8, true))
         {
             writer(w);
         }
         int pos = (int)mem.Position;
         if (reader != null)
         {
             mem.Position = 0;
             using (var r = new CKBinaryReader(mem, Encoding.UTF8, true))
             {
                 reader(r);
             }
             mem.Position.Should().Be(pos, $"Written {pos} bytes should be the same as read bytes count but found {mem.Position} bytes.");
         }
         return(pos);
     }
 }
예제 #20
0
        /// <summary>
        /// Binary writes a multicast log entry.
        /// </summary>
        /// <param name="w">Binary writer to use.</param>
        /// <param name="grandOutputId">Identifier of the GrandOutput.</param>
        /// <param name="monitorId">Identifier of the monitor.</param>
        /// <param name="previousEntryType">Log type of the previous entry in the monitor..</param>
        /// <param name="previousLogTime">Time stamp of the previous entry in the monitor.</param>
        /// <param name="depth">Depth of the line (number of opened groups above).</param>
        /// <param name="isOpenGroup">True if this the opening of a group. False for a line.</param>
        /// <param name="text">Text of the log entry.</param>
        /// <param name="level">Log level of the log entry.</param>
        /// <param name="logTime">Time stamp of the log entry.</param>
        /// <param name="tags">Tags of the log entry</param>
        /// <param name="ex">Exception of the log entry.</param>
        /// <param name="fileName">Source file name of the log entry</param>
        /// <param name="lineNumber">Source line number of the log entry</param>
        static public void WriteLog(CKBinaryWriter w,
                                    string grandOutputId,
                                    string monitorId,
                                    LogEntryType previousEntryType,
                                    DateTimeStamp previousLogTime,
                                    int depth,
                                    bool isOpenGroup,
                                    LogLevel level,
                                    DateTimeStamp logTime,
                                    string text,
                                    CKTrait tags,
                                    CKExceptionData?ex,
                                    string?fileName,
                                    int lineNumber)
        {
            Throw.CheckNotNullArgument(w);
            StreamLogType type = StreamLogType.IsMultiCast | (isOpenGroup ? StreamLogType.TypeOpenGroup : StreamLogType.TypeLine);

            type = UpdateTypeWithPrevious(type, previousEntryType, ref previousLogTime);
            DoWriteLog(w, type, level, logTime, text, tags, ex, fileName, lineNumber);
            WriteMulticastFooter(w, grandOutputId, monitorId, previousEntryType, previousLogTime, depth);
        }
예제 #21
0
        SignResult DoSign(Action <Stream>?payloadWriter, ReadOnlySpan <byte> payload)
        {
            using var m = new MemoryStream(4096);
            using var w = new CKBinaryWriter(m);

            w.Write((byte)0);   // Version.
            KeyUsageInfo keyInfo;
            int          headerLength, payloadLength, signatureLength;

            if (_key == null)
            {
                keyInfo = _local.GetNoKeyUsageInfo(null);
                WriteStdClaims(w, keyInfo);
                WritePayload(payloadWriter, payload, m, w, out headerLength, out payloadLength);
                signatureLength = 0;
            }
            else
            {
                var(key, usage) = _key.GetActualKeyObject(true, null);
                if (key == null)
                {
                    return(new SignResult(usage, ReadOnlyMemory <byte> .Empty, 0, 0, 0));
                }
                keyInfo = usage;
                WriteStdClaims(w, usage);
                WritePayload(payloadWriter, payload, m, w, out headerLength, out payloadLength);
                signatureLength = _algo.SignatureByteLength;
                Span <byte> signature = signatureLength <= 256
                                        ? stackalloc byte[signatureLength]
                                        : new byte[signatureLength];
                _algo.Sign(key, m.GetBuffer().AsSpan(0, (int)m.Position), signature);
                m.Write(signature);
            }

            var result = m.GetBuffer().AsMemory(0, (int)m.Position);

            return(new SignResult(keyInfo, result, headerLength, payloadLength, signatureLength));
예제 #22
0
        public void object_pool_with_write_marker()
        {
            using (var mem = new MemoryStream())
            {
                // Same string but in two different instances: the PureObjectRefEqualityComparer
                // does its job.
                var o1 = new String('B', 100);
                var o2 = new String('B', 100);
                using (var w = new CKBinaryWriter(mem, Encoding.UTF8, true))
                {
                    var pool = new CKBinaryWriter.ObjectPool <string>(w, PureObjectRefEqualityComparer <string> .Default);
                    pool.MustWrite(o1, 3).Should().BeTrue();
                    w.Write(o1);
                    pool.MustWrite(o2, 255).Should().BeTrue();
                    w.Write(o2);
                }
                mem.Position = 0;
                using (var r = new CKBinaryReader(mem, Encoding.UTF8, true))
                {
                    var pool   = new CKBinaryReader.ObjectPool <string>(r);
                    var state1 = pool.TryRead(out var s1);
                    s1.Should().BeNull();
                    state1.Success.Should().BeFalse();
                    state1.WriteMarker.Should().Be(3);
                    s1 = state1.SetReadResult(r.ReadString());

                    var state2 = pool.TryRead(out var s2);
                    s2.Should().BeNull();
                    state2.Success.Should().BeFalse();
                    state2.WriteMarker.Should().Be(255);
                    s2 = state2.SetReadResult(r.ReadString());

                    s1.Should().Be(o1).And.Be(o2);
                    s2.Should().Be(o1).And.Be(o2);
                }
            }
        }
예제 #23
0
 public virtual void WriteLogEntry(CKBinaryWriter w)
 {
     LogEntry.WriteCloseGroup(w, _level, _time, _conclusions);
 }
예제 #24
0
 /// <summary>
 /// Writes this cache into a binary stream.
 /// </summary>
 /// <param name="w">The writer to use.</param>
 public void Write(CKBinaryWriter w)
 {
     _db.Write(w);
 }
예제 #25
0
 public override void WriteLogEntry(CKBinaryWriter w)
 {
     LogEntry.WriteLog(w, _monitorId, _previousEntryType, _previousLogTime, _depth, true, LogLevel, LogTime, Text, Tags, Exception, FileName, LineNumber);
 }
예제 #26
0
 public TcpHandler(TcpHandlerConfiguration configuration)
 {
     _configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
     _memoryStream  = new MemoryStream();
     _binaryWriter  = new CKBinaryWriter(_memoryStream, Encoding.UTF8, true);
 }
예제 #27
0
 public override void WriteLogEntry(CKBinaryWriter w)
 {
     LogEntry.WriteCloseGroup(w, _monitorId, _previousEntryType, _previousLogTime, _depth, LogLevel, LogTime, Conclusions);
 }
예제 #28
0
 static void WritePayload(Action <Stream>?payloadWriter, ReadOnlySpan <byte> payload, MemoryStream m, CKBinaryWriter w, out int headerLength, out int payloadLength)
 {
     w.Flush();
     headerLength = (int)m.Position;
     if (payloadWriter != null)
     {
         payloadWriter(m);
     }
     else
     {
         m.Write(payload);
     }
     payloadLength = (int)m.Position - headerLength;
 }
 /// <summary>
 /// Initializes a new <see cref="SimplePipeSenderActivityMonitorClient"/>.
 /// </summary>
 /// <param name="pipeHandlerName">The name of the server pipe.</param>
 public SimplePipeSenderActivityMonitorClient(string pipeHandlerName)
 {
     _client = new AnonymousPipeClientStream(PipeDirection.Out, pipeHandlerName);
     _writer = new CKBinaryWriter(_client);
     _writer.Write(LogReader.CurrentStreamVersion);
 }
예제 #30
0
 public void reading_and_writing_CKExceptionData_with_BinaryWriter_and_BinaryReader()
 {
     var dataE0 = CKExceptionData.CreateFrom( ThrowAggregatedException() );
     var dataE1 = CKExceptionData.CreateFrom( ThrowSimpleException( "Test Message" ) );
     var dataE2 = CKExceptionData.CreateFrom( ThrowLoaderException() );
     var dataE3 = CKExceptionData.CreateFrom( ThrowExceptionWithInner() );
     var dataE4 = CKExceptionData.CreateFrom( ThrowTwoInnerExceptions() );
     using( var mem = new MemoryStream() )
     {
         CKBinaryWriter w = new CKBinaryWriter( mem );
         dataE0.Write( w );
         dataE1.Write( w );
         dataE2.Write( w );
         dataE3.Write( w );
         dataE4.Write( w );
         mem.Position = 0;
         var r = new CKBinaryReader( mem );
         var data0 = new CKExceptionData( r, StringAndStringBuilderExtension.IsCRLF );
         Assert.AreEqual( data0.ToString(), dataE0.ToString() );
         var data1 = new CKExceptionData( r, StringAndStringBuilderExtension.IsCRLF );
         Assert.AreEqual( data1.ToString(), dataE1.ToString() );
         var data2 = new CKExceptionData( r, StringAndStringBuilderExtension.IsCRLF );
         Assert.AreEqual( data2.ToString(), dataE2.ToString() );
         var data3 = new CKExceptionData( r, StringAndStringBuilderExtension.IsCRLF );
         Assert.AreEqual( data3.ToString(), dataE3.ToString() );
         var data4 = new CKExceptionData( r, StringAndStringBuilderExtension.IsCRLF );
         Assert.AreEqual( data4.ToString(), dataE4.ToString() );
     }
 }
예제 #31
0
 public virtual void WriteLogEntry(CKBinaryWriter w)
 {
     LogEntry.WriteLog(w, true, _level, _time, _text, _tags, _ex, _fileName, _lineNumber);
 }