123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596 |
- #region Apache License
- #endregion
- using System;
- using log4net.Core;
- using log4net.Appender;
- using log4net.Util;
- using log4net.Layout;
- using System.Text;
- namespace log4net.Appender
- {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public class RemoteSyslogAppender : UdpAppender
- {
-
-
-
- private const int DefaultSyslogPort = 514;
- #region Enumerations
-
-
-
-
-
-
-
-
- public enum SyslogSeverity
- {
-
-
-
- Emergency = 0,
-
-
-
- Alert = 1,
-
-
-
- Critical = 2,
-
-
-
- Error = 3,
-
-
-
- Warning = 4,
-
-
-
- Notice = 5,
-
-
-
- Informational = 6,
-
-
-
- Debug = 7
- };
-
-
-
-
-
-
-
-
- public enum SyslogFacility
- {
-
-
-
- Kernel = 0,
-
-
-
- User = 1,
-
-
-
- Mail = 2,
-
-
-
- Daemons = 3,
-
-
-
- Authorization = 4,
-
-
-
- Syslog = 5,
-
-
-
- Printer = 6,
-
-
-
- News = 7,
-
-
-
- Uucp = 8,
-
-
-
- Clock = 9,
-
-
-
- Authorization2 = 10,
-
-
-
- Ftp = 11,
-
-
-
- Ntp = 12,
-
-
-
- Audit = 13,
-
-
-
- Alert = 14,
-
-
-
- Clock2 = 15,
-
-
-
- Local0 = 16,
-
-
-
- Local1 = 17,
-
-
-
- Local2 = 18,
-
-
-
- Local3 = 19,
-
-
-
- Local4 = 20,
-
-
-
- Local5 = 21,
-
-
-
- Local6 = 22,
-
-
-
- Local7 = 23
- }
- #endregion Enumerations
- #region Public Instance Constructors
-
-
-
-
-
-
-
- public RemoteSyslogAppender()
- {
-
- this.RemotePort = DefaultSyslogPort;
- this.RemoteAddress = System.Net.IPAddress.Parse("127.0.0.1");
- this.Encoding = System.Text.Encoding.ASCII;
- }
- #endregion Public Instance Constructors
- #region Public Instance Properties
-
-
-
-
-
-
-
-
-
-
-
- public PatternLayout Identity
- {
- get { return m_identity; }
- set { m_identity = value; }
- }
-
-
-
-
-
-
-
-
- public SyslogFacility Facility
- {
- get { return m_facility; }
- set { m_facility = value; }
- }
- #endregion Public Instance Properties
-
-
-
-
-
-
-
-
-
- public void AddMapping(LevelSeverity mapping)
- {
- m_levelMapping.Add(mapping);
- }
- #region AppenderSkeleton Implementation
-
-
-
-
-
-
-
-
-
-
-
-
- protected override void Append(LoggingEvent loggingEvent)
- {
- try
- {
-
- int priority = GeneratePriority(m_facility, GetSeverity(loggingEvent.Level));
-
- string identity;
- if (m_identity != null)
- {
- identity = m_identity.Format(loggingEvent);
- }
- else
- {
- identity = loggingEvent.Domain;
- }
-
- string message = RenderLoggingEvent(loggingEvent);
- Byte[] buffer;
- int i = 0;
- char c;
- StringBuilder builder = new StringBuilder();
- while (i < message.Length)
- {
-
- builder.Length = 0;
-
- builder.Append('<');
- builder.Append(priority);
- builder.Append('>');
-
- builder.Append(identity);
- builder.Append(": ");
- for (; i < message.Length; i++)
- {
- c = message[i];
-
- if (((int)c >= 32) && ((int)c <= 126))
- {
- builder.Append(c);
- }
-
- else if ((c == '\r') || (c == '\n'))
- {
-
- if ((message.Length > i + 1) && ((message[i + 1] == '\r') || (message[i + 1] == '\n')))
- {
- i++;
- }
- i++;
- break;
- }
- }
-
- buffer = this.Encoding.GetBytes(builder.ToString());
- #if NETSTANDARD1_3
- Client.SendAsync(buffer, buffer.Length, RemoteEndPoint).Wait();
- #else
- this.Client.Send(buffer, buffer.Length, this.RemoteEndPoint);
- #endif
- }
- }
- catch (Exception e)
- {
- ErrorHandler.Error(
- "Unable to send logging event to remote syslog " +
- this.RemoteAddress.ToString() +
- " on port " +
- this.RemotePort + ".",
- e,
- ErrorCode.WriteFailure);
- }
- }
-
-
-
-
-
-
-
-
- public override void ActivateOptions()
- {
- base.ActivateOptions();
- m_levelMapping.ActivateOptions();
- }
- #endregion AppenderSkeleton Implementation
- #region Protected Members
-
-
-
-
-
-
-
-
-
-
- virtual protected SyslogSeverity GetSeverity(Level level)
- {
- LevelSeverity levelSeverity = m_levelMapping.Lookup(level) as LevelSeverity;
- if (levelSeverity != null)
- {
- return levelSeverity.Severity;
- }
-
-
-
- if (level >= Level.Alert)
- {
- return SyslogSeverity.Alert;
- }
- else if (level >= Level.Critical)
- {
- return SyslogSeverity.Critical;
- }
- else if (level >= Level.Error)
- {
- return SyslogSeverity.Error;
- }
- else if (level >= Level.Warn)
- {
- return SyslogSeverity.Warning;
- }
- else if (level >= Level.Notice)
- {
- return SyslogSeverity.Notice;
- }
- else if (level >= Level.Info)
- {
- return SyslogSeverity.Informational;
- }
-
- return SyslogSeverity.Debug;
- }
- #endregion Protected Members
- #region Public Static Members
-
-
-
-
-
-
-
-
-
-
-
- public static int GeneratePriority(SyslogFacility facility, SyslogSeverity severity)
- {
- if (facility < SyslogFacility.Kernel || facility > SyslogFacility.Local7)
- {
- throw new ArgumentException("SyslogFacility out of range", "facility");
- }
- if (severity < SyslogSeverity.Emergency || severity > SyslogSeverity.Debug)
- {
- throw new ArgumentException("SyslogSeverity out of range", "severity");
- }
- unchecked
- {
- return ((int)facility * 8) + (int)severity;
- }
- }
- #endregion Public Static Members
- #region Private Instances Fields
-
-
-
- private SyslogFacility m_facility = SyslogFacility.User;
-
-
-
- private PatternLayout m_identity;
-
-
-
- private LevelMapping m_levelMapping = new LevelMapping();
-
-
-
- private const int c_renderBufferSize = 256;
-
-
-
- private const int c_renderBufferMaxCapacity = 1024;
- #endregion Private Instances Fields
- #region LevelSeverity LevelMapping Entry
-
-
-
-
-
-
-
-
-
-
- public class LevelSeverity : LevelMappingEntry
- {
- private SyslogSeverity m_severity;
-
-
-
-
-
-
-
-
-
- public SyslogSeverity Severity
- {
- get { return m_severity; }
- set { m_severity = value; }
- }
- }
- #endregion // LevelSeverity LevelMapping Entry
- }
- }
|