-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFluentdCommand.cs
55 lines (45 loc) · 1.56 KB
/
FluentdCommand.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
50
51
52
53
54
55
using Serilog;
using Serilog.Core;
using Serilog.Sinks.Fluentd;
using System;
using System.Net.Sockets;
namespace Benner.NoSQLRepository
{
public class FluentdCommand : IDisposable
{
private readonly Logger _log;
public FluentdCommand(FluentdOptions options)
{
if (options == null)
throw new ArgumentNullException(nameof(options));
if (string.IsNullOrWhiteSpace(options.Host))
throw new InvalidOperationException("A propriedade 'fluentd:host' deve ser informada");
if(string.IsNullOrWhiteSpace(options.Tag))
throw new InvalidOperationException("A propriedade 'fluentd:Tag' deve ser informada");
try
{
using (var tcpClient = new TcpClient())
tcpClient.Connect(options.Host, options.Port);
}
catch (SocketException e)
{
throw new InvalidOperationException($"A conexão com o FluentD no host {options.Host}:{options.Port} falhou", e);
}
var sinkOpts = new FluentdSinkOptions(options.Host, options.Port, options.Tag);
_log = new LoggerConfiguration().WriteTo.Fluentd(sinkOpts).CreateLogger();
}
public void Write(object record)
{
_log.Information("{@record}", record);
}
public void Dispose()
{
//flush and close fluentd tcp connection
_log?.Dispose();
}
~FluentdCommand()
{
Dispose();
}
}
}