How to send syslog messages with log4net and Nlog

Nlog

Log4Net

To use syslog with log4net, we need to add the following appender:


<appender name="syslog" type="log4net.Appender.RemoteSyslogAppender">
    <identity value="MyApp" />
    <remoteAddress value="102.10.23.22" />
    <remotePort value="514"/>
    <threshold value="Info" />
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="[%thread] %logger [%property{NDC}] - %message%newline" />
    </layout>
</appender>

NLog

To work with NLog we need to install the NLog.Targets.Syslog package.

Edit the nlog.config and add these:



  <nlog 
  	...
  	xmlns:sl="http://www.nlog-project.org/schemas/NLog.Targets.Syslog.xsd" 
    .../>

  <extensions>
    <add assembly="nlog.targets.syslog"/>
  </extensions>
  
  ...
  
  <target name="syslogTarget" xsi:type="Syslog">
  	<messageSend>
		<udp server="127.0.0.1" port="514" />
    </messageSend>
	<sl:layout xsi:type="SimpleLayout" text="${message}" />
    <sl:messageCreation>
		<sl:facility>Local0</sl:facility>
		<sl:rfc>Rfc5424</sl:rfc>
		<sl:rfc5424>
			<sl:hostname xsi:type="SimpleLayout" text="MYSERVER" />
			<sl:appName xsi:type="SimpleLayout" text="MYAPP" />
			<sl:procId xsi:type="SimpleLayout" text="${processid}" />
			<sl:msgId xsi:type="SimpleLayout" text="${threadid}" />
			<sl:disableBom>true</sl:disableBom>
		</sl:rfc5424>
	</sl:messageCreation>    
  </target>
  
  ...
  
  <logger name="*" minlevel="Debug" writeTo="syslogTarget" />
  

More settings can be found here: https://github.com/luigiberrettini/NLog.Targets.Syslog/blob/master/docs/configuration.md

Post a Comment

Previous Post Next Post