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 ); } } }
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); } }
/// <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); }
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 ); }
/// <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 ); }
/// <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; }
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 ); } } }
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 ); } }
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 ); }