首页 文章

使用appSettings配置自定义Serilog接收器

提问于
浏览
4

我创建了一个新的Serilog自定义接收器,并尝试通过appSettings配置它 .

自定义接收器名为“DiagnosticsBroadcaster” . 其LoggerSinkConfiguration扩展名为:

public static LoggerConfiguration DiagnosticsBroadcaster(
        this LoggerSinkConfiguration loggerConfiguration,
        string defaultLoggerName = "serilog",
        LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
        IFormatProvider formatProvider = null)
    {
        if (loggerConfiguration == null)
        {
            throw new ArgumentNullException("loggerConfiguration");
        }

        if (defaultLoggerName == null)
        {
            throw new ArgumentNullException("defaultLoggerName");
        }

        return loggerConfiguration.Sink(new DiagnosticsBroadcaster(defaultLoggerName,     formatProvider), restrictedToMinimumLevel);
    }

DiagnosticsBroadcast接收器类是:

public class DiagnosticsBroadcaster : ILogEventSink, IDisposable
{
    private OMMHttpClient _clientApp = null;
    private readonly string _defaultLoggerName = string.Empty;
    private readonly IFormatProvider _formatProvider = null;
    private readonly object _syncRoot = new object();
    private bool _isDisposed = false;

    //----------------------------------------------------------------------------------------------------
    //----------------------------------------------------------------------------------------------------       
    public DiagnosticsBroadcaster(string defaultLoggerName, IFormatProvider formatProvider = null)
    {
        if (string.IsNullOrEmpty(defaultLoggerName))
        {
            throw new ArgumentException("defaultLoggerName");
        }

        _defaultLoggerName = defaultLoggerName;
        _formatProvider = formatProvider;

        string ommDispatcherUrl = ConfigurationManager.AppSettings["OMMDispatcherUrl"];

        _clientApp = new OMMHttpClient(ommDispatcherUrl, "api/omm/diag/app");            
    }

    //----------------------------------------------------------------------------------------------------
    //----------------------------------------------------------------------------------------------------
    public void Emit(Serilog.Events.LogEvent logEvent)
    {
        var loggerName = _defaultLoggerName;

        string message;

        lock (_syncRoot)
        {
            if (_isDisposed)
            {
                throw new ObjectDisposedException("The OMMJHttpClient has been disposed.");
            }

            if (logEvent.Exception != null)
            {
                message = string.Format("{0} -- EXCEPTION: {1}", logEvent.RenderMessage(_formatProvider), logEvent.Exception.Message);
            }
            else
            {
                message = logEvent.RenderMessage(_formatProvider);
            }

            Task.Factory.StartNew(async () =>
            {
                try
                {
                    HttpResponseMessage response = await _clientApp.Post<string>(message);

                    if (!response.IsSuccessStatusCode)
                    {
                        Log.Warning("Error sending diagnostic message [APP] - {0} - {1}", response.StatusCode, response.ReasonPhrase);
                    }
                }
                catch (Exception ex)
                {
                    Log.Warning("Error sending diagnostic message [APP]  - {0}", ex.Message);
                }
            });
        }
    }

    //----------------------------------------------------------------------------------------------------
    //----------------------------------------------------------------------------------------------------  
    public void Dispose()
    {
        lock (_syncRoot)
        {
            if (_clientApp != null)
            {
                _clientApp.Dispose();
                _isDisposed = true;
            }
        }
    }
}

如果我按如下方式配置,则此自定义接收器可用:

Log.Logger = new LoggerConfiguration()
                          .ReadAppSettings()
                          .WriteTo.DiagnosticsBroadcaster(restrictedToMinimumLevel: LogEventLevel.Information)                              
                          .CreateLogger();

但是如果我尝试通过appSettings配置它,它的方法永远不会被调用:

<add key="serilog:write-to:DiagnosticsBroadcaster.restrictedToMinimumLevel" value="Information" />

有关如何通过appSettings配置DiagnosticsBroadcaster的任何建议?

谢谢 .

1 回答

  • 3

    我认为您最有可能需要包含新接收器的程序集的 serilog:using 指令:

    <add key="serilog:using" value="YourCompany.YourAssembly" />
    

    如果您需要为多个附加接收器指定此项,请通过附加短名称使密钥更加明确:

    <add key="serilog:using:Diag" value="YourCompany.YourAssembly" />
    

    (短名称是什么并不重要,只是它在应用程序设置键中是唯一的 .

相关问题