More .NET libraries
-
Rebex Total Pack
All Rebex .NET libraries together
Back to feature list...
Communication logging
On this page:
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 TlsClientSocket
object):
// create an instance of TlsClientSocket // ... // start logging to a file socket.LogWriter = new Rebex.FileLogWriter(@"C:\MyData\log.txt");
A sample log file:
2020-02-20 12:34:56.011 INFO TlsClientSocket(1)[15] Info: Platform: Windows 6.2.9200 32-bit; CLR: 4.0.30319.42000
2020-02-20 12:34:56.011 DEBUG TlsClientSocket(1)[15] Info: Culture: en; Windows-1250
2020-02-20 12:34:56.011 INFO TlsClientSocket(1)[15] Info: Resolving 'test.rebex.net'.
2020-02-20 12:34:56.014 INFO TlsClientSocket(1)[15] Info: Connecting to 10.0.0.42:443 using TlsClientSocket.
2020-02-20 12:34:56.015 DEBUG TlsClientSocket(1)[15] Info: Connection established (socket #3841916).
2020-02-20 12:34:56.015 DEBUG TlsClientSocket(1)[15] TLS: Using modern transport layer.
2020-02-20 12:34:56.018 INFO TlsClientSocket(1)[15] TLS: Starting TLS negotiation.
2020-02-20 12:34:56.018 DEBUG TlsClientSocket(1)[15] TLS: Using TLS 1.3 core.
2020-02-20 12:34:56.035 DEBUG TlsClientSocket(1)[18] TLS: HandshakeMessage:ClientHello was sent.
2020-02-20 12:34:56.047 DEBUG TlsClientSocket(1)[21] TLS: HandshakeMessage:ServerHello was received.
2020-02-20 12:34:56.154 INFO TlsClientSocket(1)[21] TLS: Negotiating TLS 1.3, ECDH with secp256r1, AES with 256-bit key in GCM mode, SHA384.
2020-02-20 12:34:56.180 DEBUG TlsClientSocket(1)[18] TLS: CipherSpec:ChangeCipherSpec was received.
2020-02-20 12:34:56.189 DEBUG TlsClientSocket(1)[14] TLS: HandshakeMessage:EncryptedExtensions was received.
2020-02-20 12:34:56.213 DEBUG TlsClientSocket(1)[30] TLS: HandshakeMessage:Certificate was received.
2020-02-20 12:34:56.217 DEBUG TlsClientSocket(1)[18] TLS: HandshakeMessage:CertificateVerify was received.
2020-02-20 12:34:56.226 DEBUG TlsClientSocket(1)[30] TLS: Verifying server certificate ('CN=test.rebex.net, O=Rebex').
2020-02-20 12:34:56.226 DEBUG TlsClientSocket(1)[30] TLS: Certificate verification result: Accept.
2020-02-20 12:34:56.227 DEBUG TlsClientSocket(1)[30] TLS: Verifying server key exchange signature.
2020-02-20 12:34:56.241 DEBUG TlsClientSocket(1)[21] TLS: HandshakeMessage:Finished was received.
2020-02-20 12:34:56.286 DEBUG TlsClientSocket(1)[18] TLS: CipherSpec:ChangeCipherSpec was sent.
2020-02-20 12:34:56.287 INFO TlsClientSocket(1)[10] TLS: Connection secured using cipher: TLS 1.3, ephemeral ECDH, AES with 256-bit key in GCM mode, AEAD.
2020-02-20 12:34:56.288 DEBUG TlsClientSocket(1)[26] TLS: HandshakeMessage:Finished was sent.
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:
// start logging to a file at debug level socket.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
:
// start logging to a file at debug level socket.LogWriter = new Rebex.FileLogWriter( @"C:\MyData\log.txt", Rebex.LogLevel.Debug);
.NET trace listener
To log to .NET Trace Listeners, use TraceLogWriter
:
// start logging to subscribed trace listeners at debug level socket.LogWriter = new Rebex.TraceLogWriter(Rebex.LogLevel.Debug);
Standard output stream
To log to the standard output stream, use ConsoleLogWriter
:
// start logging to the standard output stream at debug level socket.LogWriter = new Rebex.ConsoleLogWriter(Rebex.LogLevel.Debug);
Logging to multiple log writers
To log to more log writers at the same time, use TeeLogWriter
:
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 socket.LogWriter = new Rebex.TeeLogWriter(consoleLogWriter, fileLogWriter);
Writing custom log writers
To implement your own LogWriter, either implementRebex.ILogWriter
interface or simply derive a class from Rebex.LogWriterBase
:
// 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 TLS: {0}", message); } }
Back to feature list...