Ejemplo n.º 1
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());
            }
        }
Ejemplo n.º 2
0
 static void ReadMulticastFooter(int streamVersion, CKBinaryReader r, StreamLogType t, out string gId, out string mId, out int depth, out LogEntryType prevType, out DateTimeStamp prevTime)
 {
     if (streamVersion == 9)
     {
         gId   = r.ReadString();
         mId   = r.ReadString();
         depth = r.ReadNonNegativeSmallInt32();
         Throw.CheckData(mId == GrandOutput.ExternalLogMonitorUniqueId || Base64UrlHelper.IsBase64UrlCharacters(mId));
     }
     else
     {
         gId = GrandOutput.UnknownGrandOutputId;
         Debug.Assert(Guid.Empty.ToByteArray().Length == 16);
         mId   = streamVersion < 8 ? new Guid(r.ReadBytes(16)).ToString() : r.ReadString();
         depth = streamVersion < 6 ? r.ReadInt32() : r.ReadNonNegativeSmallInt32();
         if (streamVersion >= 8)
         {
             Throw.CheckData(mId == GrandOutput.ExternalLogMonitorUniqueId || Base64UrlHelper.IsBase64UrlCharacters(mId));
         }
     }
     Throw.CheckData(gId == GrandOutput.UnknownGrandOutputId || Base64UrlHelper.IsBase64UrlCharacters(gId));
     Throw.CheckData(depth >= 0);
     prevType = LogEntryType.None;
     prevTime = DateTimeStamp.Unknown;
     if ((t & StreamLogType.IsPreviousKnown) != 0)
     {
         prevTime = new DateTimeStamp(DateTime.FromBinary(r.ReadInt64()), (t & StreamLogType.IsPreviousKnownHasUniquifier) != 0 ? r.ReadByte() : (Byte)0);
         prevType = (LogEntryType)r.ReadByte();
     }
 }
Ejemplo n.º 3
0
        static ILogEntry ReadGroupClosed(int streamVersion, CKBinaryReader r, StreamLogType t, LogLevel logLevel)
        {
            DateTimeStamp time = new DateTimeStamp(DateTime.FromBinary(r.ReadInt64()), (t & StreamLogType.HasUniquifier) != 0 ? r.ReadByte() : (Byte)0);

            ActivityLogGroupConclusion[] conclusions = Util.Array.Empty <ActivityLogGroupConclusion>();
            if ((t & StreamLogType.HasConclusions) != 0)
            {
                int conclusionsCount = streamVersion < 6 ? r.ReadInt32() : r.ReadNonNegativeSmallInt32();
                conclusions = new ActivityLogGroupConclusion[conclusionsCount];
                for (int i = 0; i < conclusionsCount; i++)
                {
                    CKTrait cTags = ActivityMonitor.Tags.Register(r.ReadString());
                    string  cText = r.ReadString();
                    conclusions[i] = new ActivityLogGroupConclusion(cText, cTags);
                }
            }
            if ((t & StreamLogType.IsMultiCast) == 0)
            {
                return(new LECloseGroup(time, logLevel, conclusions));
            }
            Guid          mId;
            int           depth;
            LogEntryType  prevType;
            DateTimeStamp prevTime;

            ReadMulticastFooter(streamVersion, r, t, out mId, out depth, out prevType, out prevTime);

            return(new LEMCCloseGroup(mId, depth, prevTime, prevType, time, logLevel, conclusions));
        }
Ejemplo n.º 4
0
 public LuceneGloutonHandler(LuceneGloutonHandlerConfiguration configuration)
 {
     _memoryStream      = new MemoryStream();
     _binaryReader      = new CKBinaryReader(_memoryStream, Encoding.UTF8, true);
     _blockingQueue     = new ConcurrentQueue <Action>();
     _indexerDictionary = new Dictionary <string, LuceneIndexer>();
     _configuration     = configuration;
 }
Ejemplo n.º 5
0
 public AlertHandler(AlertHandlerConfiguration alertHandlerConfiguration)
 {
     _memoryStream            = new MemoryStream();
     _binaryReader            = new CKBinaryReader(_memoryStream, Encoding.UTF8, true);
     _alertAlertSenderManager = new AlertSenderManager();
     _alertTableMock          = new AlertTableMock(alertHandlerConfiguration.DatabasePath);
     InitializeAlerts(alertHandlerConfiguration);
 }
Ejemplo n.º 6
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);
                }
            }
        }
Ejemplo n.º 7
0
 /// <summary>
 /// Initializes a new <see cref="LogReader"/> on an uncompressed stream with an explicit version number.
 /// </summary>
 /// <param name="stream">Stream to read logs from.</param>
 /// <param name="streamVersion">Version of the log stream.</param>
 /// <param name="headerLength">Length of the header. This will be substracted to the actual stream position to compute the <see cref="StreamOffset"/>.</param>
 /// <param name="mustClose">
 /// Defaults to true (the stream will be automatically closed).
 /// False to let the stream opened once this reader is disposed, the end of the log data is reached or an error is encountered.
 /// </param>
 public LogReader(Stream stream, int streamVersion, int headerLength, bool mustClose = true)
 {
     if (streamVersion < 5)
     {
         throw new ArgumentException("Must be greater or equal to 5 (the first version).", "streamVersion");
     }
     _stream        = stream;
     _binaryReader  = new CKBinaryReader(stream, Encoding.UTF8, !mustClose);
     _streamVersion = streamVersion;
     _headerLength  = headerLength;
 }
Ejemplo n.º 8
0
        public BinaryGloutonHandler(BinaryGloutonHandlerConfiguration configuration)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }

            _file         = new MonitorBinaryFileOutput(configuration.Path, configuration.MaxCountPerFile, configuration.UseGzipCompression);
            _memoryStream = new MemoryStream();
            _binaryReader = new CKBinaryReader(_memoryStream, Encoding.UTF8, true);

            _configuration = configuration;
        }
Ejemplo n.º 9
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);
                    }
                }
            }
        }
Ejemplo n.º 10
0
 void Close(bool throwError)
 {
     if (_stream != null)
     {
         _current = null;
         _binaryReader.Dispose();
         _stream       = null;
         _binaryReader = null;
     }
     if (throwError)
     {
         throw new InvalidOperationException("Invalid log data.");
     }
 }
Ejemplo n.º 11
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));
         }
     }
 }
        SimpleLogPipeReceiver(IActivityMonitor m, bool interProcess)
        {
            _interProcess = interProcess;
            var inherit = interProcess ? HandleInheritability.Inheritable : HandleInheritability.None;

            _server  = new AnonymousPipeServerStream(PipeDirection.In, inherit);
            _reader  = new CKBinaryReader(_server);
            _monitor = m;
            PipeName = _server.GetClientHandleAsString();
            _thread  = new Thread(Run)
            {
                IsBackground = true
            };
            _thread.Start();
        }
Ejemplo n.º 13
0
 static void ReadMulticastFooter(int streamVersion, CKBinaryReader r, StreamLogType t, out Guid mId, out int depth, out LogEntryType prevType, out DateTimeStamp prevTime)
 {
     Debug.Assert(Guid.Empty.ToByteArray().Length == 16);
     mId   = new Guid(r.ReadBytes(16));
     depth = streamVersion < 6 ? r.ReadInt32() : r.ReadNonNegativeSmallInt32();
     if (depth < 0)
     {
         throw new InvalidDataException();
     }
     prevType = LogEntryType.None;
     prevTime = DateTimeStamp.Unknown;
     if ((t & StreamLogType.IsPreviousKnown) != 0)
     {
         prevTime = new DateTimeStamp(DateTime.FromBinary(r.ReadInt64()), (t & StreamLogType.IsPreviousKnownHasUniquifier) != 0 ? r.ReadByte() : (Byte)0);
         prevType = (LogEntryType)r.ReadByte();
     }
 }
Ejemplo n.º 14
0
 /// <summary>
 /// Initializes a new <see cref="GloutonServerMock"/>.
 /// </summary>
 /// <param name="boundIpAddress">Host address. You can use <see cref="TestHelper.DefaultHost"/>.</param>
 /// <param name="port">Port. You can use <see cref="TestHelper.DefaultPort"/>.</param>
 /// <param name="clientAuthorizationHandler">Authorization Handler. If none are set, <see cref="TestAuthHandler"/> will be used.</param>
 /// <param name="serverCertificate">Server certificate. Can be null.</param>
 /// <param name="userCertificateValidationCallback">User certification call back. Can be null.</param>
 public GloutonServerMock(
     string boundIpAddress,
     int port,
     IAuthorizationHandler clientAuthorizationHandler = null,
     X509Certificate2 serverCertificate = null,
     RemoteCertificateValidationCallback userCertificateValidationCallback = null)
 {
     _controlChannelServer = new ControlChannelServer
                             (
         boundIpAddress,
         port,
         clientAuthorizationHandler ?? new TestAuthHandler(_ => true),
         serverCertificate,
         userCertificateValidationCallback
                             );
     _controlChannelServer.RegisterChannelHandler("GrandOutputEventInfo", HandleGrandOutputEventInfo);
     _memoryStream = new MemoryStream();
     _binaryReader = new CKBinaryReader(_memoryStream, Encoding.UTF8, true);
     ListLog       = new List <ILogEntry>();
 }
Ejemplo n.º 15
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);
     }
 }
Ejemplo n.º 16
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);
                }
            }
        }
Ejemplo n.º 17
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() );
     }
 }
Ejemplo n.º 18
0
        /// <summary>
        /// Reads a <see cref="ILogEntry"/> from the binary reader that can be a <see cref="IMulticastLogEntry"/>.
        /// If the first read byte is 0, read stops and null is returned.
        /// The 0 byte is the "end marker" that <see cref="CKMonWriterClient.Close()"/> write, but this
        /// method can read non zero-terminated streams (it catches an EndOfStreamException when reading the first byte and handles it silently).
        /// This method can throw any type of exception except <see cref="EndOfStreamException"/>
        /// (like <see cref="InvalidDataException"/> for instance) that must be handled by the caller.
        /// </summary>
        /// <param name="r">The binary reader.</param>
        /// <param name="streamVersion">The version of the stream.</param>
        /// <param name="badEndOfFile">True whenever the end of file is the result of an <see cref="EndOfStreamException"/>.</param>
        /// <returns>The log entry or null if a zero byte (end marker) has been found.</returns>
        static public ILogEntry?Read(CKBinaryReader r, int streamVersion, out bool badEndOfFile)
        {
            Throw.CheckNotNullArgument(r);
            badEndOfFile = false;
            StreamLogType t        = StreamLogType.EndOfStream;
            LogLevel      logLevel = LogLevel.None;

            try
            {
                ReadLogTypeAndLevel(r, streamVersion, out t, out logLevel);
            }
            catch (EndOfStreamException)
            {
                badEndOfFile = true;
                // Silently ignores here reading beyond the stream: this
                // kindly handles the lack of terminating 0 byte.
            }
            if (t == StreamLogType.EndOfStream)
            {
                return(null);
            }

            if ((t & StreamLogType.TypeMask) == StreamLogType.TypeGroupClosed)
            {
                return(ReadGroupClosed(streamVersion, r, t, logLevel));
            }
            DateTimeStamp time = new DateTimeStamp(DateTime.FromBinary(r.ReadInt64()), (t & StreamLogType.HasUniquifier) != 0 ? r.ReadByte() : (Byte)0);

            if (time.TimeUtc.Year < 2014 || time.TimeUtc.Year > 3000)
            {
                throw new InvalidDataException("Date year before 2014 or after 3000 are considered invalid.");
            }
            CKTrait         tags       = ActivityMonitor.Tags.Empty;
            string?         fileName   = null;
            int             lineNumber = 0;
            CKExceptionData?ex         = null;
            string?         text       = null;

            if ((t & StreamLogType.HasTags) != 0)
            {
                tags = ActivityMonitor.Tags.Register(r.ReadString());
            }
            if ((t & StreamLogType.HasFileName) != 0)
            {
                fileName   = r.ReadString();
                lineNumber = streamVersion < 6 ? r.ReadInt32() : r.ReadNonNegativeSmallInt32();
                if (lineNumber > 100 * 1000)
                {
                    throw new InvalidDataException("LineNumber greater than 100K is considered invalid.");
                }
            }
            if ((t & StreamLogType.HasException) != 0)
            {
                ex = new CKExceptionData(r);
                if ((t & StreamLogType.IsTextTheExceptionMessage) != 0)
                {
                    text = ex.Message;
                }
            }
            if (text == null)
            {
                text = r.ReadString((t & StreamLogType.IsLFOnly) == 0);
            }

            string        gId;
            string        mId;
            int           depth;
            LogEntryType  prevType;
            DateTimeStamp prevTime;

            if ((t & StreamLogType.TypeMask) == StreamLogType.TypeLine)
            {
                if ((t & StreamLogType.IsMultiCast) == 0)
                {
                    return(new LELog(text, time, fileName, lineNumber, logLevel, tags, ex));
                }
                ReadMulticastFooter(streamVersion, r, t, out gId, out mId, out depth, out prevType, out prevTime);
                return(new LEMCLog(gId, mId, depth, prevTime, prevType, text, time, fileName, lineNumber, logLevel, tags, ex));
            }
            if ((t & StreamLogType.TypeMask) != StreamLogType.TypeOpenGroup)
            {
                throw new InvalidDataException();
            }
            if ((t & StreamLogType.IsMultiCast) == 0)
            {
                return(new LEOpenGroup(text, time, fileName, lineNumber, logLevel, tags, ex));
            }
            ReadMulticastFooter(streamVersion, r, t, out gId, out mId, out depth, out prevType, out prevTime);
            return(new LEMCOpenGroup(gId, mId, depth, prevTime, prevType, text, time, fileName, lineNumber, logLevel, tags, ex));
        }