Specializes BinaryWriter to expose helpers.
Inheritance: System.IO.BinaryWriter
Beispiel #1
0
 /// <summary>
 /// Writes this exception data into a <see cref="BinaryWriter"/>.
 /// </summary>
 /// <param name="w">The writer to use. Can not be null.</param>
 /// <param name="writeVersion">False to not write the <see cref="CurrentStreamVersion"/>.</param>
 public void Write(CKBinaryWriter w, bool writeVersion = true)
 {
     if (writeVersion)
     {
         w.Write(CurrentStreamVersion);
     }
     WriteWithoutVersion(w);
 }
Beispiel #2
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 );
     }
 }
Beispiel #3
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 );
                }
            }

        }
Beispiel #4
0
        void WriteWithoutVersion(CKBinaryWriter w)
        {
            if (w == null)
            {
                throw new ArgumentNullException("w");
            }
            w.Write(_message);
            w.Write(_exceptionTypeName);
            w.Write(_exceptionTypeAQName);
            w.WriteNullableString(_stackTrace);
            w.WriteNullableString(_fileName);
            w.WriteNullableString(_detailedInfo);

            if (_aggregatedExceptions != null)
            {
                w.WriteSmallInt32(_aggregatedExceptions.Length);
                foreach (var agg in _aggregatedExceptions)
                {
                    agg.WriteWithoutVersion(w);
                }
            }
            else
            {
                if (_innerException != null)
                {
                    w.WriteSmallInt32(0);
                    _innerException.WriteWithoutVersion(w);
                }
                else
                {
                    w.WriteSmallInt32(-1);
                }
            }

            if (_loaderExceptions != null)
            {
                w.WriteNonNegativeSmallInt32(_loaderExceptions.Length);
                foreach (var ld in _loaderExceptions)
                {
                    ld.WriteWithoutVersion(w);
                }
            }
            else
            {
                w.WriteNonNegativeSmallInt32(0);
            }
        }
Beispiel #5
0
 public override void WriteLogEntry( CKBinaryWriter w )
 {
     LogEntry.WriteCloseGroup( w, _monitorId, _previousEntryType, _previousLogTime, _depth, LogLevel, LogTime, Conclusions );
 }
 /// <summary>
 /// Called when the current file is closed.
 /// </summary>
 protected override void CloseCurrentFile()
 {
     _writer.Write( (byte)0 );
     base.CloseCurrentFile();
     _writer.Dispose();
     _writer = null;
 }
Beispiel #7
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 )
            {
                w.Write( fileName );
                w.WriteNonNegativeSmallInt32( lineNumber );
            }
            if( (t & StreamLogType.HasException) != 0 ) ex.Write( w );
            if( (t & StreamLogType.IsTextTheExceptionMessage) == 0 ) w.Write( text );
        }
Beispiel #8
0
 public override void WriteLogEntry( CKBinaryWriter w )
 {
     LogEntry.WriteLog( w, _monitorId, _previousEntryType, _previousLogTime, _depth, false, LogLevel, LogTime, Text, Tags, Exception, FileName, LineNumber );
 }
Beispiel #9
0
        void WriteWithoutVersion( CKBinaryWriter w )
        {
            if( w == null ) throw new ArgumentNullException( "w" );
            w.Write( _message );
            w.Write( _exceptionTypeName );
            w.Write( _exceptionTypeAQName );
            w.WriteNullableString( _stackTrace );
            w.WriteNullableString( _fileName );
            w.WriteNullableString( _detailedInfo );

            if( _aggregatedExceptions != null )
            {
                w.WriteSmallInt32( _aggregatedExceptions.Length );
                foreach( var agg in _aggregatedExceptions ) agg.WriteWithoutVersion( w );
            }
            else
            {
                if( _innerException != null )
                {
                    w.WriteSmallInt32( 0 );
                    _innerException.WriteWithoutVersion( w );
                }
                else w.WriteSmallInt32( -1 );
            }

            if( _loaderExceptions != null )
            {
                w.WriteNonNegativeSmallInt32( _loaderExceptions.Length );
                foreach( var ld in _loaderExceptions ) ld.WriteWithoutVersion( w );
            }
            else w.WriteNonNegativeSmallInt32( 0 );
        }
Beispiel #10
0
 public virtual void WriteLogEntry( CKBinaryWriter w )
 {
     LogEntry.WriteCloseGroup( w, _level, _time, _conclusions );
 }
Beispiel #11
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 );
 }
Beispiel #12
0
 /// <summary>
 /// Writes this exception data into a <see cref="BinaryWriter"/>.
 /// </summary>
 /// <param name="w">The writer to use. Can not be null.</param>
 /// <param name="writeVersion">False to not write the <see cref="CurrentStreamVersion"/>.</param>
 public void Write( CKBinaryWriter w, bool writeVersion = true )
 {
     if( writeVersion ) w.Write( CurrentStreamVersion );
     WriteWithoutVersion( w );
 }
Beispiel #13
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 );
 }
 /// <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;
 }
Beispiel #15
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 )
     {
         w.WriteNonNegativeSmallInt32( conclusions.Count );
         foreach( ActivityLogGroupConclusion c in conclusions )
         {
             w.Write( c.Tag.ToString() );
             w.Write( c.Text );
         }
     }
 }
Beispiel #16
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 );
     }
 }
Beispiel #17
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 );
 }
Beispiel #18
0
 public virtual void WriteLogEntry( CKBinaryWriter w )
 {
     LogEntry.WriteLog( w, true, _level, _time, _text, _tags, _ex, _fileName, _lineNumber );
 }
Beispiel #19
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 );
 }