我正在嘗試為Serilog設置自定義接收器,無法使其工作..我正在使用Logger,例如:Log.Logger = new LoggerConfiguration().ReadFrom.AppSettings().CreateLogger();如果我這樣做的話,它確實可以工作://Log.Logger = new LoggerConfiguration().WriteTo.LogSenderSink().CreateLogger();因此,只有在使用AppSettings時,它才能正確觸發(fā)。我有兩個類,一個用于接收器,一個用于擴展。水槽: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");而且什么也沒有發(fā)生...
- 1 回答
- 0 關注
- 321 瀏覽
添加回答
舉報
0/150
提交
取消