简体   繁体   中英

Serilog TcpSink Not throwing exceptions

Is there any way to track if end-point is available for Tcp Sink logging? For example locally on my machine I do not have FileBeat setup, while its working on Staging machine.

The way I initialize Logger

private readonly ILogger _tcpLogger;


public TcpClient(IOptions<ElasticSearchConfig> tcpClientConfig)
    {
        var ip = IPAddress.Parse(tcpClientConfig.Value.TcpClientConfig.IpAddress);

        _tcpLogger = new LoggerConfiguration()
                     .WriteTo.TCPSink(ip, tcpClientConfig.Value.TcpClientConfig.Port, new TcpOutputFormatter())
                     .CreateLogger();
    }

and simple method just to submit log

public void SubmitLog(string json) 
      { 
      _tcpLogger.Information(json);
      }

And in my case when its submitting json string locally, it just goes nowhere and I would like to get an exeption/message back.

ideally on json submit, but during initialization is Ok.

Writing to a Serilog logger is meant to be a safe operation and never throw exceptions, and that's by design . Thus any exceptions that happen when sending those messages would only appear in the SelfLog - if you enable it.

eg

// Write Serilog errors to the Console
Serilog.Debugging.SelfLog.Enable(msg => Console.WriteLine(msg));

The example above is, of course, just to illustrate the SelfLog feature... You'll choose where/how to display or store these error messages.


Now, if the operation you're logging is important enough that you'd want to guarantee it succeeds (or throws exception if it doesn't) then you should use Audit Logging , ie Use .AuditTo.TCPSink(...) instead of .WriteTo.TCPSink(...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM