init
This commit is contained in:
42
DouyinApi.Serilog.Es/Sinks/UDP/UDPSink.cs
Normal file
42
DouyinApi.Serilog.Es/Sinks/UDP/UDPSink.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using Serilog.Core;
|
||||
using Serilog.Events;
|
||||
using Serilog.Formatting;
|
||||
|
||||
|
||||
namespace Serilog.Sinks.Network.Sinks.UDP
|
||||
{
|
||||
public class UDPSink : ILogEventSink, IDisposable
|
||||
{
|
||||
private Socket _socket = new Socket(SocketType.Dgram, ProtocolType.Udp);
|
||||
private readonly ITextFormatter _formatter;
|
||||
|
||||
public UDPSink(IPAddress ipAddress, int port, ITextFormatter formatter)
|
||||
{
|
||||
_socket.Connect(ipAddress, port);
|
||||
_formatter = formatter;
|
||||
}
|
||||
|
||||
public void Emit(LogEvent logEvent)
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
|
||||
using (var sw = new StringWriter(sb))
|
||||
_formatter.Format(logEvent, sw);
|
||||
|
||||
sb.Replace("RenderedMessage", "message");
|
||||
|
||||
_socket.Send(Encoding.UTF8.GetBytes(sb.ToString()));
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_socket?.Dispose();
|
||||
_socket = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user