zoukankan      html  css  js  c++  java
  • serilog Getting Started

    Getting Started

    using System;
    using Serilog;
    
    namespace SerilogExample
    {
        class Program
        {
            static void Main()
            {
                Log.Logger = new LoggerConfiguration()
                    .MinimumLevel.Debug()
                    .WriteTo.Console()
                    .WriteTo.File("logs\myapp.txt", rollingInterval: RollingInterval.Day)
                    .CreateLogger();
    
                Log.Information("Hello, world!");
    
                int a = 10, b = 0;
                try
                {
                    Log.Debug("Dividing {A} by {B}", a, b);
                    Console.WriteLine(a / b);
                }
                catch (Exception ex)
                {
                    Log.Error(ex, "Something went wrong");
                }
    
                Log.CloseAndFlush();
                Console.ReadKey();
            }
        }
    }

    Serilog.Settings.AppSettings 

    An XML <appSettings> reader for Serilog

    To read configuration from <appSettings> use the ReadFrom.AppSettings() extension method on your LoggerConfiguration:

    Log.Logger = new LoggerConfiguration()
      .ReadFrom.AppSettings()
      ... // Other configuration here, then
      .CreateLogger()

    You can mix and match XML and code-based configuration, but each sink must be configured either using XML or in code - sinks added in code can't be modified via app settings.

    Configuration syntax

    To configure the logger, an <appSettings> element should be included in the program's App.config or Web.config file.

    https://github.com/serilog/serilog/wiki/AppSettings 

    Serilog supports a simple <appSettings>-based configuration syntax in App.config and Web.config files to set the minimum level, enrich events with additional properties, and control log output.

    Serilog is primarily configured using code, with settings support intended as a supplementary feature. It is not comprehensive but most logger configuration tasks can be achieved using it.

    https://github.com/serilog/serilog-sinks-elasticsearch#quick-start

    elasticsearch的配置在单独的项目里面提到

     <appSettings>
        <add key="serilog:using" value="Serilog.Sinks.Elasticsearch"/>
        <add key="serilog:write-to:Elasticsearch.nodeUris" value="http://localhost:9200;http://remotehost:9200"/>
        <add key="serilog:write-to:Elasticsearch.indexFormat" value="custom-index-{0:yyyy.MM}"/>
        <add key="serilog:write-to:Elasticsearch.templateName" value="myCustomTemplate"/>
        <add key="serilog:write-to:Elasticsearch.typeName" value="myCustomLogEventType"/>
        <add key="serilog:write-to:Elasticsearch.pipelineName" value="myCustomPipelineName"/>
        <add key="serilog:write-to:Elasticsearch.batchPostingLimit" value="50"/>
        <add key="serilog:write-to:Elasticsearch.period" value="2"/>
        <add key="serilog:write-to:Elasticsearch.inlineFields" value="true"/>
        <add key="serilog:write-to:Elasticsearch.restrictedToMinimumLevel" value="Warning"/>
        <add key="serilog:write-to:Elasticsearch.bufferBaseFilename" value="C:TempSerilogElasticBuffer"/>
        <add key="serilog:write-to:Elasticsearch.bufferFileSizeLimitBytes" value="5242880"/>
        <add key="serilog:write-to:Elasticsearch.bufferLogShippingInterval" value="5000"/>
        <add key="serilog:write-to:Elasticsearch.bufferRetainedInvalidPayloadsLimitBytes" value="5000"/>
        <add key="serilog:write-to:Elasticsearch.bufferFileCountLimit " value="31"/>
        <add key="serilog:write-to:Elasticsearch.connectionGlobalHeaders" value="Authorization=Bearer SOME-TOKEN;OtherHeader=OTHER-HEADER-VALUE" />
        <add key="serilog:write-to:Elasticsearch.connectionTimeout" value="5" />
        <add key="serilog:write-to:Elasticsearch.emitEventFailure" value="WriteToSelfLog" />
        <add key="serilog:write-to:Elasticsearch.queueSizeLimit" value="100000" />
        <add key="serilog:write-to:Elasticsearch.autoRegisterTemplate" value="true" />
        <add key="serilog:write-to:Elasticsearch.autoRegisterTemplateVersion" value="ESv2" />
        <add key="serilog:write-to:Elasticsearch.overwriteTemplate" value="false" />
        <add key="serilog:write-to:Elasticsearch.registerTemplateFailure" value="IndexAnyway" />
        <add key="serilog:write-to:Elasticsearch.deadLetterIndexName" value="deadletter-{0:yyyy.MM}" />
        <add key="serilog:write-to:Elasticsearch.numberOfShards" value="20" />
        <add key="serilog:write-to:Elasticsearch.numberOfReplicas" value="10" />
        <add key="serilog:write-to:Elasticsearch.formatProvider" value="My.Namespace.MyFormatProvider, My.Assembly.Name" />
        <add key="serilog:write-to:Elasticsearch.connection" value="My.Namespace.MyConnection, My.Assembly.Name" />
        <add key="serilog:write-to:Elasticsearch.serializer" value="My.Namespace.MySerializer, My.Assembly.Name" />
        <add key="serilog:write-to:Elasticsearch.connectionPool" value="My.Namespace.MyConnectionPool, My.Assembly.Name" />
        <add key="serilog:write-to:Elasticsearch.customFormatter" value="My.Namespace.MyCustomFormatter, My.Assembly.Name" />
        <add key="serilog:write-to:Elasticsearch.customDurableFormatter" value="My.Namespace.MyCustomDurableFormatter, My.Assembly.Name" />
        <add key="serilog:write-to:Elasticsearch.failureSink" value="My.Namespace.MyFailureSink, My.Assembly.Name" />
      </appSettings>

    Watch the config file at runtime

    Try to figure out the feature about watch the App.config at runtime like log4net and NLog
    https://stackoverflow.com/questions/25477415/how-can-i-reconfigure-serilog-without-restarting-the-application
    https://stackoverflow.com/questions/50715913/serilog-equivalent-to-log4net-config-watch
    The above two links have no useful info about reload all settings in App.config.

    Here is another link https://stackoverflow.com/questions/53449596/reload-serilog-json-configuration-on-changes-in-net-core-2-1/60474967#60474967
    The current Serilog implementation (2.9.0) is such that it is unable to fully reload settings. To work around this issue without introducing additional dependencies, avoid creating static loggers and follow the example provided here: https://github.com/akovac35/Logging/blob/v1.0.4/src/com.github.akovac35.Logging.Serilog/SerilogHelper.cs

    Will you consider to implement this feature?

    Change logger/sink configuration at runtime?

    Thanks for the weigh-in @damianh. I agree there are some rough edges around this scenario, especially existing contextual loggers, as you point out. I think the way existing loggers continue to point to the sinks they were created with falls out from the immutable/stateless design, which isn't ideal here but brings a lot of other benefits. Do you think your workaround is something that could be made general? It seems like the best approach available at this point.... :-)

    I think the new WriteTo.Logger() method added in the latest Serilog gets rid of your casting issue:

    Log.Logger = new LoggerConfiguration()
          .WriteTo.Logger(Log.Logger)
          .WriteTo.Console().CreateLogger();
    
  • 相关阅读:
    saltstack之(九)配置管理源码部署Nginx
    saltstack之(八)配置管理部署LAMP
    saltstack之(七)配置管理系统初始化init
    saltstack之(六)配置管理state
    saltstack之(五)数据系统Grains和Pillar
    Visual Studio 2010 如何改用 Beyond Compare 作为 TFS 的比较工具
    C++名人的网站 转
    使用MAP文件快速定位程序崩溃代码行 (转)
    Mybatis自动生成实体类,映射文件,dao
    MinGW安装教程( MinGW
  • 原文地址:https://www.cnblogs.com/chucklu/p/13272020.html
Copyright © 2011-2022 走看看