我正在尝试为Serilog设置自定义接收器,无法使其工作..
我正在使用Logger,例如:
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>
我在这里想念什么?
我这样称呼日志:
Log.Information("Debug was just sent");
而且什么也没有发生...
开心每一天1111
相关分类