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. 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 (after creating the SyslogClient
or SyslogServer
object):
// start logging to a file server.LogWriter = new Rebex.FileLogWriter(@"C:\MyData\log.txt");
' start logging to a file server.LogWriter = New Rebex.FileLogWriter("C:\MyData\log.txt")
Sample server-side log file:
2021-03-16 18:01:12.111 INFO SyslogServer(1)[30] Info: Starting server.
2021-03-16 18:01:12.113 DEBUG SyslogServer(1)[30] Info: Binding 'Udp' protocol listener to the 0.0.0.0:514 endpoint.
2021-03-16 18:01:12.127 DEBUG SyslogServer(1)[30] Info: Binding 'Tcp' protocol listener to the 0.0.0.0:514 endpoint.
2021-03-16 18:01:12.129 DEBUG SyslogServer(1)[30] Info: Binding 'Tls' protocol listener to the 0.0.0.0:6514 endpoint.
2021-03-16 18:01:12.130 INFO SyslogServer(1)[30] Info: Server started.
2021-03-16 18:01:12.183 DEBUG SyslogServer(1)[27] Connection #1: Accepted new connection: 0.0.0.0:514<==Tcp==>127.0.0.1:50794.
2021-03-16 18:01:12.188 DEBUG SyslogServer(1)[27] Connection #1: Connection added to internal list.
2021-03-16 18:01:12.191 DEBUG SyslogServer(1)[26] Connection #1: Received 29 bytes.
2021-03-16 18:01:12.193 DEBUG SyslogServer(1)[26] Connection #1: Message parsed.
2021-03-16 18:01:12.194 DEBUG SyslogServer(1)[27] Connection #1: Closing connection 0.0.0.0:514<==Tcp==>127.0.0.1:50794.
Sample client-side log file:
2021-03-16 18:38:40.295 INFO SyslogClient(1)[9] Info: Connecting to localhost:514 using TCP/IP.
2021-03-16 18:38:40.295 INFO SyslogClient(1)[9] Info: Sending Syslog message (29 bytes).
2021-03-16 18:38:40.295 DEBUG SyslogClient(1)[9] Info: Disposing client instance.
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 server.LogWriter = new Rebex.FileLogWriter(@"C:\MyData\log.txt", Rebex.LogLevel.Debug);
' start logging to a file at debug level server.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 server.LogWriter = new Rebex.FileLogWriter(@"C:\MyData\log.txt", Rebex.LogLevel.Debug);
' start logging to a file at debug level server.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 server.LogWriter = new Rebex.TraceLogWriter(Rebex.LogLevel.Debug);
' start logging to subscribed trace listeners server.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 server.LogWriter = new Rebex.ConsoleLogWriter(Rebex.LogLevel.Debug);
' start logging to the standard output stream at debug level server.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 server.LogWriter = new Rebex.TeeLogWriter(consoleLogWriter, fileLogWriter);
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 server.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: {0}", message); } }
' Sample log writer that logs to the standard error output stream Public Class MyLogWriter Inherits LogWriterBase Protected Overrides Sub WriteMessage(ByVal message As String) Console.Error.WriteLine("Rebex: {0}", message) End Sub End Class
Back to feature list...