Serilog在其输出中写入重复的条目

时间:2019-03-24 18:19:45

标签: c# windows-services serilog

我写了一个Windows Service,它使用Serilog

记录了其操作

该应用程序还将Autofac用于依赖项注入支持,Seq用于结构化日志记录。

 using Autofac;
 public class ContainerInitiator
 {
       public static IContainer BuildContainer()
       {
          var _builder = new ContainerBuilder();
          var logger = LoggerUtility.CreateLogger();
          _builder.RegisterInstance(logger).As<ILogger>().SingleInstance();
          var container = _builder.Build();
          ContainerFactory.SetContainer(container);
          return container;
        }
  }

这是Log实用程序类

public class LoggerUtility
{
    private static string connectionString = ConfigurationManager.AppSettings["applicationName"];
    public static ILogger CreateLogger()
    {
        return GetLoggerConfiguration().CreateLogger();
    }
    private static LoggerConfiguration GetLoggerConfiguration()
    {
        var config = new LoggerConfiguration()
            .ReadFrom.AppSettings();
        config = config.
           Enrich.WithProperty("ApplicationName", connectionString).
           Enrich.WithExceptionDetails().
           Enrich.WithMachineName().
           Enrich.WithProcessId().
           Enrich.WithThreadId().
           ReadFrom.AppSettings();
        return config;
    }
}

这是我的服务类,正在记录活动

public class Engine : IEngine
{
      private readonly ILogger _logger;

      public RedistributeEngine(IContainerFactory containerFactory)
      {
           _logger = containerFactory.GetInstance<ILogger>();
      }

       public void Start()
       {
             _logger.Information("Engine started!");
       }
}

正如您在Seq日志下方看到的那样,它同时具有两个条目!

enter image description here

1 个答案:

答案 0 :(得分:0)

您两次从App.Settings中读取配置:

    private static LoggerConfiguration GetLoggerConfiguration()
{
    var config = new LoggerConfiguration()
        .ReadFrom.AppSettings(); // <<<<<<<<<<<< *#*#*#*#*#*#*#*#
    config = config.
       Enrich.WithProperty("ApplicationName", connectionString).
       Enrich.WithExceptionDetails().
       Enrich.WithMachineName().
       Enrich.WithProcessId().
       Enrich.WithThreadId().
       ReadFrom.AppSettings(); // <<<<<<<<<<<< *#*#*#*#*#*#*#*#
    return config;
}