123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607 |
- #region Apache License
- #endregion
- #if !NETCF && !SSCLI
- using System;
- using System.Runtime.InteropServices;
- using log4net.Core;
- using log4net.Appender;
- using log4net.Util;
- using log4net.Layout;
- namespace log4net.Appender
- {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public class LocalSyslogAppender : AppenderSkeleton
- {
- #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 LocalSyslogAppender()
- {
- }
- #endregion // Public Instance Constructors
- #region Public Instance Properties
-
-
-
-
-
-
-
-
-
-
-
-
- public string 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 IOptionHandler Implementation
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- #if NET_4_0 || MONO_4_0 || NETSTANDARD1_3
- [System.Security.SecuritySafeCritical]
- #endif
- public override void ActivateOptions()
- {
- base.ActivateOptions();
-
- m_levelMapping.ActivateOptions();
- string identString = m_identity;
- if (identString == null)
- {
-
- identString = SystemInfo.ApplicationFriendlyName;
- }
-
-
-
- m_handleToIdentity = Marshal.StringToHGlobalAnsi(identString);
-
- openlog(m_handleToIdentity, 1, m_facility);
- }
- #endregion // IOptionHandler Implementation
- #region AppenderSkeleton Implementation
-
-
-
-
-
-
-
-
-
-
-
-
- #if NET_4_0 || MONO_4_0 || NETSTANDARD1_3
- [System.Security.SecuritySafeCritical]
- #endif
- #if !NETSTANDARD1_3
- [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand, UnmanagedCode = true)]
- #endif
- protected override void Append(LoggingEvent loggingEvent)
- {
- int priority = GeneratePriority(m_facility, GetSeverity(loggingEvent.Level));
- string message = RenderLoggingEvent(loggingEvent);
-
-
- syslog(priority, "%s", message);
- }
-
-
-
-
-
-
-
-
- #if NET_4_0 || MONO_4_0 || NETSTANDARD1_3
- [System.Security.SecuritySafeCritical]
- #endif
- protected override void OnClose()
- {
- base.OnClose();
- try
- {
-
- closelog();
- }
- catch(DllNotFoundException)
- {
-
- }
-
- if (m_handleToIdentity != IntPtr.Zero)
- {
-
- Marshal.FreeHGlobal(m_handleToIdentity);
- }
- }
-
-
-
-
-
-
-
-
-
- override protected bool RequiresLayout
- {
- get { return true; }
- }
- #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
-
-
-
-
-
-
- private static int GeneratePriority(SyslogFacility facility, SyslogSeverity severity)
- {
- return ((int)facility * 8) + (int)severity;
- }
- #endregion // Public Static Members
- #region Private Instances Fields
-
-
-
- private SyslogFacility m_facility = SyslogFacility.User;
-
-
-
- private string m_identity;
-
-
-
-
-
- private IntPtr m_handleToIdentity = IntPtr.Zero;
-
-
-
- private LevelMapping m_levelMapping = new LevelMapping();
- #endregion // Private Instances Fields
- #region External Members
-
-
-
-
- [DllImport("libc")]
- private static extern void openlog(IntPtr ident, int option, SyslogFacility facility);
-
-
-
-
-
-
-
-
-
-
-
-
- [DllImport("libc", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.Cdecl)]
- private static extern void syslog(int priority, string format, string message);
-
-
-
- [DllImport("libc")]
- private static extern void closelog();
- #endregion // External Members
- #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
- }
- }
- #endif
|