More .NET components

Communication logging and replay

Session recording and replaying #

TerminalControl and VirtualTerminal classes have a useful and unique feature - session recording. This makes it possible to save a complete SSH shell session into a file and replay it later using our ANSI Player application.

CSharp

// start recording the session to a file
terminal.Recorder = System.IO.File.AppendText("recording.ans");

// the resulting file can be replayed by the AnsiPlayer sample

VisualBasic

' start recording the session to a file
terminal.Recorder = System.IO.File.AppendText("recording.ans")

' the resulting file can be replayed by the AnsiPlayer sample

Tip: ANSI Player sample comes with source code and can be easily embedded into your application.

Logging communication using LogWriter #

Communication logs are very useful when troubleshooting issues at the SSH protocol or communication level. Whenever you run into a problem, a log makes it possible to see what is going on.

To start logging, just add the following line into your code (just after creating the Ssh object):

CSharp

// create an SSH object
var ssh = new Rebex.Net.Ssh();

// start logging to a file
ssh.LogWriter = new Rebex.FileLogWriter(@"C:\MyData\log.txt");

VisualBasic

' create an SSH object
Dim ssh = New Rebex.Net.Ssh()

' start logging to a file
ssh.LogWriter = New Rebex.FileLogWriter("C:\MyData\log.txt")

A sample log file:

2013-03-13 11:06:23.188 Opening log file.
2013-03-13 11:06:23.189 Using FileLogWriter version 2.0.2700.0.
2013-03-13 11:06:23.193 INFO Ssh(4)[34] Info: Connecting to testserver.local:22 using Ssh 1.0.2700.0.
2013-03-13 11:06:23.203 INFO Ssh(4)[34] SSH: Negotiation started.
2013-03-13 11:06:23.243 INFO Ssh(4)[34] SSH: Negotiation finished.
2013-03-13 11:06:23.243 INFO Ssh(4)[34] Info: Server: SSH-2.0-OpenSSH_6.0p1 Debian-3
2013-03-13 11:06:23.243 INFO Ssh(4)[34] Info: Fingerprint: e2:29:05:cd:7a:59:ee:03:fa:03:f5:72:61:77:e3:1c
2013-03-13 11:06:23.244 INFO Ssh(4)[34] Info: Cipher info: SSH 2.0, DiffieHellmanGroupExchangeSHA256, DSS, aes256-ctr/aes256-ctr

Log verbosity levels #

There are several levels of log verbosity:

  • Error - logs error messages only
  • Info - default level; logs important informative messages as well
  • Debug - logs all messages useful for debugging purposes
  • Verbose - very detailed log for advanced analysis; logs content of communication packets
    Warning: At the Verbose level, user credentials are written to the log as well.

To specify a log level, just pass an argument to FileLogWriter's constructor:

CSharp

// start logging to a file at debug level
ssh.LogWriter = new Rebex.FileLogWriter(
    @"C:\MyData\log.txt", Rebex.LogLevel.Debug);

VisualBasic

' start logging to a file at debug level
ssh.LogWriter = New Rebex.FileLogWriter("C:\MyData\log.txt", Rebex.LogLevel.Debug)

Built-in log writers #

Text file

To log into a text file, use FileLogWriter:

CSharp

// start logging to a file at debug level
ssh.LogWriter = new Rebex.FileLogWriter(
    @"C:\MyData\log.txt", Rebex.LogLevel.Debug);

VisualBasic

' start logging to a file at debug level
ssh.LogWriter = New Rebex.FileLogWriter("C:\MyData\log.txt", Rebex.LogLevel.Debug)

.NET trace listener

To log to .NET Trace Listeners, use TraceLogWriter:

CSharp

// start logging to subscribed trace listeners
ssh.LogWriter = new Rebex.TraceLogWriter(Rebex.LogLevel.Debug);

VisualBasic

' start logging to subscribed trace listeners
ssh.LogWriter = New Rebex.TraceLogWriter(Rebex.LogLevel.Debug)

Standard output stream

To log to the standard output stream, use ConsoleLogWriter:

CSharp

// start logging to the standard output stream at debug level
ssh.LogWriter = new Rebex.ConsoleLogWriter(Rebex.LogLevel.Debug);

VisualBasic

' start logging to the standard output stream at debug level
ssh.LogWriter = New Rebex.ConsoleLogWriter(Rebex.LogLevel.Debug)

Logging to multiple log writers

To log to more log writers at the same time, use TeeLogWriter:

CSharp

var consoleLogWriter = new Rebex.ConsoleLogWriter(Rebex.LogLevel.Debug);
var fileLogWriter = new Rebex.FileLogWriter(@"C:\MyData\log.txt", Rebex.LogLevel.Info);

// start logging to both console and file log writers
ssh.LogWriter = new Rebex.TeeLogWriter(consoleLogWriter, fileLogWriter);

VisualBasic

Dim consoleLogWriter = New Rebex.ConsoleLogWriter(Rebex.LogLevel.Debug)
Dim fileLogWriter = New Rebex.FileLogWriter("C:\MyData\log.txt", Rebex.LogLevel.Debug)

' start logging to both console and file log writers
ssh.LogWriter = New Rebex.TeeLogWriter(consoleLogWriter, fileLogWriter)

Writing custom log writers #

To implement your own LogWriter, either implement Rebex.ILogWriter interface or simply derive a class from Rebex.LogWriterBase:

CSharp

// Sample log writer that logs to the standard error output stream
public class MyLogWriter : LogWriterBase
{
    protected override void WriteMessage(string message)
    {
        Console.Error.WriteLine("Rebex: {0}", message);
    }
}

VisualBasic

' Sample log writer that logs to the standard error output stream
Public Class MyLogWriter
    Inherits LogWriterBase

    Protected Overrides Sub WriteMessage(message As String)
        Console.Error.WriteLine("Rebex: {0}", message)
    End Sub

End Class