-
Notifications
You must be signed in to change notification settings - Fork 3
/
SysLogMessage.cs
170 lines (148 loc) · 5.19 KB
/
SysLogMessage.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
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
using Jakkl.Syslog;
using Jakkl.Syslog.Serialization;
using Jakkl.Syslog.Transport;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Text;
namespace Jakkl
{
internal class Options
{
// The host name. If not set, defaults to the NetBIOS name of the local machine
public string LocalHostName { get; set; } = Environment.MachineName;
// The application name
public string AppName { get; set; }
// The process identifier
public string ProcId { get; set; }
// The message type (called msgId in spec)
public string MsgType { get; set; }
// The message
public string Message { get; set; }
// Host name of the syslog server
public string SyslogServerHostname { get; set; }
// The syslog server port
public int SyslogServerPort { get; set; } = 514;
// The version of syslog protocol to use. Possible values are '3164' and '5424' (from corresponding RFC documents) or 'local' to send messages to a local syslog (only on Linux or OS X). Default is '5424'
public string SyslogVersion { get; set; } = "5424";
// The network protocol to use. Possible values are 'tcp' or 'udp' to send to a remote syslog server, or 'local' to send to a local syslog over Unix sockets (only on Linux or OS X). Default is 'tcp'. Note: TCP always uses SSL connection.
public string NetworkProtocol { get; set; } = "tcp";
// Optional path to a CA certificate used to verify Syslog server certificate when using TCP protocol
public string CACertPath { get; set; }
}
public class SysLogMessage
{
public static string SyslogServer { get; set; }
public static int Port { get; set; }
public static string Host { get; set; }
public static Int32 MaxLength = 2048;
public static EventLogEntry Eventlog { get; set;}
public enum Syslog_Facility
{
kern,
user,
mail,
daemon,
auth,
syslog,
lpr,
news,
uucp,
clock,
authpriv,
ftp,
ntp,
logaudit,
logalert,
cron,
local0,
local1,
local2,
local3,
local4,
local5,
local6,
local7,
}
public enum Syslog_Severity
{
Emergency,
Alert,
Critical,
Error,
Warning,
Notice,
Informational,
Debug
}
public enum Syslog_Protocol
{
UDP,
TCP,
TCPwithTLS
}
/// <summary>
/// Send event to Syslog
/// </summary>
/// <returns></returns>
public bool SendLog(string _server,string _port, string msg)
{
Options options = new Options();
options.SyslogServerHostname = _server;
options.SyslogServerPort = Convert.ToInt32(_port);
options.Message = msg;
options.NetworkProtocol = "tcp";
try
{
ISyslogMessageSerializer serializer = (ISyslogMessageSerializer)new SyslogRfc5424MessageSerializer();
ISyslogMessageSender sender = (ISyslogMessageSender)new SyslogTcpSender(options.SyslogServerHostname, options.SyslogServerPort, true);
SyslogMessage msg1 = CreateSyslogMessage(options);
File.AppendAllText("C:\\Jakkl_log.txt", msg1.Facility.ToString() + msg1.Severity.ToString());
sender.Send(msg1, serializer);
return true;
}
catch (Exception e)
{
Debug.WriteLine("ArgumentNullException: {0}", e);
File.AppendAllText("C:\\Jakkl_log.txt", e.Message);
return false;
}
}
public bool TestConnect(string _server, string _port)
{
try
{
TcpClient client = new TcpClient(_server, Convert.ToInt32(_port));
if (client.Connected)
return true;
return false;
}
catch (ArgumentNullException e)
{
Debug.WriteLine("ArgumentNullException: {0}", e);
return false;
}
catch (SocketException e)
{
Debug.WriteLine("SocketException: {0}", e);
return false;
}
}
private static SyslogMessage CreateSyslogMessage(Options options)
{
return new SyslogMessage(
DateTimeOffset.Now,
Facility.LogAlert,
Severity.Informational,
options.LocalHostName ?? Environment.MachineName,
options.AppName,
options.ProcId ?? "3104",
options.MsgType ?? "ID47",
options.Message ?? "Test message at " + DateTime.Now);
}
}
}