没有可用服务器时的 Serilog 和 seq [英] Serilog and seq when no server available

查看:80
本文介绍了没有可用服务器时的 Serilog 和 seq的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用 Serilog 和 Seq 的应用程序找不到要将日志发送到的服务器时,预期的行为是什么?每次尝试记录都会抛出异常吗?

如果 Seq 服务器可用,我希望我的应用使用它,但如果它不可用,我仍继续运行并记录到文件.

解决方案

当使用 Serilog 和 Seq 的应用程序找不到将日志发送到的服务器时,预期的行为是什么?

这取决于您使用的是常规接收器(通过 .WriteTo)还是审计接收器(通过 .AuditTo).

写入常规接收器是为了安全操作,从不抛出异常,

What is the expected behavior when an application that uses Serilog and Seq can't find a server to send the logs to? Will every attempt to log throw an exception?

I would like my app to use the Seq server if it's available, but still continue to run and log to file if it is not.

解决方案

What is the expected behavior when an application that uses Serilog and Seq can't find a server to send the logs to?

It depends if your are using a regular sink (via .WriteTo) or an auditing sink (via .AuditTo).

Writing to a regular sink 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.

e.g.

// 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'd choose if/where/how to display or store these error messages.

Writing to an auditing sink, you'd expect to see an error for each message that fails.

Will every attempt to log throw an exception?

It depends on the Sink. In the case of Serilog.Sinks.Seq, it's a periodic batching sink, so it will try to send messages in batches of X log events (X is configurable) so if the Seq server is not available, you'd expect to see an error per each batch that fails to send.

I would like my app to use the Seq server if it's available, but still continue to run and log to file if it is not.

The Seq sink has in-memory buffering and retry support to handle short periods of server or network unavailability, which is usually enough for most applications.

Alternatively, the Seq sink has durable log shipping baked-in, and you can specify a file on disk where it's going to store buffered messages on a file on disk, so it can try again even if your app is restarted.

Log.Logger = new LoggerConfiguration()
    .WriteTo.Seq("https://my-seq-server:5341",
        bufferBaseFilename: @"C:\MyApp\Logs\myapp") // <<<<<<
    .CreateLogger();

Finally, another alternative is to use Seq Forwarder that you can install side-by-side with your app (i.e. same server) and you write the logs directly to it and it takes care of persisting them to its own internal local storage and forwarding them to a remote Seq server when it becomes available.

这篇关于没有可用服务器时的 Serilog 和 seq的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆