gpt4 book ai didi

c# - Log4net SmtpAppender 不工作

转载 作者:太空狗 更新时间:2023-10-29 20:16:48 27 4
gpt4 key购买 nike

我尝试使用这个配置:

<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 文件中找到了日志,但没有邮件!请注意,我登录错误并且我能够通过代码发送电子邮件。我有这个配置:

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

我不知道我做错了什么!

最佳答案

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

当我使用端口 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>

关于c# - Log4net SmtpAppender 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26932880/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com