forked from serilog-contrib/serilog-sinks-graylog
-
Notifications
You must be signed in to change notification settings - Fork 1
/
GraylogSink.cs
49 lines (43 loc) · 1.46 KB
/
GraylogSink.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
using System;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using Serilog.Core;
using Serilog.Debugging;
using Serilog.Events;
using Serilog.Sinks.Graylog.Core;
using Serilog.Sinks.Graylog.Core.Transport;
namespace Serilog.Sinks.Graylog
{
public class GraylogSink : ILogEventSink, IDisposable
{
private readonly Lazy<IGelfConverter> _converter;
private readonly Lazy<ITransport> _transport;
public GraylogSink(GraylogSinkOptions options)
{
ISinkComponentsBuilder sinkComponentsBuilder = new SinkComponentsBuilder(options);
_transport = new Lazy<ITransport>(() => sinkComponentsBuilder.MakeTransport());
_converter = new Lazy<IGelfConverter>(() => sinkComponentsBuilder.MakeGelfConverter());
}
public void Dispose()
{
_transport.Value.Dispose();
}
public async void Emit(LogEvent logEvent)
{
try
{
await EmitAsync(logEvent).ConfigureAwait(false);
}
catch (Exception exc)
{
SelfLog.WriteLine("Oops something going wrong {0}", exc);
}
}
private Task EmitAsync(LogEvent logEvent)
{
JObject json = _converter.Value.GetGelfJson(logEvent);
string payload = json.ToString(Newtonsoft.Json.Formatting.None);
return _transport.Value.Send(payload);
}
}
}