jt
2021-06-10 5d0d028456874576560552f5a5c4e8b801786f11
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
#region Apache License
//
// Licensed to the Apache Software Foundation (ASF) under one or more 
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership. 
// The ASF licenses this file to you 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.
//
#endregion
 
// .NET Compact Framework 1.0 has no support for Marshal.StringToHGlobalAnsi
// SSCLI 1.0 has no support for Marshal.StringToHGlobalAnsi
#if !NETCF && !SSCLI
 
using System;
using System.Runtime.InteropServices;
 
using log4net.Core;
using log4net.Appender;
using log4net.Util;
using log4net.Layout;
 
namespace log4net.Appender 
{
    /// <summary>
    /// Logs events to a local syslog service.
    /// </summary>
    /// <remarks>
    /// <note>
    /// This appender uses the POSIX libc library functions <c>openlog</c>, <c>syslog</c>, and <c>closelog</c>.
    /// If these functions are not available on the local system then this appender will not work!
    /// </note>
    /// <para>
    /// The functions <c>openlog</c>, <c>syslog</c>, and <c>closelog</c> are specified in SUSv2 and 
    /// POSIX 1003.1-2001 standards. These are used to log messages to the local syslog service.
    /// </para>
    /// <para>
    /// This appender talks to a local syslog service. If you need to log to a remote syslog
    /// daemon and you cannot configure your local syslog service to do this you may be
    /// able to use the <see cref="RemoteSyslogAppender"/> to log via UDP.
    /// </para>
    /// <para>
    /// Syslog messages must have a facility and and a severity. The severity
    /// is derived from the Level of the logging event.
    /// The facility must be chosen from the set of defined syslog 
    /// <see cref="SyslogFacility"/> values. The facilities list is predefined
    /// and cannot be extended.
    /// </para>
    /// <para>
    /// An identifier is specified with each log message. This can be specified
    /// by setting the <see cref="Identity"/> property. The identity (also know 
    /// as the tag) must not contain white space. The default value for the
    /// identity is the application name (from <see cref="SystemInfo.ApplicationFriendlyName"/>).
    /// </para>
    /// </remarks>
    /// <author>Rob Lyon</author>
    /// <author>Nicko Cadell</author>
    public class LocalSyslogAppender : AppenderSkeleton 
    {
        #region Enumerations
 
        /// <summary>
        /// syslog severities
        /// </summary>
        /// <remarks>
        /// <para>
        /// The log4net Level maps to a syslog severity using the
        /// <see cref="LocalSyslogAppender.AddMapping"/> method and the <see cref="LevelSeverity"/>
        /// class. The severity is set on <see cref="LevelSeverity.Severity"/>.
        /// </para>
        /// </remarks>
        public enum SyslogSeverity
        {
            /// <summary>
            /// system is unusable
            /// </summary>
            Emergency = 0,
 
            /// <summary>
            /// action must be taken immediately
            /// </summary>
            Alert = 1,
 
            /// <summary>
            /// critical conditions
            /// </summary>
            Critical = 2,
 
            /// <summary>
            /// error conditions
            /// </summary>
            Error = 3,
 
            /// <summary>
            /// warning conditions
            /// </summary>
            Warning = 4,
 
            /// <summary>
            /// normal but significant condition
            /// </summary>
            Notice = 5,
 
            /// <summary>
            /// informational
            /// </summary>
            Informational = 6,
 
            /// <summary>
            /// debug-level messages
            /// </summary>
            Debug = 7
        };
 
        /// <summary>
        /// syslog facilities
        /// </summary>
        /// <remarks>
        /// <para>
        /// The syslog facility defines which subsystem the logging comes from.
        /// This is set on the <see cref="Facility"/> property.
        /// </para>
        /// </remarks>
        public enum SyslogFacility
        {
            /// <summary>
            /// kernel messages
            /// </summary>
            Kernel = 0,
 
            /// <summary>
            /// random user-level messages
            /// </summary>
            User = 1,
 
            /// <summary>
            /// mail system
            /// </summary>
            Mail = 2,
 
            /// <summary>
            /// system daemons
            /// </summary>
            Daemons = 3,
 
            /// <summary>
            /// security/authorization messages
            /// </summary>
            Authorization = 4,
 
            /// <summary>
            /// messages generated internally by syslogd
            /// </summary>
            Syslog = 5,
 
            /// <summary>
            /// line printer subsystem
            /// </summary>
            Printer = 6,
 
            /// <summary>
            /// network news subsystem
            /// </summary>
            News = 7,
 
            /// <summary>
            /// UUCP subsystem
            /// </summary>
            Uucp = 8,
 
            /// <summary>
            /// clock (cron/at) daemon
            /// </summary>
            Clock = 9,
 
            /// <summary>
            /// security/authorization  messages (private)
            /// </summary>
            Authorization2 = 10,
 
            /// <summary>
            /// ftp daemon
            /// </summary>
            Ftp = 11,
 
            /// <summary>
            /// NTP subsystem
            /// </summary>
            Ntp = 12,
 
            /// <summary>
            /// log audit
            /// </summary>
            Audit = 13,
 
            /// <summary>
            /// log alert
            /// </summary>
            Alert = 14,
 
            /// <summary>
            /// clock daemon
            /// </summary>
            Clock2 = 15,
 
            /// <summary>
            /// reserved for local use
            /// </summary>
            Local0 = 16,
 
            /// <summary>
            /// reserved for local use
            /// </summary>
            Local1 = 17,
 
            /// <summary>
            /// reserved for local use
            /// </summary>
            Local2 = 18,
 
            /// <summary>
            /// reserved for local use
            /// </summary>
            Local3 = 19,
 
            /// <summary>
            /// reserved for local use
            /// </summary>
            Local4 = 20,
 
            /// <summary>
            /// reserved for local use
            /// </summary>
            Local5 = 21,
 
            /// <summary>
            /// reserved for local use
            /// </summary>
            Local6 = 22,
 
            /// <summary>
            /// reserved for local use
            /// </summary>
            Local7 = 23
        }
 
        #endregion // Enumerations
 
        #region Public Instance Constructors
 
        /// <summary>
        /// Initializes a new instance of the <see cref="LocalSyslogAppender" /> class.
        /// </summary>
        /// <remarks>
        /// This instance of the <see cref="LocalSyslogAppender" /> class is set up to write 
        /// to a local syslog service.
        /// </remarks>
        public LocalSyslogAppender() 
        {
        }
 
        #endregion // Public Instance Constructors
 
        #region Public Instance Properties
        
        /// <summary>
        /// Message identity
        /// </summary>
        /// <remarks>
        /// <para>
        /// An identifier is specified with each log message. This can be specified
        /// by setting the <see cref="Identity"/> property. The identity (also know 
        /// as the tag) must not contain white space. The default value for the
        /// identity is the application name (from <see cref="SystemInfo.ApplicationFriendlyName"/>).
        /// </para>
        /// </remarks>
        public string Identity
        {
            get { return m_identity; }
            set { m_identity = value; }
        }
 
        /// <summary>
        /// Syslog facility
        /// </summary>
        /// <remarks>
        /// Set to one of the <see cref="SyslogFacility"/> values. The list of
        /// facilities is predefined and cannot be extended. The default value
        /// is <see cref="SyslogFacility.User"/>.
        /// </remarks>
        public SyslogFacility Facility
        {
            get { return m_facility; }
            set { m_facility = value; }
        }
        
        #endregion // Public Instance Properties
 
        /// <summary>
        /// Add a mapping of level to severity
        /// </summary>
        /// <param name="mapping">The mapping to add</param>
        /// <remarks>
        /// <para>
        /// Adds a <see cref="LevelSeverity"/> to this appender.
        /// </para>
        /// </remarks>
        public void AddMapping(LevelSeverity mapping)
        {
            m_levelMapping.Add(mapping);
        }
 
        #region IOptionHandler Implementation
 
        /// <summary>
        /// Initialize the appender based on the options set.
        /// </summary>
        /// <remarks>
        /// <para>
        /// This is part of the <see cref="IOptionHandler"/> delayed object
        /// activation scheme. The <see cref="ActivateOptions"/> method must 
        /// be called on this object after the configuration properties have
        /// been set. Until <see cref="ActivateOptions"/> is called this
        /// object is in an undefined state and must not be used. 
        /// </para>
        /// <para>
        /// If any of the configuration properties are modified then 
        /// <see cref="ActivateOptions"/> must be called again.
        /// </para>
        /// </remarks>
#if NET_4_0
        [System.Security.SecuritySafeCritical]
#endif
        public override void ActivateOptions()
        {
            base.ActivateOptions();
            
            m_levelMapping.ActivateOptions();
 
            string identString = m_identity;
            if (identString == null)
            {
                // Set to app name by default
                identString = SystemInfo.ApplicationFriendlyName;
            }
 
            // create the native heap ansi string. Note this is a copy of our string
            // so we do not need to hold on to the string itself, holding on to the
            // handle will keep the heap ansi string alive.
            m_handleToIdentity = Marshal.StringToHGlobalAnsi(identString);
 
            // open syslog
            openlog(m_handleToIdentity, 1, m_facility);
        }
 
        #endregion // IOptionHandler Implementation
 
        #region AppenderSkeleton Implementation
 
        /// <summary>
        /// This method is called by the <see cref="AppenderSkeleton.DoAppend(LoggingEvent)"/> 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>
#if NET_4_0
        [System.Security.SecuritySafeCritical]
#endif
        [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand, UnmanagedCode = true)]
        protected override void Append(LoggingEvent loggingEvent) 
        {
            int priority = GeneratePriority(m_facility, GetSeverity(loggingEvent.Level));
            string message = RenderLoggingEvent(loggingEvent);
 
            // Call the local libc syslog method
            // The second argument is a printf style format string
            syslog(priority, "%s", message);
        }
 
        /// <summary>
        /// Close the syslog when the appender is closed
        /// </summary>
        /// <remarks>
        /// <para>
        /// Close the syslog when the appender is closed
        /// </para>
        /// </remarks>
#if NET_4_0
        [System.Security.SecuritySafeCritical]
#endif
        protected override void OnClose()
        {
            base.OnClose();
 
            try
            {
                // close syslog
                closelog();
            }
            catch(DllNotFoundException)
            {
                // Ignore dll not found at this point
            }
        
            if (m_handleToIdentity != IntPtr.Zero)
            {
                // free global ident
                Marshal.FreeHGlobal(m_handleToIdentity);
            }
        }
 
        /// <summary>
        /// This appender requires a <see cref="AppenderSkeleton.Layout"/> to be set.
        /// </summary>
        /// <value><c>true</c></value>
        /// <remarks>
        /// <para>
        /// This appender requires a <see cref="AppenderSkeleton.Layout"/> to be set.
        /// </para>
        /// </remarks>
        override protected bool RequiresLayout
        {
            get { return true; }
        }
 
        #endregion // AppenderSkeleton Implementation
 
        #region Protected Members
 
        /// <summary>
        /// Translates a log4net level to a syslog severity.
        /// </summary>
        /// <param name="level">A log4net level.</param>
        /// <returns>A syslog severity.</returns>
        /// <remarks>
        /// <para>
        /// Translates a log4net level to a syslog severity.
        /// </para>
        /// </remarks>
        virtual protected SyslogSeverity GetSeverity(Level level)
        {
            LevelSeverity levelSeverity = m_levelMapping.Lookup(level) as LevelSeverity;
            if (levelSeverity != null)
            {
                return levelSeverity.Severity;
            }
 
            //
            // Fallback to sensible default values
            //
 
            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;
            } 
            // Default setting
            return SyslogSeverity.Debug;
        }
 
        #endregion // Protected Members
 
        #region Public Static Members
 
        /// <summary>
        /// Generate a syslog priority.
        /// </summary>
        /// <param name="facility">The syslog facility.</param>
        /// <param name="severity">The syslog severity.</param>
        /// <returns>A syslog priority.</returns>
        private static int GeneratePriority(SyslogFacility facility, SyslogSeverity severity)
        {
            return ((int)facility * 8) + (int)severity;
        }
 
        #endregion // Public Static Members
 
        #region Private Instances Fields
 
        /// <summary>
        /// The facility. The default facility is <see cref="SyslogFacility.User"/>.
        /// </summary>
        private SyslogFacility m_facility = SyslogFacility.User;
 
        /// <summary>
        /// The message identity
        /// </summary>
        private string m_identity;
 
        /// <summary>
        /// Marshaled handle to the identity string. We have to hold on to the
        /// string as the <c>openlog</c> and <c>syslog</c> APIs just hold the
        /// pointer to the ident and dereference it for each log message.
        /// </summary>
        private IntPtr m_handleToIdentity = IntPtr.Zero;
 
        /// <summary>
        /// Mapping from level object to syslog severity
        /// </summary>
        private LevelMapping m_levelMapping = new LevelMapping();
 
        #endregion // Private Instances Fields
 
        #region External Members
        
        /// <summary>
        /// Open connection to system logger.
        /// </summary>
        [DllImport("libc")]
        private static extern void openlog(IntPtr ident, int option, SyslogFacility facility);
 
        /// <summary>
        /// Generate a log message.
        /// </summary>
        /// <remarks>
        /// <para>
        /// The libc syslog method takes a format string and a variable argument list similar
        /// to the classic printf function. As this type of vararg list is not supported
        /// by C# we need to specify the arguments explicitly. Here we have specified the
        /// format string with a single message argument. The caller must set the format 
        /// string to <c>"%s"</c>.
        /// </para>
        /// </remarks>
        [DllImport("libc", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.Cdecl)]
        private static extern void syslog(int priority, string format, string message);
 
        /// <summary>
        /// Close descriptor used to write to system logger.
        /// </summary>
        [DllImport("libc")]
        private static extern void closelog();
 
        #endregion // External Members
 
        #region LevelSeverity LevelMapping Entry
 
        /// <summary>
        /// A class to act as a mapping between the level that a logging call is made at and
        /// the syslog severity that is should be logged at.
        /// </summary>
        /// <remarks>
        /// <para>
        /// A class to act as a mapping between the level that a logging call is made at and
        /// the syslog severity that is should be logged at.
        /// </para>
        /// </remarks>
        public class LevelSeverity : LevelMappingEntry
        {
            private SyslogSeverity m_severity;
 
            /// <summary>
            /// The mapped syslog severity for the specified level
            /// </summary>
            /// <remarks>
            /// <para>
            /// Required property.
            /// The mapped syslog severity for the specified level
            /// </para>
            /// </remarks>
            public SyslogSeverity Severity
            {
                get { return m_severity; }
                set { m_severity = value; }
            }
        }
 
        #endregion // LevelSeverity LevelMapping Entry
    }
}
 
#endif