【问题标题】:localhost doesn't work with UdpClient for NLoglocalhost 不适用于 NLog 的 UdpClient
【发布时间】:2017-01-27 17:21:22
【问题描述】:

我可以通过 127.0.0.1 接收带有 UdpClient 对象的 NLog 消息,但不能通过 localhost。

我的 NLog.config 文件中的目标是:

    <!-- this works -->
    <target name="<MyLogger" type="Chainsaw" address="udp://127.0.0.1:7777"/>

    <!-- this does not work, no messages received -->
    <!--<target name="MyLogger" type="Chainsaw" address="udp://localhost:7777"/>-->

我尝试了各种方法来打开/配置我的UdpClient 对象,包括:

// this way
_udpClient = new UdpClient(settings.Port);

// this way
_endPoint = new IPEndPoint(IPAddress.Any, settings.Port);

/// this way
_udpClient = new UdpClient();
_endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), settings.Port);
_udpClient.Client.Bind(_endPoint);

我已经尝试了以上所有方法和许多变体。

我知道我可以在配置文件中使用 localhost,因为它可以与另一个 NLog 客户端 Log2Console 一起使用。我的配置文件中的 Log2Console 行仅因目标名称(MyLogger Log2Console)而异。 127.0.0.1 和 localhost 都可以与 Log2Console 一起使用。

我担心我在做一些愚蠢的事情,但我看不到它。

【问题讨论】:

    标签: localhost nlog udpclient


    【解决方案1】:

    localhost 可能会解析 IP V6 地址 (::1),因此监听器也应该监听那个。

    您可以使用udp4://localhost:7777 强制IP V4,请参阅the docs

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-08
      • 2015-05-09
      相关资源
      最近更新 更多