自定义 Serilog 接收器无法使用 AppSettings
Custom Serilog sink dont work using AppSettings
我正在尝试为 Serilog 设置自定义接收器,但无法使其正常工作..
我像这样使用记录器:
Log.Logger = new LoggerConfiguration().ReadFrom.AppSettings().CreateLogger();
如果我这样做确实有效:
//Log.Logger = new LoggerConfiguration().WriteTo.LogSenderSink().CreateLogger();
所以它只有在使用 AppSettings 时才会正确触发。
我有两个 类,一个用于水槽,一个用于扩展。
水槽:
using System;
using Serilog.Core;
using Serilog.Events;
namespace Serilog.Sinks.LogSender
{
public class LogSenderSink : ILogEventSink, IDisposable
{
private readonly IFormatProvider _formatProvider;
public LogSenderSink(IFormatProvider formatProvider, LogEventLevel restrictedToMinimumLevel)
{
_formatProvider = formatProvider;
}
public void Dispose()
{
}
public void Emit(LogEvent logEvent)
{
var message = logEvent.RenderMessage(_formatProvider);
Console.WriteLine(DateTimeOffset.Now.ToString() + " " + message);
try
{
System.IO.File.WriteAllText(@"C:\Logs\test.txt", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
}
catch { }
}
}
}
以及扩展方法:
using Serilog.Configuration;
using Serilog.Events;
using Serilog.Sinks.LogSender;
using System;
namespace Serilog
{
public static class LogSinkExtensions
{
public static LoggerConfiguration LogSenderSink(
this LoggerSinkConfiguration loggerConfiguration,
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
IFormatProvider formatProvider = null)
{
return loggerConfiguration.Sink(new LogSenderSink(formatProvider, restrictedToMinimumLevel));
}
}
}
我测试的项目中的 App.config 看起来像这样:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<appSettings>
<add key="serilog:minimum-level" value="Verbose"/>
<add key="serilog:using:LogSender" value="Serilog.Sinks.LogSender" />
</appSettings>
</configuration>
我在这里错过了什么?
我是这样调用日志的:
Log.Information("Debug was just sent");
然后什么也没发生...
感谢您的宝贵时间!
您是不是缺少配置文件中的 serilog:write-to:LogSenderSink
设置?
我正在尝试为 Serilog 设置自定义接收器,但无法使其正常工作..
我像这样使用记录器:
Log.Logger = new LoggerConfiguration().ReadFrom.AppSettings().CreateLogger();
如果我这样做确实有效:
//Log.Logger = new LoggerConfiguration().WriteTo.LogSenderSink().CreateLogger();
所以它只有在使用 AppSettings 时才会正确触发。
我有两个 类,一个用于水槽,一个用于扩展。
水槽:
using System;
using Serilog.Core;
using Serilog.Events;
namespace Serilog.Sinks.LogSender
{
public class LogSenderSink : ILogEventSink, IDisposable
{
private readonly IFormatProvider _formatProvider;
public LogSenderSink(IFormatProvider formatProvider, LogEventLevel restrictedToMinimumLevel)
{
_formatProvider = formatProvider;
}
public void Dispose()
{
}
public void Emit(LogEvent logEvent)
{
var message = logEvent.RenderMessage(_formatProvider);
Console.WriteLine(DateTimeOffset.Now.ToString() + " " + message);
try
{
System.IO.File.WriteAllText(@"C:\Logs\test.txt", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
}
catch { }
}
}
}
以及扩展方法:
using Serilog.Configuration;
using Serilog.Events;
using Serilog.Sinks.LogSender;
using System;
namespace Serilog
{
public static class LogSinkExtensions
{
public static LoggerConfiguration LogSenderSink(
this LoggerSinkConfiguration loggerConfiguration,
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
IFormatProvider formatProvider = null)
{
return loggerConfiguration.Sink(new LogSenderSink(formatProvider, restrictedToMinimumLevel));
}
}
}
我测试的项目中的 App.config 看起来像这样:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<appSettings>
<add key="serilog:minimum-level" value="Verbose"/>
<add key="serilog:using:LogSender" value="Serilog.Sinks.LogSender" />
</appSettings>
</configuration>
我在这里错过了什么?
我是这样调用日志的:
Log.Information("Debug was just sent");
然后什么也没发生...
感谢您的宝贵时间!
您是不是缺少配置文件中的 serilog:write-to:LogSenderSink
设置?