在使用4.6.2在Windows上发布后,seil无法找到System.Net.Http的Serilog

时间:2017-07-24 15:37:37

标签: c# logging serilog

因此,在完成发布过程后,我无法让应用程序运行。它们来自visual studio调试器,因为System.Net.hhtp的一个版本被放入调试目录中。我相信这个dll已经存在并存储在客户端机器上,这就是为什么它不在输出中,我不明白的是我做错了。

**:我想要注意的是,在linux上设置dotnetcore,然后发布和运行时,我没有遇到任何问题。

这是一个简单的cli应用程序,用于显示和隔离我的问题:

using Serilog;
using Serilog.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SerilogTest
{
    class Program
    {
        static void Main(string[] args)
        {
            SetStaticLoggerConfiguration("http://localhost:5341/", @"C:\Logs\IQLRServices\JobDispatcher-{Date}.txt");
            Log.Information("The logger is working.");

        }
        private static void SetStaticLoggerConfiguration(string seqAddress = null, string rollingFileFormat = null)
        {
            LoggerConfiguration logConfig = new LoggerConfiguration();

            if (!string.IsNullOrWhiteSpace(seqAddress))
            {
                logConfig.WriteTo.Seq(seqAddress, Serilog.Events.LogEventLevel.Verbose, 2500, TimeSpan.FromSeconds(10));
            }
            if (!string.IsNullOrWhiteSpace(rollingFileFormat))
            {
                logConfig.WriteTo.RollingFile(rollingFileFormat,
                        restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Debug,
                        outputTemplate: "{Timestamp:MM-dd-yyyy HH:mm:ss.fff zzz} [{Level}] {Scope} {Properties} {Message} {NewLine}{Exception}");
            }
            if (Environment.UserInteractive)
            {
                logConfig.WriteTo.Console(LogEventLevel.Verbose);
            }

            var log = logConfig.CreateLogger();
            Log.Logger = log;
            Serilog.Debugging.SelfLog.Enable(msg => Log.Error("Serilog internal error: {@ErrorMessage}", msg));
        }
    }
}

以下是我为编译添加的Nuget依赖项(我添加了Serilog.Sinks.Seq包,它有自己的deps然后添加了Serilog.Sinks.Console):

<packages>
  <package id="Serilog" version="2.5.0" targetFramework="net462" />
  <package id="Serilog.Formatting.Compact" version="1.0.0" targetFramework="net462" />
  <package id="Serilog.Sinks.Console" version="3.0.1" targetFramework="net462" />
  <package id="Serilog.Sinks.File" version="3.2.0" targetFramework="net462" />
  <package id="Serilog.Sinks.PeriodicBatching" version="2.1.1" targetFramework="net462" />
  <package id="Serilog.Sinks.RollingFile" version="3.3.0" targetFramework="net462" />
  <package id="Serilog.Sinks.Seq" version="3.3.2" targetFramework="net462" />
  <package id="System.Net.Http" version="4.3.1" targetFramework="net462" />
  <package id="System.Security.Cryptography.Algorithms" version="4.3.0" targetFramework="net462" />
  <package id="System.Security.Cryptography.Encoding" version="4.3.0" targetFramework="net462" />
  <package id="System.Security.Cryptography.Primitives" version="4.3.0" targetFramework="net462" />
  <package id="System.Security.Cryptography.X509Certificates" version="4.3.0" targetFramework="net462" />
</packages>

以下是使用默认发布配置发布应用程序的结果(这是在我创建installshield安装项目时以及直接从vs发布时发生的情况):

Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'System.Net.Http, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
   at SerilogTest.Program.SetStaticLoggerConfiguration(String seqAddress, String rollingFileFormat)
   at SerilogTest.Program.Main(String[] args)

1 个答案:

答案 0 :(得分:0)

您需要将以下内容添加到项目的CSPROJ文件中:

<PropertyGroup>
  <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
  <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>