You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
1.1 KiB
C#
44 lines
1.1 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Net;
|
|
using System.Text;
|
|
using Serilog.Core;
|
|
using Serilog.Events;
|
|
using Serilog.Formatting;
|
|
|
|
namespace Admin.Core.Serilog.Es
|
|
{
|
|
public class TCPSink : ILogEventSink, IDisposable
|
|
{
|
|
private readonly ITextFormatter _formatter;
|
|
private readonly TcpSocketWriter _socketWriter;
|
|
|
|
public TCPSink(IPAddress ipAddress, int port, ITextFormatter formatter)
|
|
{
|
|
_socketWriter = new TcpSocketWriter(new Uri($"tcp://{ipAddress}:{port}"));
|
|
_formatter = formatter;
|
|
}
|
|
|
|
public TCPSink(Uri uri, ITextFormatter formatter)
|
|
{
|
|
_socketWriter = new TcpSocketWriter(uri);
|
|
_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");
|
|
_socketWriter.Enqueue(sb.ToString());
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
_socketWriter.Dispose();
|
|
}
|
|
}
|
|
} |