Search code examples
log4netsmtpappender

log4net smtp appender failing


I have log4net configured as follows

<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">

  <!-- Edit to change your filename here -->
  <file value="Logs\\rolling.log" />

  <appendToFile value="true" />
  <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
  <rollingStyle value="Date" />
  <datePattern value=".yyyy-MM-dd'.log'" />
  <maximumFileSize value="10MB" />

  <!-- %identity used for ASP.NET app, %username used for windows app. You can leave them as is or remove one of them accordingly -->
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="[%date{dd MMM yyyy HH:mm:ss fff}] [%5level] (%identity-%username) %message%newline" />
  </layout>
</appender>

<appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
  <to value="***" />
  <from value="***" />
  <subject value="****" />
  <smtpHost value="localhost" />
  <bufferSize value="1" />
  <lossy value="true" />
  <evaluator type="log4net.Core.LevelEvaluator">
    <threshold value="ERROR"/>
  </evaluator>
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="[%date{dd MMM yyyy HH:mm:ss fff}] [%5level] (%identity-%username) %message%newline" />
  </layout>
</appender>

<root>
  <!-- Edit to change level of logging here, under normal conditions this should be set to INFO, use DEBUG level for verbose logging -->
  <level value="INFO" />
  <appender-ref ref="RollingFile" />
  <appender-ref ref="SmtpAppender" />
</root>

The rolling file appender works fine however the SMTP appender is failing with the following error System.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: An attempt was made to access a socket in a way forbidden by its access permissions [::1]:25

What I find strange is that the square brackets in the end of the error do not contain the SMTP host from the config. I was expecting to see localhost there.

I am running this against smtp4dev on my local machine.

Thank you!


Solution

  • The [::1] notation is the IPV6 way of denoting the loopback address (the double colon notation simply hides 0 that don't add information to the address) so the message just tells you the appender is trying to reach the localhost.

    Regarding the error itself, it may be due to an antivirus blocking access to the port you want to use, or to a firewall preventing the connection. Please check whether you have messages regarding blocked access on these applications.