zoukankan      html  css  js  c++  java
  • 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)

  • 相关阅读:
    尝试使用word发布博客
    设计模式学习系列7 建造者模式
    设计模式学习系列6 原型模式(prototype)
    最近比较闲
    提高程序运行效率的10个简单方法(转)
    设计模式学习系列5 工厂模式
    【LINUX/UNIX网络编程】之使用消息队列,信号量和命名管道实现的多进程服务器(多人群聊系统)
    三十分钟掌握STL
    在python包管理中使用easy_install软件的步骤
    【转】推荐给大家的7本游戏开发书
  • 原文地址:https://www.cnblogs.com/chucklu/p/13280183.html
Copyright © 2011-2022 走看看