C# – Log4net SmtpAppender not working

asp.net-mvc-5c++log4net

I tried to use this configuration :

<log4net>
    <appender name="SmtpAppender" type="log4net.Appender.SmtpAppender,log4net">
        <to value="myaccount@supinfo.com" />
        <from value="support@company.com" />
        <subject value="TimeZone Crash log" />
        <smtpHost value="in.mailjet.com" />
        <username value="login" />
        <password value="password" />
        <bufferSize value="10" />
        <lossy value="false" />
        <evaluator type="log4net.Core.LevelEvaluator,log4net">
            <threshold value="WARN" />
        </evaluator>
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%level %date - %message%newline" />
        </layout>
    </appender>
    <logger name="SmtpAppender">
        <level value="ERROR" />
        <appender-ref ref="SmtpAppender" />
    </logger>
    <appender name="Trace" type="log4net.Appender.RollingFileAppender">
        <file value="c:\Traces\TimeZone.log" />
        <PreserveLogFileNameExtension value="true" />
        <appendToFile value="true" />
        <maximumFileSize value="10MB" />
        <maxSizeRollBackups value="2" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%level %date - %message%newline" />
        </layout>
        <param name="Encoding" value="utf-8" />
    </appender>
    <logger name="Trace">
        <!--ALL
        DEBUG
        INFO
        WARN
        ERROR
        FATAL
        OFF-->
        <level value="DEBUG" />
        <appender-ref ref="Trace" />
    </logger>
</log4net>

i had this internal log

log4net: log4net assembly [log4net, Version=1.2.13.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a]. Loaded from [C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\project\27cf2c51\20668825\assembly\dl3\5844a199\aebaa176_49edcf01\log4net.dll]. (.NET Runtime [4.0.30319.18444] on Microsoft Windows NT 6.1.7601 Service Pack 1)
log4net: defaultRepositoryType [log4net.Repository.Hierarchy.Hierarchy]
log4net: Creating repository for assembly [Company.Server.Utils, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]
log4net: Assembly [Company.Server.Utils, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] Loaded From [C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\project\27cf2c51\20668825\assembly\dl3\a39c173f\4cf4538a_4aedcf01\Company.Server.Utils.dll]
log4net: Assembly [Company.Server.Utils, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] does not have a RepositoryAttribute specified.
log4net: Assembly [Company.Server.Utils, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] using repository [log4net-default-repository] and repository type [log4net.Repository.Hierarchy.Hierarchy]
log4net: Creating repository [log4net-default-repository] using type [log4net.Repository.Hierarchy.Hierarchy]
log4net: configuring repository [log4net-default-repository] using .config file section
log4net: Application config file is [D:\folder\trunk\Project\Project\web.config]
log4net: Configuring Repository [log4net-default-repository]
log4net: Configuration update mode [Merge].
log4net: Retrieving an instance of log4net.Repository.Logger for logger [SmtpAppender].
log4net: Setting [SmtpAppender] additivity to [True].
log4net: Logger [SmtpAppender] Level string is [WARN].
log4net: Logger [SmtpAppender] level set to [name="WARN",value=60000].
log4net: Loading Appender [SmtpAppender] type: [log4net.Appender.SmtpAppender,log4net]
log4net: Setting Property [To] to String value [myaccount@supinfo.com]
log4net: Setting Property [From] to String value [support@company.com]
log4net: Setting Property [Subject] to String value [TimeZone Crash log]
log4net: Setting Property [SmtpHost] to String value [in.mailjet.com]
log4net: Setting Property [Username] to String value [login]
log4net: Setting Property [Password] to String value [password]
log4net: Setting Property [BufferSize] to Int32 value [10]
log4net: Setting Property [Lossy] to Boolean value [False]
log4net: Setting Property [Threshold] to Level value [WARN]
log4net: Setting Property [Evaluator] to object [log4net.Core.LevelEvaluator]
log4net: Converter [message] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [newline] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Setting Property [ConversionPattern] to String value [%level %date - %message%newline]
log4net: Converter [level] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [literal] Option [ ] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [date] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [literal] Option [ - ] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [message] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [newline] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Setting Property [Layout] to object [log4net.Layout.PatternLayout]
log4net: Created Appender [SmtpAppender]
log4net: Adding appender named [SmtpAppender] to logger [SmtpAppender].
log4net: Retrieving an instance of log4net.Repository.Logger for logger [Trace].
log4net: Setting [Trace] additivity to [True].
log4net: Logger [Trace] Level string is [DEBUG].
log4net: Logger [Trace] level set to [name="DEBUG",value=30000].
log4net: Loading Appender [Trace] type: [log4net.Appender.RollingFileAppender]
log4net: Setting Property [File] to String value [c:\Traces\TimeZone.log]
log4net: Setting Property [PreserveLogFileNameExtension] to Boolean value [True]
log4net: Setting Property [AppendToFile] to Boolean value [True]
log4net: Setting Property [MaximumFileSize] to String value [10MB]
log4net: Setting Property [MaxSizeRollBackups] to Int32 value [2]
log4net: Converter [message] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [newline] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Setting Property [ConversionPattern] to String value [%level %date - %message%newline]
log4net: Converter [level] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [literal] Option [ ] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [date] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [literal] Option [ - ] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [message] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [newline] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Setting Property [Layout] to object [log4net.Layout.PatternLayout]
log4net: Setting Property [Encoding] to UTF8Encoding value [System.Text.UTF8Encoding]
log4net: Type = [0], r0 = [.1970-01-01], r1 = [.1970-01-01]
log4net: Type = [1], r0 = [.1970-01-01], r1 = [.1970-01-01]
log4net: Type = [2], r0 = [.1970-01-01], r1 = [.1970-01-01]
log4net: Type = [3], r0 = [.1970-01-01], r1 = [.1970-01-02]
log4net: Searched for existing files in [c:\Traces]
log4net: curSizeRollBackups starts at [0]
log4net: [.2014-11-14] vs. [.2014-11-14]
log4net: Opening file for writing [c:\Traces\TimeZone.log] append [True]
log4net: Created Appender [Trace]
log4net: Adding appender named [Trace] to logger [Trace].
log4net: Hierarchy Threshold []

i found the log in TimeZone.log file but no mail !
Note that i logged in Error & i'm able to send emails by code. i have this config :

<system.net>
    <mailSettings>
        <smtp deliveryMethod="network">
            <network defaultCredentials="false" host="in.mailjet.com" userName="login" password="password" enableSsl="true"/>
        </smtp>
    </mailSettings>
</system.net>

i don't know what i'm doing wrong !

Best Solution

Using this question for my own troubleshooting I thought to share my findings.

E-mails were not sent when I was using port 465, though it is acceptable by the server. Simple solution was to use:

<port value="587" />

For those interested to use other than gmail, below are complete settings for yahoo mail:

 <configuration>
  <configSections>
    <section name ="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
  </configSections>
  <log4net>
    <appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
      <to value="email1@gmail.com; email2@yahoo.com" />
      <from value="your_User_Name@yahoo.co.uk" />
      <subject type="log4net.Util.PatternString" value="Message logged" />
      <smtpHost value="smtp.mail.yahoo.com" />
      <authentication value="Basic" />
      <port value="587" />
      <username value="your_User_Name@yahoo.co.uk" />
      <password value="your_password" />
      <bufferSize value="1" />
      <EnableSsl value="true"/>
      <lossy value="false" />
      <threshold value="DEBUG" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%utcdate [%level] - %message%newline%exception" />
      </layout>
    </appender>
    <root>
      <level value="DEBUG"/>
      <appender-ref ref="SmtpAppender"/>
    </root>
  </log4net>
</configuration>

Log4net fails silently so you will not see any errors. For debugging add following to your app.config/web.config file. Any errors will be recorded there.

 <appSettings>
    <add key="log4net.Internal.Debug" value="true" />
 </appSettings>

 <system.diagnostics>
  <trace autoflush="true">
   <listeners>
    <add name="tracer" 
     type="System.Diagnostics.TextWriterTraceListener"
     initializeData="C:\\yourfolder\\log4net.log" />
    </listeners>
   </trace>
 </system.diagnostics>
Related Question