Hi,

I don't know if this will help..about a year ago I created an appender for 
logging to a syslog server (for which I use
to the excellent Kiwi SysLog [1]).

Here's a sample configuration element:

  <appender name="SysLogAppender" type="log4net.Appender.SysLogAppender, 
syslog4net">
   <param name="Host" value="127.0.0.1" />
   <param name="Port" value="514" />
   <param name="ProtocolType" value="Udp" />
   <param name="BufferSize" value="1" />
   <!-- <param name="Port" value="1468" />
   <param name="ProtocolType" value="Tcp" /> -->
   
   <layout type="log4net.Layout.PatternLayout, log4net">
    <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n" />
   </layout>   
  </appender>

The Tcp logging (and port) is commented out and is muturally exculsive to Udp 
(and port).  With Kiwi SysLog you
can enable either or both.  If you compile the appender as part of log4net you 
will need to change the syslog4net 
assembly in the appender type above.

If you do use Kiwi SysLog I recommend you modify the max message length (under 
modifiers in the configuration), otherwise
exceptions tend to get missed.  Also enable retain messages between restarts 
and increase the messages displayed.

Cheers,

Duncan

[1] http://www.kiwisyslog.com/info_syslog.htm

----- Original Message ----- 
From: "Cheng" <[EMAIL PROTECTED]>
To: "Log4NET User" <[email protected]>
Sent: Monday, September 27, 2004 5:06 PM
Subject: Re: Watching logs


>I have problem to make UDPReceiver work in Chainsaw too. 
> 
> NetLogClient seems to be able to receive UDPAppender, but it display
> all un-readable charactors in the message column... also it will be
> great if it has the ability to break-down the message by Level and
> namespace. the project on sourceforge seems to be dead....
> 
> 
> On Mon, 27 Sep 2004 11:28:17 +0200, Shai Berger <[EMAIL PROTECTED]> wrote:
>> As far as I have seen, the most recent Chainsaw (well, the package they make 
>> available for download) has a problem with the UDPReceiver. I could not get 
>> it to become "active".
>> 
>> 
>> 
>> > -----Original Message-----
>> > From: Nicko Cadell [mailto:[EMAIL PROTECTED]
>> > Sent: Monday, September 27, 2004 1:43 AM
>> > To: Log4NET User
>> > Subject: RE: Watching logs
>> >
>> >
>> > Actually it has been a long time since I have tried to configure
>> > Chainsaw and log4net and I cannot remember the exact details, but you
>> > need to at least do the following:
>> >
>> > You need to set log4net up to use the UdpAppender with the
>> > XmlLayoutSchemaLog4j layout.
>> >
>> > <appender name="UdpAppender" type="log4net.Appender.UdpAppender">
>> >       <remoteAddress value="1.2.3.4" />
>> >       <remotePort value="5555" />
>> >       <layout type="log4net.Layout.XmlLayoutSchemaLog4j" />
>> > </appender>
>> >
>> > You need to configure Chainsaw and log4j to use the UDPReceiver to
>> > listen for incoming log events.
>> >
>> > Nicko
>> >
>> >
>> > > -----Original Message-----
>> > > From: Ayende Rahien [mailto:[EMAIL PROTECTED]
>> > > Sent: 27 September 2004 00:01
>> > > To: 'Log4NET User'
>> > > Subject: RE: Watching logs
>> > >
>> > > I couldn't get it to work.
>> > > Is there some place where it's explained?
>> > >
>> > >
>> > > > -----Original Message-----
>> > > > From: Nicko Cadell [mailto:[EMAIL PROTECTED]
>> > > > Sent: Monday, September 27, 2004 12:32 AM
>> > > > To: Log4NET User
>> > > > Subject: RE: Watching logs
>> > > >
>> > > > Chainsaw is the best (and I think the only) GUI logging
>> > viewer that
>> > > > log4net currently works with.
>> > > >
>> > > > > -----Original Message-----
>> > > > > From: Ayende Rahien [mailto:[EMAIL PROTECTED]
>> > > > > Sent: 26 September 2004 19:36
>> > > > > To: 'Log4NET User'
>> > > > > Subject: Watching logs
>> > > > >
>> > > > > I want to see the logs as they are being logged on a GUI
>> > > application.
>> > > > > Is there some log client that I can use?
>> > > > > I tried chainsow, but I couldn't get it to show me anything.
>> > > > >
>> > >
>> > >
>> > >
>> > >
>> > 
>> **************************************************************************************************
>> The contents of this email and any attachments are confidential.
>> They are intended for the named recipient(s) only.
>> If you have received this email in error please notify the system manager or 
>>  the
>> sender immediately and do not disclose the contents to anyone or make copies.
>> 
>> ** TASE scanned this email for viruses, vandals and malicious content. **
>> **************************************************************************************************
>>
>
using System;
using System.Globalization;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;

using log4net;
using log4net.Layout;
using log4net.spi;

namespace log4net.Appender {
        /// <summary>
        /// 
        /// </summary>
        public class SysLogAppender : 
log4net.Appender.BufferingAppenderSkeleton {
                
                // Will only buffer 10 events before sending

                // The following constants are extracted from a syslog.h file
                // copyrighted by the Regents of the University of California
                // I hope nobody at Berkley gets offended.

                public enum SyslogFacility {
                        /** Kernel messages */
                        KERN     = 0,
                        /** Random user-level messages */
                        USER     = 1<<3,
                        /** Mail system */
                        MAIL     = 2<<3,
                        /** System daemons */
                        DAEMON   = 3<<3,
                        /** security/authorization messages */
                        AUTH     = 4<<3,
                        /** messages generated internally by syslogd */
                        SYSLOG   = 5<<3,

                        /** line printer subsystem */
                        LPR      = 6<<3,
                        /** network news subsystem */
                        NEWS     = 7<<3,
                        /** UUCP subsystem */
                        UUCP     = 8<<3,
                        /** clock daemon */
                        CRON     = 9<<3,
                        /** security/authorization  messages (private) */
                        AUTHPRIV = 10<<3,
                        /** ftp daemon */
                        FTP      = 11<<3,

                        // other codes through 15 reserved for system use
                        /** reserved for local use */
                        LOCAL0 = 16<<3,
                        /** reserved for local use */
                        LOCAL1 = 17<<3,
                        /** reserved for local use */
                        LOCAL2 = 18<<3,
                        /** reserved for local use */
                        LOCAL3 = 19<<3,
                        /** reserved for local use */
                        LOCAL4 = 20<<3,
                        /** reserved for local use */
                        LOCAL5 = 21<<3,
                        /** reserved for local use */
                        LOCAL6 = 22<<3,
                        /** reserved for local use*/
                        LOCAL7 = 23<<3,
                }

                public enum SyslogLevel {
                        Emergency = 0,
                        Alert = 1,
                        Critical = 2,
                        Error = 3,
                        Warn = 4,
                        Notice = 5,
                        Info = 6,
                        Debug = 7
                }
                
                public enum SyslogProtocolType {
                        Tcp = 0,
                        Udp = 1
                }

                // default ports
                private const int DEFALUT_TCP_PORT = 1468;
                private const int DEFALUT_UDP_PORT = 514;

                // need to defined default ports for both TCP and UDP

                private string _host;
                private int _port = 0;
                private SyslogProtocolType _protocolType;
                private SyslogFacility _facility;
                private Socket _sysLogSocket = null;

                public SysLogAppender() {
                        _protocolType = SyslogProtocolType.Tcp;
                        _facility = SyslogFacility.USER;

                        // override buffer size
                        // this.BufferSize = 10;
                }

                #region Public Instance Properties
                public string Host {
                        get { return _host; }
                        set { _host = value; }
                }

                public int Port {
                        get { return _port; }
                        set { 
                                if (value < IPEndPoint.MinPort || value > 
IPEndPoint.MaxPort) {
                                        throw new ArgumentOutOfRangeException(
                                                "The value specified is less 
than " + 
                                                
IPEndPoint.MinPort.ToString(NumberFormatInfo.InvariantInfo) + 
                                                " or greater than " + 
                                                
IPEndPoint.MaxPort.ToString(NumberFormatInfo.InvariantInfo) + ".");
                                } 
                                else {
                                        _port = value;
                                }
                        }
                }

                public SyslogProtocolType ProtocolType {
                        get { return _protocolType; }
                        set { _protocolType = value; }
                }

                public SyslogFacility Facility {
                        get { return _facility; }
                        set { _facility = value; }
                }
                #endregion

                #region Implementation of IOptionHandler

                /// <summary>
                /// Initialise the appender based on the options set
                /// </summary>
                public override void ActivateOptions() {
                        base.ActivateOptions();
                        
                        if (this._host == null) {
                                throw new ArgumentNullException("The required 
property 'Host' was not specified.");
                        } 
                        else if (_port < IPEndPoint.MinPort || _port > 
IPEndPoint.MaxPort) {
                                throw new ArgumentOutOfRangeException(
                                        "The Port is less than " + 
                                        
IPEndPoint.MinPort.ToString(NumberFormatInfo.InvariantInfo) + 
                                        " or greater than " + 
                                        
IPEndPoint.MaxPort.ToString(NumberFormatInfo.InvariantInfo) + ".");
                        }                       

                        InitialiseSysLog();
                }

                #endregion

                #region Initialisation
                protected void InitialiseSysLog() {
                        System.Net.Sockets.ProtocolType socketProtocolType;
                        
                        socketProtocolType = GetProtocolDetails();

                        SocketType socType = SocketType.Stream;
                        if (socketProtocolType == 
System.Net.Sockets.ProtocolType.Udp) 
                                socType = SocketType.Dgram;

                        _sysLogSocket = new Socket(AddressFamily.InterNetwork, 
socType, socketProtocolType);
                        
                        IPAddress ip = GetIPAddress();

                        _sysLogSocket.Connect(new IPEndPoint(ip, _port));

                        if (!_sysLogSocket.Connected) {
                                // TODO: log an error
                                throw new Exception("Could not connect to 
host");
                        }
                }

                private IPAddress GetIPAddress() {
                        Uri testHost = new Uri("http://"; + _host);
                        if (testHost.HostNameType == UriHostNameType.Dns) {
                                
                                IPHostEntry ipEntry = Dns.GetHostByName(_host); 
                                if (ipEntry.AddressList.Length == 0) {
                                        throw new ArgumentException("Host not 
found", "Host");
                                }

                                return ipEntry.AddressList[0];
                        }
                        else {
                                return IPAddress.Parse(_host);
                        }
                }

                // has the side effect of setting the port if _port == 0
                private System.Net.Sockets.ProtocolType GetProtocolDetails() {
                        System.Net.Sockets.ProtocolType socketProtocolType;

                        if (_protocolType == SyslogProtocolType.Tcp) {
                                socketProtocolType = 
System.Net.Sockets.ProtocolType.Tcp;
                                if (_port == 0) 
                                        _port = DEFALUT_TCP_PORT;
                        }
                        else {
                                socketProtocolType = 
System.Net.Sockets.ProtocolType.Udp;
                                if (_port == 0) 
                                        _port = DEFALUT_UDP_PORT;
                        }

                        return socketProtocolType;
                }
                #endregion

                #region Override implementation of AppenderSkeleton

                /// <summary>
                /// This appender requires a <see cref="Layout"/> to be set.
                /// </summary>
                /// <value><c>true</c></value>
                protected override bool RequiresLayout {
                        get { return true; }
                }

                /// <summary>
                /// Override the parent method to close the database
                /// </summary>
                public override void OnClose() {
                        base.OnClose();
                
                        if (_sysLogSocket != null) {
                                if (_sysLogSocket.Connected) {
                                        _sysLogSocket.Close();
                                }
                        }
                }

                #endregion

                #region Override implementation of BufferingAppenderSkeleton

                /// <summary>
                /// Send the contents of the buffer to the remote sink.
                /// </summary>
                /// <param name="events">The events to send.</param>
                 protected override void SendBuffer(LoggingEvent[] events) {
                        // Set the hostname
                        foreach(LoggingEvent e in events) {
                                //e.Properties["hostname"] = m_hostName;
                        
                                byte[] toSend = 
Encoding.ASCII.GetBytes(FormatSysLogMessage(e));
                
                                _sysLogSocket.Send(toSend, toSend.Length, 
SocketFlags.None);                            
                        }
                }

                protected virtual string FormatSysLogMessage(LoggingEvent 
logEvent) {
                        StringBuilder sb = new StringBuilder();
                        sb.Append("<");
                        sb.Append(GetFacilityAndLevel(logEvent).ToString());
                        sb.Append(">");
                        sb.Append(FormatLogEvent(logEvent));

                        return sb.ToString();
                }

                protected virtual string FormatLogEvent(LoggingEvent logEvent) {
                        if (Layout == null) {
                                ErrorHandler.Error("SysLogAppender: No Layout 
specified.");
                                return "";
                        }
                        else {
                                return Layout.Format(logEvent);
                        }
                }

                protected virtual int GetFacilityAndLevel(LoggingEvent 
logEvent) {
                        SyslogLevel lev = TranslateLevel(logEvent);
                        
                        return (Convert.ToInt32(_facility) | 
Convert.ToInt32(lev));
                }

                protected virtual SyslogLevel TranslateLevel(LoggingEvent 
logEvent) {
                        switch (logEvent.Level.Name.ToLower()) {
                                case "info": 
                                        return SyslogLevel.Info;
                                case "debug": 
                                        return SyslogLevel.Debug;
                                case "error": 
                                        return SyslogLevel.Error;
                                case "warn": 
                                        return SyslogLevel.Warn;
                                case "fatal": 
                                        return SyslogLevel.Critical;
                                default:
                                        return SyslogLevel.Info;
                        }
                }

                #endregion

        }
}

Reply via email to