serilog Debugging and Diagnostics Debugging and Diagnostics TextWriter Class 捕获到的错误

When Serilog is not behaving as you expect, this may be caused by an internal exception or configuration issue. Here are a couple of ways to sort things out.

SelfLog

First, Serilog will write simple diagnostic messages to user-specified output if provided. Call SelfLog.Enable() at program startup:

Serilog.Debugging.SelfLog.Enable(msg => Debug.WriteLine(msg));

The system console, a file or an in-memory StringWriter can all be used to collect Serilog's output by providing a TextWriter instead of a delegate:

Serilog.Debugging.SelfLog.Enable(Console.Error);   
Console的Error属性,public static System.IO.TextWriter Error { get; }

Serilog never writes its own events to user-defined sinks.

Warning: SelfLog does not perform any synchronization over the provided TextWriter. For most implementations you should use the TextWriter.Synchronized() method to ensure the object being passed in can be written from multiple threads:

var file = File.CreateText(...);
Serilog.Debugging.SelfLog.Enable(TextWriter.Synchronized(file));
static Action<string> _output;

https://github.com/serilog/serilog/blob/dev/src/Serilog/Debugging/SelfLog.cs#L71

 /// <summary>
        /// Set the output mechanism for self-log messages.
        /// </summary>
        /// <param name="output">An action to invoke with self-log messages.</param>
        /// // ReSharper disable once MemberCanBePrivate.Global
        /// <exception cref="ArgumentNullException">When <paramref name="output"/> is <code>null</code></exception>
        public static void Enable(Action<string> output)
        {
            _output = output ?? throw new ArgumentNullException(nameof(output));
        }

https://github.com/serilog/serilog/blob/dev/src/Serilog/Debugging/SelfLog.cs#L54

这个方法调用了上面的Enable方法,这个方法的内部自己构造了匿名委托

      /// <summary>
        /// Set the output mechanism for self-log messages.
        /// </summary>
        /// <param name="output">A synchronized <see cref="TextWriter"/> to which
        /// self-log messages will be written.</param>
        /// <exception cref="ArgumentNullException">When <paramref name="output"/> is <code>null</code></exception>
        // ReSharper disable once MemberCanBePrivate.Global
        public static void Enable(TextWriter output)
        {
            if (output == null) throw new ArgumentNullException(nameof(output));

            Enable(m =>
            {
                output.WriteLine(m);
                output.Flush();
            });
        }

https://github.com/serilog/serilog/blob/dev/src/Serilog/Debugging/SelfLog.cs#L96

如果output是空的话,selflog就不会写了

 /// <summary>
        /// Write a message to the self-log.
        /// </summary>
        /// <param name="format">Standard .NET format string containing the message.</param>
        /// <param name="arg0">First argument, if supplied.</param>
        /// <param name="arg1">Second argument, if supplied.</param>
        /// <param name="arg2">Third argument, if supplied.</param>
        /// <remarks>
        /// The name is historical; because this is used from third-party sink packages, removing the "Line"
        /// suffix as would seem sensible isn't worth the breakage.
        /// </remarks>
        public static void WriteLine(string format, object arg0 = null, object arg1 = null, object arg2 = null)
        {
            var o = _output;

            o?.Invoke(string.Format(DateTime.UtcNow.ToString("o") + " " + format, arg0, arg1, arg2));
        }

TextWriter Class

Inheritance
Derived
Implements

捕获到的错误

elasticserach的uri无效的 

2020-07-10T09:58:01.2784651Z Caught exception while preforming bulk operation to Elasticsearch: Elasticsearch.Net.ElasticsearchClientException: Maximum timeout reached while retrying request. Call: Status code unknown from: POST /_bulk ---> System.Net.WebException: The operation has timed out
at System.Net.HttpWebRequest.GetRequestStream(TransportContext& context)
at System.Net.HttpWebRequest.GetRequestStream()
at Elasticsearch.Net.HttpWebRequestConnection.Request[TResponse](RequestData requestData)
--- End of inner exception stack trace ---
at Elasticsearch.Net.Transport`1.HandleElasticsearchClientException(RequestData data, Exception clientException, IElasticsearchResponse response)
at Elasticsearch.Net.Transport`1.FinalizeResponse[TResponse](RequestData requestData, IRequestPipeline pipeline, List`1 seenExceptions, TResponse response)
at Elasticsearch.Net.Transport`1.Request[TResponse](HttpMethod method, String path, PostData data, IRequestParameters requestParameters)
at Elasticsearch.Net.ElasticLowLevelClient.DoRequest[TResponse](HttpMethod method, String path, PostData data, IRequestParameters requestParameters)
at Elasticsearch.Net.ElasticLowLevelClient.Bulk[TResponse](PostData body, BulkRequestParameters requestParameters)
at Serilog.Sinks.Elasticsearch.ElasticsearchSink.EmitBatchChecked[T](IEnumerable`1 events)
at Serilog.Sinks.Elasticsearch.ElasticsearchSink.EmitBatch(IEnumerable`1 events)