.Net Core 2.0 控制台应用程序不通过 NLog 向 Loggly 发送消息

.Net Core 2.0 Console Application not sending messages to Loggly via NLog

我有一个 .NET Core 2.0 (Topshelf) 控制台应用程序需要将日志发送到 Loggly。我们已经使用 NLog、NLog.Targets.Loggly、Topshelf.NLog、loggly-csharp-config 和 loggly-csharp 实现了相同类型的应用程序。

我添加了两个额外的目标,控制台和文本。其他两个目标都工作正常。

客户 Loggly 密钥在我们具有相同结构和相同包的 .NET 4.71 应用程序中工作正常。

这是我的包参考:

<ItemGroup>
<PackageReference Include="Autofac" Version="4.8.1" />
<PackageReference Include="loggly-csharp" Version="4.6.1.64" />
<PackageReference Include="loggly-csharp-config" Version="4.6.1.64" />
<PackageReference Include="NLog.Targets.Loggly" Version="4.7.0" />
<PackageReference Include="Topshelf" Version="4.1.0" />
<PackageReference Include="Topshelf.NLog" Version="4.1.0" />

编辑:添加更多实现代码 来自 App.config:

<configSections>
    <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog" />
    <section name="loggly" type="Loggly.Config.LogglyAppConfig, Loggly.Config, Version=3.5.0.0, Culture=neutral, PublicKeyToken=null" />
  </configSections>
...

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
        autoReload="true"
        throwExceptions="false"
        internalLogLevel="Trace" internalLogFile="c:\temp\nlog-internal.log">
    <extensions>
      <add assembly="NLog.Targets.Loggly" />
    </extensions>
    <variable name="DefaultLayout" value="${longdate} | ${level:uppercase=true:padding=5} | ${message} | ${exception:format=@} | ${callsite} | ${callsite-linenumber} | ${all-event-properties}" />
    <targets async="true">
      <target xsi:type="File" name="logfile" fileName="logs/log.txt"  layout="${DefaultLayout}"/>
      <target xsi:type="Console" name="console"  layout="${DefaultLayout}"/>
      <target xsi:type="Loggly" name="Loggly" layout="${DefaultLayout}"/>
    </targets>
    <rules>
      <logger name="*" minlevel="Trace" writeTo="logfile,Loggly,console" />
    </rules>
  </nlog>
  <loggly
    xmlns="Loggly"
    applicationName="masked"
    customerToken="masked"
    throwExceptions="true">
    <transport logTransport="Https"/>
    <tags>
      <simple>
        <tag value="DEV"/>
      </simple>
      <complex>
        <tag type="Loggly.HostnameTag" formatter="{0}"/>
        <tag type="Loggly.ApplicationNameTag" formatter="{0}"/>
      </complex>
    </tags>
  </loggly>

内部日志似乎显示至少有尝试命中 Loggly。

2018-12-12 09:05:09.8816 Debug Targets for Topshelf.Hosts.ConsoleRunHost by level: 2018-12-12 09:05:09.8816 Debug Trace => 2018-12-12 09:05:09.8816 Debug Debug => 2018-12-12 09:05:09.8816 Debug Info => 2018-12-12 09:05:09.8816 Debug Warn => 2018-12-12 09:05:09.8816 Debug Error => 2018-12-12 09:05:09.8816 Debug Fatal => 2018-12-12 09:05:10.1486 Trace AsyncWrapper(Name=Loggly): Writing 1 events (Timer) 2018-12-12 09:05:10.1486 Trace AsyncWrapper(Name=console): Writing 1 events (Timer) 2018-12-12 09:05:10.1486 Trace AsyncWrapper(Name=logfile): Writing 1 events (Timer) 2018-12-12 09:05:10.1747 Trace Opening

感谢@RolfKristensen 的评论,我尝试通过代码手动配置 csharp 客户端,它工作正常。我必须将配置移动到 appSettings 并让配置管理器挑选它们。我想 loggly-csharp 需要更好的文档来避免在 .NET Core 中使用他们的配置示例。