[英]Custom Serilog sink dont work using AppSettings
I am trying to setup a custom sink for Serilog and cant make it work..我正在尝试为 Serilog 设置一个自定义接收器,但无法使其工作..
I am using the Logger like:我正在使用记录器,例如:
Log.Logger = new LoggerConfiguration().ReadFrom.AppSettings().CreateLogger();
It does work if i do it like this:如果我这样做,它确实有效:
//Log.Logger = new LoggerConfiguration().WriteTo.LogSenderSink().CreateLogger();
So its only when using AppSettings it wont trigger correctly.所以它只有在使用 AppSettings 时才会正确触发。
I have two classes, one for the sink and one for the extension.我有两个类,一个用于接收器,一个用于扩展。
The sink:水槽:
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 { }
}
}
}
And the extension method:和扩展方法:
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));
}
}
}
My App.config in the project where i test from looks like this:我测试的项目中的 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>
What am i missing here??我在这里错过了什么?
I am calling the log like this:我这样调用日志:
Log.Information("Debug was just sent");
And nothing happens at all...什么都没有发生......
Thanks for your time!谢谢你的时间!
您是不是缺少配置文件中的serilog:write-to:LogSenderSink
设置?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.