Skip to content

Commit 970292b

Browse files
committed
Fix bugs with processing partial messages.
Actually use MessageTemplate config item.
1 parent 1d01f77 commit 970292b

File tree

3 files changed

+53
-12
lines changed

3 files changed

+53
-12
lines changed

src/SyslogProxy/Messages/JsonSyslogMessage.cs

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace SyslogProxy.Messages
22
{
3+
using System;
34
using System.Linq;
45
using System.Text.RegularExpressions;
56

@@ -9,23 +10,46 @@ public class JsonSyslogMessage
910
{
1011
private static readonly Regex DateStampRegex = new Regex(@"\w{3} \w{3}.{4}\d{2}:\d{2}:\d{2}.\d{3}");
1112

12-
public JsonSyslogMessage(string syslogLine)
13+
public JsonSyslogMessage(string rawMessage)
1314
{
14-
var splitLine = syslogLine.Split(' ');
15+
this.RawMessage = rawMessage;
1516

16-
var priority = int.Parse(splitLine[0]);
17-
var facility = priority / 8;
18-
var severity = priority % 8;
17+
var splitLine = rawMessage.Split(' ');
18+
if (splitLine.Length < 4)
19+
{
20+
this.Invalid = true;
21+
return;
22+
}
23+
24+
try
25+
{
26+
var priority = int.Parse(splitLine[0]);
27+
var facility = priority / 8;
28+
var severity = priority % 8;
1929

20-
this.Facility = ((Facility) facility).ToString();
21-
this.Level = ((Severity) severity).ToString();
30+
this.Facility = ((Facility)facility).ToString();
31+
this.Level = ((Severity)severity).ToString();
32+
}
33+
catch (Exception)
34+
{
35+
Console.WriteLine("Could not parse priority. [{0}]", rawMessage);
36+
this.Invalid = true;
37+
return;
38+
}
39+
40+
DateTime notUsed;
41+
this.Invalid = !DateTime.TryParse(splitLine[1], out notUsed);
2242

2343
this.Timestamp = splitLine[1];
2444
this.Hostname = splitLine[2].Trim();
2545
this.ApplicationName = splitLine[3].Trim();
2646
this.Message = DateStampRegex.Replace(string.Join(" ", splitLine.Skip(4)).Trim(), string.Empty).Trim();
2747
}
2848

49+
public bool Invalid { get; private set; }
50+
51+
public string RawMessage { get; private set; }
52+
2953
public string Timestamp { get; set; }
3054

3155
public string Level { get; set; }
@@ -44,7 +68,7 @@ public override string ToString()
4468
{
4569
Level = this.Level,
4670
Timestamp = this.Timestamp,
47-
MessageTemplate = "{Hostname}:{ApplicationName} {Message}",
71+
MessageTemplate = Configuration.MessageTemplate,
4872
Properties = new { this.Facility, this.Hostname, this.ApplicationName, this.Message }
4973
});
5074
}

src/SyslogProxy/Proxy.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace SyslogProxy
22
{
33
using System;
4+
using System.Linq;
45
using System.Net;
56
using System.Net.Sockets;
67
using System.Text;
@@ -12,6 +13,8 @@ public class Proxy
1213
{
1314
private readonly Action<string> messageHandler;
1415

16+
private const int BufferSize = 2048;
17+
1518
public Proxy(Action<string> messageHandler)
1619
{
1720
this.messageHandler = messageHandler;
@@ -35,11 +38,13 @@ private async Task EchoAsync(TcpClient client)
3538
Console.WriteLine("New client connected.");
3639
using (client)
3740
{
38-
var buf = new byte[4096];
3941
var stream = client.GetStream();
42+
var buf = new byte[BufferSize];
43+
var accumulator = new StringBuilder();
4044
while (true)
4145
{
4246
var timeoutTask = Task.Delay(TimeSpan.FromSeconds(Configuration.TcpConnectionTimeout));
47+
Array.Clear(buf, 0, BufferSize);
4348
var amountReadTask = stream.ReadAsync(buf, 0, buf.Length);
4449
var completedTask = await Task.WhenAny(timeoutTask, amountReadTask)
4550
.ConfigureAwait(false);
@@ -54,8 +59,14 @@ private async Task EchoAsync(TcpClient client)
5459
{
5560
break;
5661
}
57-
58-
this.messageHandler(Encoding.UTF8.GetString(buf).TrimEnd('\0'));
62+
accumulator.Append(Encoding.UTF8.GetString(buf).TrimEnd('\0'));
63+
if (accumulator.ToString().Contains("\n"))
64+
{
65+
var splitMessage = accumulator.ToString().Split('\n').ToList();
66+
accumulator = new StringBuilder(splitMessage.Last());
67+
splitMessage.RemoveAt(splitMessage.Count - 1);
68+
splitMessage.ForEach(this.messageHandler);
69+
}
5970
}
6071
}
6172
Console.WriteLine("Client disconnected");

src/SyslogProxy/SeqWriter.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ public class SeqWriter
1414

1515
public async Task WriteToSeq(JsonSyslogMessage message, int delay = 0)
1616
{
17+
if (message.Invalid)
18+
{
19+
Console.WriteLine("Skipping incomplete/invalid message. [{0}]", message.RawMessage);
20+
return;
21+
}
22+
1723
await Task.Delay(Math.Min(delay, 60000));
1824

1925
ExceptionDispatchInfo capturedException = null;
@@ -30,7 +36,7 @@ public async Task WriteToSeq(JsonSyslogMessage message, int delay = 0)
3036
{
3137
Console.Write("Couldn't write to SEQ. Exception: [{0}]", capturedException.SourceException.Message);
3238
this.retryCount++;
33-
await this.WriteToSeq(message, 100 ^ this.retryCount);
39+
await this.WriteToSeq(message, (int)Math.Pow(100, this.retryCount));
3440
}
3541
}
3642

0 commit comments

Comments
 (0)