.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.