Log4net SmtpAppender无法正常工作

时间:2014-11-14 15:03:32

标签: c# asp.net-mvc-5 log4net

我尝试使用此配置:

<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>

我有这个内部日志

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 []

我在TimeZone.log文件中找到了日志但没有邮件! 请注意,我登录了错误&amp;我可以通过代码发送电子邮件。我有这个配置:

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

我不知道我做错了什么!

4 个答案:

答案 0 :(得分:8)

将此问题用于我自己的故障排除,我想分享我的发现。

当我使用端口465时,没有发送电子邮件,尽管它可以被服务器接受。简单的解决方案是使用:

<port value="587" />

对于有兴趣使用除gmail之外的人,以下是雅虎邮件的完整设置:

 <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无提示失败,因此您不会看到任何错误。对于调试,请在app.config / web.config文件中添加以下内容。任何错误都会记录在那里。

 <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>

答案 1 :(得分:0)

我的配置无法解决,它与我使用的配置类似。试试这些测试:

更改

<appender name="SmtpAppender" type="log4net.Appender.SmtpAppender,log4net">

<appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">

更改

    <bufferSize value="10" />

    <bufferSize value="1" />

这样可以防止缓冲,以便立即发送电子邮件。

尝试删除以下内容,以便通过电子邮件发送所有内容:

    <evaluator type="log4net.Core.LevelEvaluator,log4net">
        <threshold value="WARN" />
    </evaluator>

答案 2 :(得分:0)

我使用gmail解决了我的问题。 我尝试使用mailjet使用相同的配置(端口,主机等除外),但它没有用。

我现在的配置:

<appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
  <to value="to@mail.com" />
  <from value="from@mail.com" />
  <subject value="Crash log" />
  <smtpHost value="smtp.gmail.com" />
  <authentication value="Basic" />
  <port value="587" />
  <username value="myusername" />
  <password value="mypassword" />
  <bufferSize value="1" />
  <EnableSsl value="true"/>
  <lossy value="true" />
  <evaluator type="log4net.Core.LevelEvaluator">
    <threshold value="ERROR"/>
  </evaluator>
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%level %date - %message%newline" />
  </layout>
</appender>

答案 3 :(得分:0)

配置已指定用户名和密码,但未指定身份验证。 添加以下身份验证元素,意味着您的配置将在连接时使用用户名和密码。

<authentication value="Basic" />