How do you log the machine name via log4net?

后端 未结 2 1810
借酒劲吻你
借酒劲吻你 2020-12-01 10:08

I am using Log4Net with the AdoNetAppender to log messages from a simple systray application into a SQL Server 2005 database.

I want to log the machine name along wi

相关标签:
2条回答
  • 2020-12-01 10:42

    You can use the pre-populated property log4net:HostName, for example:

    <conversionPattern value="%property{log4net:HostName}" />
    

    This way you don't need to populate the MDC.

    0 讨论(0)
  • 2020-12-01 10:43

    you can create a parameter similar to the following:

    <parameter>
      <parameterName value="@machine" />
      <dbType value="String" />
      <size value="255" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%X{machine}" />
      </layout>
    </parameter>
    

    Then add this line before writing to the log: MDC.Set("machine", Environment.MachineName);

    0 讨论(0)
提交回复
热议问题