我创建了一个新的 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 的任何建议?
谢谢。