serilog Debugging and Diagnostics

Debugging and Diagnostics

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)

 

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(418)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2019-07-10 your current language level is ecmascript 5
2019-07-10 How to correctly use preventDefault(), stopPropagation(), or return false; on events
2019-07-10 var this=(this)
2019-07-10 jQuery .prop()
2019-07-10 What is the most efficient way to deep clone an object in JavaScript?
2019-07-10 Is JavaScript a pass-by-reference or pass-by-value language?
2019-07-10 Removing jQuery from GitHub.com frontend
点击右上角即可分享
微信分享提示