-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSyslog.cs
68 lines (57 loc) · 1.44 KB
/
Syslog.cs
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
using System;
using System.Runtime.InteropServices;
namespace Jitbit.Utils
{
public class Syslog
{
[Flags]
private enum Option
{
Pid = 0x01,
Console = 0x02,
Delay = 0x04,
NoDelay = 0x08,
NoWait = 0x10,
PrintError = 0x20
}
[Flags]
private enum Facility
{
User = 1 << 3, //removed other unused enum values for brevity
}
[Flags]
public enum Level
{
Emerg = 0,
Alert = 1,
Crit = 2,
Err = 3,
Warning = 4,
Notice = 5,
Info = 6,
Debug = 7
}
[DllImport("libc")]
private static extern void openlog(IntPtr ident, Option option, Facility facility);
[DllImport("libc")]
private static extern void syslog(int priority, string message);
[DllImport("libc")]
private static extern void closelog();
public static void Write(Syslog.Level level, string identity, string message)
{
//are we on linux?
if (!OperatingSystem.IsLinux()) return;
//validate input
if (string.IsNullOrWhiteSpace(message) && string.IsNullOrWhiteSpace(identity)) return;
IntPtr ident = Marshal.StringToHGlobalAnsi(identity);
openlog(ident, Option.Console | Option.Pid | Option.PrintError, Facility.User);
//split multiline messages, otherwise we end up with "line1 #012 line2 #012 line3" etc
foreach (var line in message.Split('\n', StringSplitOptions.RemoveEmptyEntries))
{
syslog((int)Facility.User | (int)level, line.Trim());
}
closelog();
Marshal.FreeHGlobal(ident);
}
}
}