-
Notifications
You must be signed in to change notification settings - Fork 3
/
LeAppender.cs
157 lines (138 loc) · 4.75 KB
/
LeAppender.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
/*
Logentries Log4Net Logging agent
Copyright 2010,2011 Logentries, Jlizard
Mark Lacomber <[email protected]>
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Security;
using System.Net.Sockets;
using System.IO;
using log4net.Layout;
using log4net.Core;
using log4net.Util;
using log4net.Appender;
namespace log4net.Appender
{
public class LeAppender : AppenderSkeleton
{
#region Private Instance Fields
private SslStream sslSock = null;
private TcpClient leSocket = null;
private System.Text.ASCIIEncoding encoding;
private String m_Key;
private String m_Location;
private bool m_Debug;
#endregion
#region Public Instance Properties
public string Key
{
get { return m_Key; }
set { m_Key = value; }
}
public string Location
{
get { return m_Location; }
set { m_Location = value; }
}
public bool Debug
{
get { return m_Debug; }
set { m_Debug = value; }
}
#endregion
#region Constructor
public LeAppender()
{
}
#endregion
private void createSocket(String key, String location)
{
this.encoding = new System.Text.ASCIIEncoding();
this.leSocket = new TcpClient("api.logentries.com", 443);
this.leSocket.NoDelay = true;
this.sslSock = new SslStream(this.leSocket.GetStream());
this.sslSock.AuthenticateAsClient("logentries.com");
String output = "PUT /" + key + "/hosts/" + location + "/?realtime=1 HTTP/1.1\r\n";
this.sslSock.Write(this.encoding.GetBytes(output), 0, output.Length);
output = "Host: api.logentries.com\r\n";
this.sslSock.Write(this.encoding.GetBytes(output), 0, output.Length);
output = "Accept-Encoding: identity\r\n";
this.sslSock.Write(this.encoding.GetBytes(output), 0, output.Length);
output = "Transfer_Encoding: chunked\r\n\r\n";
this.sslSock.Write(this.encoding.GetBytes(output), 0, output.Length);
}
protected override void Append(LoggingEvent loggingEvent)
{
if (this.sslSock == null)
{
try
{
this.createSocket(this.Key, this.Location);
}
catch (Exception e)
{
if (this.Debug == true)
{
Console.Error.WriteLine("Error connecting to Logentries");
Console.Error.WriteLine(e.ToString());
}
}
}
String final = RenderLoggingEvent(loggingEvent) + "\r\n";
try
{
this.sslSock.Write(this.encoding.GetBytes(final), 0, final.Length);
}
catch (Exception e)
{
if (this.Debug == true)
{
Console.Error.WriteLine("Error sending log to logentries");
Console.Error.WriteLine(e.ToString());
}
try
{
this.createSocket(this.Key, this.Location);
this.sslSock.Write(this.encoding.GetBytes(final), 0, final.Length);
}
catch (Exception ex)
{
if (this.Debug == true)
{
Console.Error.WriteLine("Error sending log to Logentries");
Console.Error.WriteLine(ex.ToString());
}
}
}
}
protected override void Append(LoggingEvent[] loggingEvents)
{
if (this.sslSock == null)
{
try
{
this.createSocket(this.Key, this.Location);
}
catch (SocketException e)
{
if (this.Debug == true)
{
Console.Error.WriteLine("Error connecting to Logentries");
Console.Error.WriteLine(e.ToString());
}
}
}
foreach (LoggingEvent logEvent in loggingEvents)
{
this.Append(logEvent);
}
}
protected override bool RequiresLayout
{
get { return true; }
}
}
}