简体   繁体   English

Serilog TcpSink 不抛出异常

[英]Serilog TcpSink Not throwing exceptions

Is there any way to track if end-point is available for Tcp Sink logging?有什么方法可以跟踪端点是否可用于 Tcp 接收器日志记录? For example locally on my machine I do not have FileBeat setup, while its working on Staging machine.例如,在我的机器上本地我没有 FileBeat 设置,而它在 Staging 机器上工作。

The way I initialize Logger我初始化 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.就我而言,当它在本地提交 json 字符串时,它无处可去,我想得到一个例外/消息。

ideally on json submit, but during initialization is Ok.理想情况下在 json 上提交,但在初始化期间是可以的。

Writing to a Serilog logger is meant to be a safe operation and never throw exceptions, and that's by design .写入 Serilog 记录器是一种安全操作,绝不会抛出异常,这是设计使然。 Thus any exceptions that happen when sending those messages would only appear in the SelfLog - if you enable it.因此,发送这些消息时发生的任何异常只会出现在SelfLog中 - 如果您启用它。

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.当然,上面的示例只是为了说明SelfLog功能...您将选择在何处/如何显示或存储这些错误消息。


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(...)现在,如果您正在记录的操作非常重要,以至于您希望保证它成功(或者如果没有成功则抛出异常),那么您应该使用Audit Logging ,即使用.AuditTo.TCPSink(...)代替.WriteTo.TCPSink(...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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