I attached an implementation of a syslog log4net appender.  Please
review.  I added a BSD license header because I used the defines from
syslog.h.  Is that correct?  Please review the copyright and license.

Thanks,
Rob

>>> [EMAIL PROTECTED] 8/22/04 10:16 AM >>>
Rob,

You may want to look at the log4j syslog implementation:

http://cvs.apache.org/viewcvs.cgi/logging-log4j/src/java/org/apache/log4

j/net/SyslogAppender.java?rev=1.18&view=auto

http://cvs.apache.org/viewcvs.cgi/logging-log4j/src/java/org/apache/log4

j/helpers/SyslogWriter.java?rev=1.5&view=auto

http://cvs.apache.org/viewcvs.cgi/logging-log4j/src/java/org/apache/log4

j/helpers/SyslogQuietWriter.java?rev=1.5&view=auto

Nicko 

> -----Original Message-----
> From: Rob Lyon [mailto:[EMAIL PROTECTED] 
> Sent: 21 August 2004 16:06
> To: [email protected] 
> Subject: syslog log4net appender
> 
> I need a syslog log4net appender.  Has anyone started an 
> implementation?
> Does anyone have any suggestions as I start an implementation?
> 
> Thanks,
> Rob
> 
> 
#region Copyright & License
/*
 * Copyright 2001-2004 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Copyright (c) 1982, 1986, 1988, 1993
 *      The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *      @(#)syslog.h    8.1 (Berkeley) 6/2/93
 */
#endregion

using System;
using System.Net;

using log4net.spi;

namespace log4net.Appender 
{
        /// <summary>
        /// Logs entries to a remote syslog daemon.
        /// </summary>
        /// <author>Rob Lyon</author>
        public class SyslogAppender : UdpAppender 
        {
                #region Public Instance Constructors

                /// <summary>
                /// Initializes a new instance of the <see 
cref="SyslogAppender" /> class.
                /// </summary>
                /// <remarks>
                /// The instance of the <see cref="SyslogAppender" /> class is 
set up to write 
                /// to a remote syslog daemon.
                /// </remarks>
                public SyslogAppender() 
                {
                        // syslog udp defaults
                        this.RemotePort = SYSLOG_PORT;
                        this.RemoteAddress = 
System.Net.IPAddress.Parse("127.0.0.1");
                        this.Encoding = System.Text.Encoding.ASCII;
                }

                #endregion Public Instance Constructors

                #region Private Members

                /// <summary>
                /// Translates a log4net level to a syslog level.
                /// </summary>
                /// <param name="level">A log4net level.</param>
                /// <returns>A syslog level.</returns>
                int GetLevel(Level level)
                {
                        int result = LOG_DEBUG;

                        if (level == Level.OFF)
                        {
                                result = LOG_EMERG;
                        }
                        else if (level == Level.FATAL)
                        {
                                result = LOG_EMERG;
                        }
                        else if (level == Level.ERROR)
                        {
                                result = LOG_ERR;
                        }
                        else if (level == Level.WARN)
                        {
                                result = LOG_WARNING;
                        }
                        else if (level == Level.INFO)
                        {
                                result = LOG_INFO;
                        }

                        return result;
                }

                #endregion Private Members

                #region AppenderSkeleton Implementation

                /// <summary>
                /// This method is called by the <see 
cref="AppenderSkeleton.DoAppend"/> method.
                /// </summary>
                /// <param name="loggingEvent">The event to log.</param>
                /// <remarks>
                /// <para>
                /// Writes the event to a remote syslog daemon.
                /// </para>
                /// <para>
                /// The format of the output will depend on the appender's 
layout.
                /// </para>
                /// </remarks>
                protected override void Append(LoggingEvent loggingEvent) 
                {
                        try 
                        {
                                string message = 
RenderLoggingEvent(loggingEvent);

                                message = "<" + (facility | 
GetLevel(loggingEvent.Level)) + ">" + message;

                                Byte [] buffer = 
this.Encoding.GetBytes(message.ToCharArray());
                                
                                this.Client.Send(buffer, buffer.Length, 
this.RemoteAddress.ToString(), this.RemotePort);
                        } 
                        catch (Exception e) 
                        {
                                ErrorHandler.Error(
                                        "Unable to send logging event to remote 
host " + 
                                        this.RemoteAddress.ToString() + 
                                        " on port " + 
                                        this.RemotePort + ".", 
                                        e, 
                                        ErrorCodes.WriteFailure);
                        }
                }

                #endregion AppenderSkeleton Implementation

                #region Public Static Fields

                /// <summary>
                /// system is unusable
                /// </summary>
                public static readonly int LOG_EMERG = 0;

                /// <summary>
                /// action must be taken immediately
                /// </summary>
                public static readonly int LOG_ALERT = 1;

                /// <summary>
                /// critical conditions
                /// </summary>
                public static readonly int LOG_CRIT = 2;

                /// <summary>
                /// error conditions
                /// </summary>
                public static readonly int LOG_ERR = 3;

                /// <summary>
                /// warning conditions
                /// </summary>
                public static readonly int LOG_WARNING = 4;

                /// <summary>
                /// normal but significant condition
                /// </summary>
                public static readonly int LOG_NOTICE = 5;

                /// <summary>
                /// informational
                /// </summary>
                public static readonly int LOG_INFO = 6;

                /// <summary>
                /// debug-level messages
                /// </summary>
                public static readonly int LOG_DEBUG = 7;

                /// <summary>
                /// kernel messages
                /// </summary>
                public static readonly int LOG_KERN = (0 << 3);

                /// <summary>
                /// random user-level messages
                /// </summary>
                public static readonly int LOG_USER = (1 << 3);

                /// <summary>
                /// mail system
                /// </summary>
                public static readonly int LOG_MAIL = (2 << 3);

                /// <summary>
                /// system daemons
                /// </summary>
                public static readonly int LOG_DAEMON = (3 << 3);

                /// <summary>
                /// security/authorization messages
                /// </summary>
                public static readonly int LOG_AUTH = (4 << 3);

                /// <summary>
                /// messages generated internally by syslogd
                /// </summary>
                public static readonly int LOG_SYSLOG = (5 << 3);

                /// <summary>
                /// line printer subsystem
                /// </summary>
                public static readonly int LOG_LPR = (6 << 3);

                /// <summary>
                /// network news subsystem
                /// </summary>
                public static readonly int LOG_NEWS = (7 << 3);

                /// <summary>
                /// UUCP subsystem
                /// </summary>
                public static readonly int LOG_UUCP = (8 << 3);

                /// <summary>
                /// clock daemon
                /// </summary>
                public static readonly int LOG_CRON = (9 << 3);

                /// <summary>
                /// security/authorization  messages (private)
                /// </summary>
                public static readonly int LOG_AUTHPRIV = (10 << 3);

                /// <summary>
                /// ftp daemon
                /// </summary>
                public static readonly int LOG_FTP = (11 << 3);

                /// <summary>
                /// reserved for local use
                /// </summary>
                public static readonly int LOG_LOCAL0 = (16 << 3);

                /// <summary>
                /// reserved for local use
                /// </summary>
                public static readonly int LOG_LOCAL1 = (17 << 3);

                /// <summary>
                /// reserved for local use
                /// </summary>
                public static readonly int LOG_LOCAL2 = (18 << 3);

                /// <summary>
                /// reserved for local use
                /// </summary>
                public static readonly int LOG_LOCAL3 = (19 << 3);

                /// <summary>
                /// reserved for local use
                /// </summary>
                public static readonly int LOG_LOCAL4 = (20 << 3);

                /// <summary>
                /// reserved for local use
                /// </summary>
                public static readonly int LOG_LOCAL5 = (21 << 3);

                /// <summary>
                /// reserved for local use
                /// </summary>
                public static readonly int LOG_LOCAL6 = (22 << 3);

                /// <summary>
                /// reserved for local use
                /// </summary>
                public static readonly int LOG_LOCAL7 = (23 << 3);

                #endregion Public Static Fields

                #region Protected Static Fields

                /// <summary>
                /// syslog port
                /// </summary>
                protected static int SYSLOG_PORT = 514;

                #endregion Protected Static Fields

                #region Private Instances Fields

                // default to LOG_USER
                int facility = LOG_USER;

                #endregion Private Instances Fields
        }
}

Reply via email to