Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Windows 10 IoT Core: Logging to Your Syslog Server

DZone's Guide to

Windows 10 IoT Core: Logging to Your Syslog Server

A Syslog server is a great place to store logs away from external servers. Popular for Linux users, they can also be used to store IoT logs.

· IoT Zone
Free Resource

Address your IoT software testing needs – improve quality, security, safety, and compliance across the development lifecycle.

Syslog servers are most popular in the Linux world. These servers accept logs from different clients and are not open to external networks. There are also Syslog servers available for Windows. This article shows how to write messages to a Syslog server from a Windows 10 IoT Core background application.

Syslog Server

A Syslog server is logging server. Usually, it is a separate box or virtual machine that is configured to accept log messages and not accept any users from external networks. If some system or device gets hacked, then, usually, your Syslog server is not accessible and the logs are there to analyze what happened.

syslog-serverAn example network with a Syslog server.
Copyright belongs to Network Management Software

For more information about Syslog server follow these links:

ILogger Interface

In my TemperatureStation IoT solution, I use ILogger interface to support multiple logger implementations.

internal interface ILogger
{
    void Debug(string message);
    void Info(string message);
    void Warn(string message);
    void Error(string message);
    void Critical(string message);
}


Now let’s get to logger implementation.

Adding Support for Syslog Server

To write a logger for a Syslog server, we need enum with log levels. These doesn’t match completely with the ones used in .NET-based logging solutions.

public enum SyslogLogLevel
{
    Emergency,
    Alert,
    Critical,
    Error,
    Warn,
    Notice,
    Info,
    Debug
}


And here is the logger class for Syslog. It’s simple and primitive, but it works. The most important method in this class is Send(). All other methods are just shortcuts for it that come with ILogger interface.

internal class SyslogLogger : ILogger
{
    private const string SyslogClientName = "RPI-001";
    private const string SyslogHost = "192.168.210.2";
    private const int SyslogPort = 514;
    private const int SyslogFacility = 16;

    public void Critical(string message)
    {
        Send(SyslogLogLevel.Critical, message);
    }

    public void Debug(string message)
    {
        Send(SyslogLogLevel.Debug, message);
    }

    public void Error(string message)
    {
        Send(SyslogLogLevel.Error, message);
    }

    public void Info(string message)
    {
        Send(SyslogLogLevel.Info, message);
    }

    public void Warn(string message)
    {
        Send(SyslogLogLevel.Warn, message);
    }

    internal static void Send(SyslogLogLevel logLevel, string message)
    {
        if (string.IsNullOrWhiteSpace(SyslogHost) || SyslogPort <= 0)
        {
            return;
        }

        var hostName = Dns.GetHostName();
        var level = SyslogFacility * 8 + (int)logLevel;
        var logMessage = string.Format("<{0}>{1} {2}", level, hostName, message);
        var bytes = Encoding.UTF8.GetBytes(logMessage);

        using (var client = new UdpClient())
        {
            client.SendAsync(bytes, bytes.Length, SyslogHost, SyslogPort)
                  .Wait();
        }
    }
}


Here is the fragment of code that shows how loggers based on the ILogger interface are used in my TemperatureStation solution.

_logger.Info("Timer callback: reading and reporting sensors");

try
{
    var readings = _sensorsClient.ReadSensors();
    await _reportingClient.ReportReadings(readings);

    _logger.Info(readings.ToString());
}
catch (Exception ex)
{
    _logger.Critical(ex.ToString());
}

_logger.Info("Timer callback: reading and reporting is done");


And here is how Visual Syslog Server monitor is displaying messages sent from my RaspberryPI.

Visual Syslog Server

NB! You can use the same code also with Linux based Syslog servers as they share the same protocol.

Wrapping Up

Connecting good things from two worlds may work out as a perfect solution. Here, we took a Syslog server — popular in Linux circles — and used it as logging solution for an IoT service running on Windows IoT Core and a Raspberry Pi. We wrote some lines of code to make it work and we were able to send log messages to Syslog server.

Accelerate the delivery of high-quality software in the connected IoT era through an integrated analysis, testing, security, and analytics platform

Topics:
windows 10 iot core ,logging ,syslog ,raspberry pi ,iot ,tutorial

Published at DZone with permission of Gunnar Peipman, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}