CK.Monitoring.MonitorTextFileOutput.Write C# (CSharp) Method

Write() public method

Writes a log entry (that can actually be a IMulticastLogEntry).
public Write ( IMulticastLogEntry e ) : void
e IMulticastLogEntry The log entry.
return void
        public void Write( IMulticastLogEntry e )
        {
            Debug.Assert( DateTimeStamp.MaxValue.ToString().Length == 32,
                "DateTimeStamp FileNameUniqueTimeUtcFormat and the uniquifier: max => 32 characters long." );
            Debug.Assert( Guid.NewGuid().ToString().Length == 36,
                "Guid => 18 characters long." );

            BeforeWrite();
            _builder.Append( ' ', _nameLen + 32 );
            _builder.Append( "| ", e.Text != null ? e.GroupDepth : e.GroupDepth - 1 );
            string prefix = _builder.ToString();
            _builder.Clear();
            // MonitorId (if needed) on one line.
            if( _currentMonitorId == e.MonitorId )
            {
                _builder.Append( ' ', _nameLen + 1 );
            }
            else
            {
                _currentMonitorId = e.MonitorId;
                if( !_monitorNames.TryGetValue( _currentMonitorId, out _currentMonitorName ) )
                {
                    _currentMonitorName = _monitorNames.Count.ToString( "X" + _nameLen );
                    int len = _currentMonitorName.Length;
                    if( _nameLen < len )
                    {
                        prefix = " " + prefix;
                        _nameLen = len;
                    }
                    _monitorNames.Add( _currentMonitorId, _currentMonitorName );
                    _builder.Append( _currentMonitorName )
                            .Append( "~~~~" )
                            .Append( ' ', 28 )
                            .Append( "~~ Monitor: " )
                            .AppendLine( _currentMonitorId.ToString() );
                    _builder.Append( ' ', _nameLen + 1 );
                }
                else
                {
                    _builder.Append( _currentMonitorName ).Append( '~' );
                    _builder.Append( ' ', _nameLen - _currentMonitorName.Length );
                }
            }
            // Log time prefixes the first line only.
            TimeSpan delta = e.LogTime.TimeUtc - _lastLogTime;
            if( delta >= TimeSpan.FromMinutes(1) )
            {
                string logTime = e.LogTime.TimeUtc.ToString( FileUtil.FileNameUniqueTimeUtcFormat );
                _builder.Append( ' ' );
                _builder.Append( logTime );
                _builder.Append( ' ' );
                _lastLogTime = e.LogTime.TimeUtc;
            }
            else
            {
                _builder.Append( ' ', 17 );
                _builder.Append( '+' );
                _builder.Append( delta.ToString( @"ss\.fffffff" ) );
                _builder.Append( ' ' );
            }

            // Level is one char.
            char level;
            switch( e.LogLevel & LogLevel.Mask )
            {
                case LogLevel.Trace: level = ' '; break;
                case LogLevel.Info: level = 'i'; break;
                case LogLevel.Warn: level = 'W'; break;
                case LogLevel.Error: level = 'E'; break;
                default: level = 'F'; break;
            }
            _builder.Append( level );
            _builder.Append( ' ' );
            _builder.Append( "| ", e.Text != null ? e.GroupDepth : e.GroupDepth - 1 );

            if( e.Text != null )
            {
                if( e.LogType == LogEntryType.OpenGroup ) _builder.Append( "> " );
                prefix += "  ";
                _builder.AppendMultiLine( prefix, e.Text, false ).AppendLine();
                if( e.Exception != null )
                {
                    e.Exception.ToStringBuilder( _builder, prefix );
                }
            }
            else 
            {
                Debug.Assert( e.Conclusions != null );
                _builder.Append( "< " );
                if( e.Conclusions.Count > 0 )
                {
                    _builder.Append( " | " ).Append( e.Conclusions.Count ).Append( " conclusion" );
                    if( e.Conclusions.Count > 1 ) _builder.Append( 's' );
                    _builder.Append( ':' ).AppendLine();
                    prefix += "   | ";
                    foreach( var c in e.Conclusions )
                    {
                        _builder.AppendMultiLine( prefix, c.Text, true ).AppendLine();
                    }
                }
                else 
                {
                    _builder.AppendLine();
                }
            }
            _writer.Write( _builder.ToString() );
            AfterWrite();
            _builder.Clear();
        }