.NET and Syslog?


Interop is not only about webservices.  There are lots of other systems that already expose interfaces of some sort that do not depend upon angle brackets and WSDL.


Take Syslog, for instance.  The Windows Event Log is the analog to Syslog in the *nix world.  It’s pretty dern easy for a .NET app to spool an event to the Windows Event Log.  But how can a .NET app log an event in a Syslog? 


There are bridges available, that can “tee” the Windows Event Log and send the result to a Syslog.  If that’s not what you want, you can also connect directly to Syslog just using the UDP protocol.  There is no built-in support for Syslog specifically in the .NET base class framework, but it is pretty simple to do:


// syslog.cs
// ——————————————————————
//
// small class for sending log messages to Syslog from .NET
//
// Author: Admin
// built on host: DINOCH-2
// Created Fri Jul 24 01:10:38 2009
//
// last saved:
// Time-stamp: <2009-July-24 01:23:12>
// ——————————————————————
//
// Copyright (c) 2009 by Dino Chiesa
// All rights reserved!
//
// This code is licensed under the Ms-PL license:
//
// This license governs use of the accompanying software. If you use the
// software, you accept this license. If you do not accept the license,
// do not use the software.
//
// 1. Definitions
//
// The terms “reproduce,” “reproduction,” “derivative works,” and “distribution” have
// the same meaning here as under U.S. copyright law. A “contribution” is the original
// software, or any additions or changes to the software. A “contributor” is any person
// that distributes its contribution under this license. “Licensed patents” are a
// contributor’s patent claims that read directly on its contribution.
//
//
// 2. Grant of Rights
//
// (A) Copyright Grant- Subject to the terms of this license, including the license
// conditions and limitations in section 3, each contributor grants you a
// non-exclusive, worldwide, royalty-free copyright license to reproduce its
// contribution, prepare derivative works of its contribution, and distribute its
// contribution or any derivative works that you create.
//
// (B) Patent Grant- Subject to the terms of this license, including the license
// conditions and limitations in section 3, each contributor grants you a
// non-exclusive, worldwide, royalty-free license under its licensed patents to
// make, have made, use, sell, offer for sale, import, and/or otherwise dispose of
// its contribution in the software or derivative works of the contribution in the
// software.
//
// 3. Conditions and Limitations
//
// (A) No Trademark License- This license does not grant you rights to use any
// contributors’ name, logo, or trademarks.
//
// (B) If you bring a patent claim against any contributor over patents that you claim
// are infringed by the software, your patent license from such contributor to the
// software ends automatically. (C) If you distribute any portion of the software,
// you must retain all copyright, patent, trademark, and attribution notices that
// are present in the software.
//
// (D) If you distribute any portion of the software in source code form, you may do so
// only under this license by including a complete copy of this license with your
// distribution. If you distribute any portion of the software in compiled or object
// code form, you may only do so under a license that complies with this license.
//
// (E) The software is licensed “as-is.” You bear the risk of using it. The contributors
// give no express warranties, guarantees or conditions. You may have additional
// consumer rights under your local laws which this license cannot change. To the
// extent permitted under your local laws, the contributors exclude the implied
// warranties of merchantability, fitness for a particular purpose and
// non-infringement.
//
// ——————————————————————

using System;
using System.Net;
using System.Net.Sockets;

namespace Ionic.Syslog
{
public enum Level
{
Emergency= 0,
Alert=1,
Critical=2,
Error=3,
Warning=4,
Notice=5,
Information=6,
Debug=7,
}

public enum Facility
{
Kernel=0,
User=1,
Mail=2,
Daemon=3,
Auth=4,
Syslog=5,
Lpr=6,
News=7,
UUCP=8,
Cron=9,
Local0=10,
Local1=11,
Local2=12,
Local3=13,
Local4=14,
Local5=15,
Local6=16,
Local7=17,
}

public class Message
{
private int _facility;
public int Facility
{
get { return _facility;}
set { _facility=value; }
}
private int _level;
public int Level
{
get { return _level;}
set { _level=value; }
}
private string _text;
public string Text
{
get { return _text;}
set { _text=value; }
}
public Message() {}
public Message (int facility, int level, string text)
{
_facility= facility;
_level= level;
_text= text;
}
}

/// need this helper class to expose the Active propery of UdpClient
/// (why is it protected, anyway?)
public class UdpClientEx : System.Net.Sockets.UdpClient
{
public UdpClientEx() : base() { }
public UdpClientEx(IPEndPoint ipe) : base (ipe) { }
~UdpClientEx()
{
if (this.Active) this.Close();
}

public bool IsActive
{
get { return this.Active ; }
}
}

public class Client
{
private IPHostEntry ipHostInfo;
private IPAddress ipAddress;
private IPEndPoint ipLocalEndPoint;
private Ionic.Syslog.UdpClientEx udpClient;
private string _sysLogServerIp= null;
private int _port= 514;

public Client()
{
ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
ipAddress = ipHostInfo.AddressList[0];
ipLocalEndPoint = new IPEndPoint(ipAddress, 0);
udpClient= new Syslog.UdpClientEx(ipLocalEndPoint);
}

public bool IsActive
{
get { return udpClient.IsActive ; }
}

public void Close()
{
if (udpClient.IsActive) udpClient.Close();
}

public int Port
{
set {_port= value; }
get {return _port; }
}

public string SysLogServerIp
{
get { return _sysLogServerIp; }
set
{
if ((_sysLogServerIp==null) && (!IsActive))
{
_sysLogServerIp= value;
//udpClient.Connect(_hostIp, _port);
}
}
}

public void Send(Syslog.Message message)
{
if (!udpClient.IsActive)
udpClient.Connect(_sysLogServerIp, _port);
if (udpClient.IsActive)
{
int priority = message.Facility * 8 + message.Level;
string msg = System.String.Format(“<{0}>{1} {2} {3}”,
priority,
DateTime.Now.ToString(“MMM dd HH:mm:ss”),
ipLocalEndPoint.Address,
message.Text);
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(msg);
udpClient.Send(bytes, bytes.Length);
}
else throw new Exception (“Syslog client Socket is not connected. Please set the SysLogServerIp property”);
}

}
}

namespace Ionic.Syslog.Tests
{
public class TestClient
{
public static void Main(string[] args)
{
Ionic.Syslog.Client c = new Ionic.Syslog.Client();
try
{
//c.Port= 1200;
c.SysLogServerIp= “127.0.0.1” ; // syslogd on local machine
int facility= (int) Syslog.Facility.User; // Local5
int level= (int) Syslog.Level.Warning; // Debug;
string text= (args.Length >0)?args[0]:“Hello, Syslog World.”;

c.Send(new Syslog.Message(facility,level,text));
}
catch (System.Exception ex1)
{
Console.WriteLine(“Exception! “ + ex1);
}
finally
{
c.Close();
}
}
}
}


Update: you can find other source code here


 

Comments (7)

  1. Uwe Keim says:

    What about writing an appender for LOG4NET (http://logging.apache.org)?

    LOG4NET is for me the by far best logging framework.

  2. Dino says:

    Uwe queries:

    > What about writing an appender for LOG4NET

    Why not? Should be pretty simple, but I am not volunteering! I figure someone from Mono is already doing this, or at least putting thought into it. There is a SysLogAppender in log4j, but I am not clear on whether it is implemented in log4net? The doc is here:

    http://logging.apache.org/log4j/docs/api-1.3/org/apache/log4j/net/SyslogAppender.html

    but the features page does not list Syslog.

    http://logging.apache.org/log4net/release/features.html

    It does, however, list the UdpAppender. There is only a very small leap to go from UdpAppender to SyslogAppender. Maybe they are the same.

    Also there is the Enterprise Instrumentation Framework, or EIF, and the logging block (http://msdn.microsoft.com/library/en-us/dnpag/html/Logging.asp ). To use the Logging Block, you’d need to build a Syslog sink, which is like a syslog appender for log4net. Again, it shouldn’t be difficult.

  3. Chris says:

    I’m getting the exception when the udp connection is opened:

    + ex1 {"The requested address is not valid in its context 127.0.0.1:514"} System.Exception {System.Net.Sockets.SocketException}

    I’m running kiwi Syslog Service on the same box, (configured for udp port 514).  Local firewalls are turned off.

    any suggestions?

  4. This is a very useful piece of code, thanks for your good work.

    I’ve modified your Send() method to include client IP and timestamp in the message header, so I thought I’d share:

    public void Send(Message message)

           {

               if (!helper.IsActive)

                   helper.Connect(_syslogServerIp, _port);

               if (helper.IsActive)

               {

                   int priority = message.Facility * 8 + message.Level;

                   string msg = System.String.Format("<{0}>{1} {2} {3}",

                                                    priority,

                                                    DateTime.Now.ToString("MMM dd HH:mm:ss"),

                                                    ipLocalEndPoint.Address,

                                                    message.Text);

                   byte[] bytes = System.Text.Encoding.ASCII.GetBytes(msg);

                   helper.Send(bytes, bytes.Length);

               }

               else throw new Exception("Syslog client Socket is not connected. Please set the host IP");

           }

  5. cheeso says:

    Thanks Dave, I’ve included your updates into the source in the article.

  6. Fred says:

    Thanks for your code, it works perfect for me!

  7. Scott says:

    This code works great with just one problem… the enums for the Facility are wrong, especially if you are trying to hit the LocalX facilities.

    See tools.ietf.org/…/rfc5424

    Local0 = 16

    Local1 = 17

    and so on.