Skip to content

Commit 8f5fbb2

Browse files
committed
Implemented Udp Socket logger class
#6
1 parent 851f0d5 commit 8f5fbb2

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
using System.Net.Sockets;
3+
using System.Text;
4+
using Microsoft.Extensions.Logging;
5+
6+
namespace Splunk.Loggers
7+
{
8+
public class UdpLogger : BaseLogger, ILogger
9+
{
10+
readonly UdpClient udpClient;
11+
12+
public UdpLogger(string categoryName, LogLevel threshold, UdpClient udpClient, ILoggerFormatter loggerFormatter)
13+
: base(categoryName, threshold, loggerFormatter)
14+
{
15+
this.udpClient = udpClient;
16+
}
17+
18+
public void Log<T>(LogLevel logLevel, EventId eventId, T state, Exception exception, Func<T, Exception, string> formatter)
19+
{
20+
string formatedMessage = string.Empty;
21+
if (loggerFormatter != null)
22+
formatedMessage = loggerFormatter.Format(logLevel, eventId, state, exception);
23+
else if (formatter != null)
24+
formatedMessage = formatter(state, exception);
25+
26+
if (!string.IsNullOrWhiteSpace(formatedMessage))
27+
{
28+
formatedMessage = formatedMessage + Environment.NewLine;
29+
Byte[] data = Encoding.ASCII.GetBytes(formatedMessage);
30+
udpClient.Send(data, data.Length);
31+
}
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)