More .NET libraries
-
Rebex Total Pack
All Rebex .NET libraries together
Back to feature list...
Syslog client
On this page:
Sending syslog messages
To send a syslog message, connect to a server and call the Send
method:
// create a SyslogClient instance
using (var client = new Rebex.Net.SyslogClient())
{
// connect to a syslog server
client.Connect(hostname, port, SyslogTransportProtocol.Tcp);
// send a message with default severity and facility
client.Send("Hello from Rebex Syslog client.");
// send a message with a specific severity and facility
client.Send("Everything is working fine.",
SyslogSeverity.Informational,
SyslogFacility.User);
}
' create a SyslogClient instance
Using client = New Rebex.Net.SyslogClient()
' connect to a syslog server
client.Connect(hostname, port, SyslogTransportProtocol.Tcp)
' send a message with default severity and facility
client.Send("Hello from Rebex Syslog client.")
' send a message with a specific severity and facility
client.Send("Everything is working fine.",
SyslogSeverity.Informational,
SyslogFacility.User)
End Using
Rebex Syslog supports three transport protocols: UDP, TCP, and TLS.
For more information about TLS and certificate validation, see TLS/SSL
and X.509 certificates.
Advanced syslog messaging
Use SyslogMessage
object to send messages with addition options:
// create and connect a SyslogClient instance
// ...
// prepare a syslog message
var message = new SyslogMessage();
message.Facility = SyslogFacility.System;
message.Severity = SyslogSeverity.Notice;
message.Hostname = "example.com";
message.ApplicationName = "Sample";
message.ProcessId = "123";
message.Text = "Hello from Rebex Syslog client.";
// send the message
client.Send(message);
// reuse message object
message.Text = "This is second message.";
// send updated message
client.Send(message);
' create and connect a SyslogClient instance
' ...
' prepare a syslog message
Dim message = New SyslogMessage()
message.Facility = SyslogFacility.System
message.Severity = SyslogSeverity.Notice
message.Hostname = "example.com"
message.ApplicationName = "Sample"
message.ProcessId = "123"
message.Text = "Hello from Rebex Syslog client."
' send the message
client.Send(message)
' reuse message object
message.Text = "This is second message."
' send updated message
client.Send(message)
Tip: A
SyslogMessage
instance can be reused for sending multiple messages with the same options.
SyslogMessage
object also makes it possible to get a raw byte array representation of the message:
// get raw byte data of the syslog message
var rawData = message.Encode();
// work with the raw data
// for example: write it to a stream
stream.Write(rawData, 0, rawData.Length);
' get raw byte data of the syslog message
Dim rawData = message.Encode()
' work with the raw data
' for example: write it to a stream
stream.Write(rawData, 0, rawData.Length)
Message framing methods
Syslog defines two framing methods for messages transmitted over TCP and TLS connections:
- Octet-Counting: used by default.
-
Non-Transparent: requires message trailer to be used (default is
0x0A
).
// create and connect a SyslogClient instance
// ...
// set the Non-Transparent-Framing
client.Settings.Framing = SyslogFraming.NonTransparent;
// set message trailer if needed
client.Settings.Trailer = new byte[] { 0x0D, 0x0A };
// send the message
client.Send("Hello from Rebex Syslog client.");
' create and connect a SyslogClient instance
' ...
' set the Non-Transparent-Framing
client.Settings.Framing = SyslogFraming.NonTransparent
' set message trailer if needed
client.Settings.Trailer = New Byte() {&HD, &HA}
' send the message
client.Send("Hello from Rebex Syslog client.")
Message formats
Rebex Syslog supports 4 message formats, covered by the SyslogMessageFormat
enumeration:
-
Modern
- format defined in RFC 5424. -
Bsd
- format defined in RFC 3164. -
Legacy
- format for legacy Syslog systems, which uses<PRI>
header, but other fields are unknown/undefined. -
Plain
- this does not use any header values. The whole message corresponds to the value of theSyslogMessage.Text
property.
// create and connect a SyslogClient instance
// ...
// set the BSD Syslog message format
client.Settings.Format = SyslogMessageFormat.Bsd;
// send the message
client.Send("Hello from Rebex Syslog client.");
' create and connect a SyslogClient instance
' ...
' set the BSD Syslog message format
client.Settings.Format = SyslogMessageFormat.Bsd
' send the message
client.Send("Hello from Rebex Syslog client.")
Tip: The
Legacy
and Plain
formats can be used to produce Syslog messages in any format.
Just insert required header fields at the beginning of the SyslogMessage.Text
property.
For example:
// set format to Plain to send the message in custom format
client.Settings.Format = SyslogMessageFormat.Plain;
// prepare required custom headers
string headers = "[CUSTOM1] [CUSTOM2] %% ";
// send the message
client.Send(headers + "Hello from Rebex Syslog client.");
' set format to Plain to send the message in custom format
client.Settings.Format = SyslogMessageFormat.Plain
' prepare required custom headers
Dim headers = "[CUSTOM1] [CUSTOM2] %% "
' send the message
client.Send(headers + "Hello from Rebex Syslog client.")
Back to feature list...