Log the events to SysLog on Remote Machine using Log4Net utility

I have given the C# class and procedure to log the events or exceptions generated in your C# application to the remote machine's special logging place called syslog. This is very useful to maintain a centralized logging mechanism and can be access from anywhere and easy to parse these logs by another utility(mostly be in Perl) because it specifies the severity in form of INFO, DEBUG, ERROR or WARNING. You can sent out the mails while ERROR or high severity log message logged in the SysLog.

You can intergrate this project with your existing project to enable the logging mechanism on remote host. It will require following changes.

1. You will need  to use log4net dll as a reference to your program and syslog_log4net config file must be a part of installation directory of your application. Please download these files from http://www.eggheadcafe.com/fileupload/-698781408_log4net.zip

2. The message and real_error_message will be generated from your application.

3. You will need to remove main function and add reference for "namespace SysLog_EventLogger" used in this program. And call to Send function from anywhere in your project.

Complete C# Program:
-----------------------
using System;
using System.Collections;
using System.Net.Sockets;
using System.Text;
using System.Net;
using log4net;
using log4net.Config;

namespace SysLog_EventLogger
{
    class My_Message_Logger
    {
        private static readonly ILog sysLog = LogManager.GetLogger(typeof(My_Message_Logger));

        public enum PriorityType
        {
            Emergency,
            Alert,
            Critical,
            Error,
            Warning,
            Notice,
            Informational,
            Debug
        }
        private static UdpClient udp;
        private static ASCIIEncoding ascii = new ASCIIEncoding();
        private static string machine = System.Net.Dns.GetHostName() + " ";

        public static void Send(string hostname, PriorityType priority, DateTime time, string body)
        {
            try
            {
                udp = new UdpClient(hostname, 514);
                byte[] rawMsg;
                string[] strParams = { priority.ToString()+": ", time.ToString("MMM dd HH:mm:ss "),
                                       machine, body };
                rawMsg = ascii.GetBytes(string.Concat(strParams));
                udp.Send(rawMsg, rawMsg.Length);
                udp.Close();
                udp = null;
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: " + ex);
            }
        }
        static void Main(string[] args)
        {       
            string real_error_message = "This will be from your application";
            string message = "The error was occured in XXX application: " + real_error_message;
            PriorityType t = PriorityType.Error;
            Send("loghost.hyd.deshaw.com", t, DateTime.Now, message);          
        }
    }
}

Best Luck,
Megha
By Perry    Popularity  (2042 Views)