Communication logging

Logging communication using LogWriter #

Communication logs are very useful when troubleshooting issues at the HTTP protocol on 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 HttpRequestCreator or WebClient object):

CSharp

// create client instance (Rebex.Net.WebClient or Rebex.Net.HttpRequestCreator)
// ...

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

VisualBasic

' create client instance (Rebex.Net.WebClient or Rebex.Net.HttpRequestCreator)
' ...

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

A sample log file:

2017-01-23 16:24:25.472 Opening log file.
2017-01-23 16:24:25.472 Using FileLogWriter version 2.0.0.0.
2017-01-23 16:24:25.549 INFO WebRequest(1)[12] HTTP: Connecting to 'http://test.rebex.net/'...
2017-01-23 16:24:25.649 INFO WebRequest(1)[12] HTTP: Sending request: GET /
2017-01-23 16:24:25.650 DEBUG WebRequest(1)[12] HTTP: Sending request (126 bytes).
2017-01-23 16:24:25.709 INFO WebRequest(1)[12] HTTP: Received response: 200 OK.
2017-01-23 16:24:25.710 DEBUG WebRequest(1)[12] HTTP: Received 10 headers.
2017-01-23 16:24:25.711 DEBUG WebRequest(1)[12] HTTP: Response Content-Length: 1668 bytes.
2017-01-23 16:24:25.711 DEBUG WebRequest(1)[12] HTTP: Response Transfer-Encoding not specified.
2017-01-23 16:24:25.736 DEBUG WebRequest(1)[12] HTTP: Received content (1668 raw bytes, 3410 decompressed bytes).

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
client.LogWriter = new Rebex.FileLogWriter(
    @"C:\MyData\log.txt", Rebex.LogLevel.Debug);

VisualBasic

' start logging to a file at debug level
client.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
client.LogWriter = new Rebex.FileLogWriter(
    @"C:\MyData\log.txt", Rebex.LogLevel.Debug);

VisualBasic

' start logging to a file at debug level
client.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 at debug level
client.LogWriter = new Rebex.TraceLogWriter(Rebex.LogLevel.Debug);

VisualBasic

' start logging to subscribed trace listeners at debug level
client.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
client.LogWriter = new Rebex.ConsoleLogWriter(Rebex.LogLevel.Debug);

VisualBasic

' start logging to the standard output stream at debug level
client.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
client.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.Info)

' start logging to both console and file log writers
client.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 HTTP: {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 HTTP: {0}", message)
    End Sub
End Class