我对log4net有以下SmtpAppender配置:
<appender name="AlchemySmtpAppender" type="log4net.Appender.SmtpAppender">
<to value="my@mail.com" />
<from value="validfrom@mail.com" />
<subject value="Fatal error on Web Portal" />
<smtpHost value="ValidHost" />
<username value="myUserName"/>
<password value="myPassword"/>
<lossy value="false" />
<bufferSize value="0"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%newline%date [%thread] %-5level %logger - %message%newline%newline%newline" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="AlchemySmtpAppender" />
</root>
电子邮件从未发送过。
我启用了内部调试并检查了trace / output并找到了以下内容:
log4net: Loading Appender [SmtpAppender] type: [log4net.Appender.SmtpAppender]
log4net: Setting Property [To] to String value [my@mail.com]
log4net: Setting Property [From] to String value [validfrom@mail.com]
log4net: Setting Property [Subject] to String value [Fatal error on Web Portal]
log4net: Setting Property [SmtpHost] to String value [ValidHost]
log4net: Setting Property [Username] to String value [myUserName]
log4net: Setting Property [Password] to String value [myPassword]
log4net: Setting Property [Lossy] to Boolean value [False]
log4net: Setting Property [BufferSize] to Int32 value [0]
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 [%newline%date [%thread] %-5level %logger - %message%newline%newline%newline]
log4net: Converter [newline] 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 [thread] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [literal] Option [] ] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [level] Option [] Format [min=5,max=2147483647,leftAlign=True]
log4net: Converter [literal] Option [ ] Format [min=-1,max=2147483647,leftAlign=False]
log4net: Converter [logger] 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: Converter [newline] 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: reated Appender [SmtpAppender]
log4net: Adding appender named [SmtpAppender] to logger [root].
log4net: Hierarchy Threshold []
我看不到任何错误,但也没有电子邮件。电子邮件地址,凭据和主机都是有效的,因为我们也在web.config文件的System.Net部分中使用它们,它们完美地工作。
任何人都知道我的配置可能会出错吗?或者我可以做一些其他形式的故障排除来解决这个问题? 使用像RollingFile,Trace和AdoNetAppender这样的其他appender也可以很好地工作。
答案 0 :(得分:1)
我设法解决了我的问题,安装smtp4dev并配置log4net以使用我设置的虚拟主机。
只是为了确认有人需要这样做:
<appender name="MySmtpAppender" type="log4net.Appender.SmtpAppender">
<To value="my@mail.com" />
<From value="no-reply@mail.com" />
<Subject value="Fatal error on Web Portal" />
<SmtpHost value="FakeHost" />
<Port value="25" />
<Username value="myUsername"/>
<Password value="myPassword"/>
<Layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%newline%date [%thread] %level %logger [%property{NDC}] - %message%newline%newline%newline" />
</Layout>
</appender>
希望有所帮助。