我的 log4net 配置如下
<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>
滚动文件附加程序工作正常,但是 SMTP 附加程序失败并出现以下错误
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
我觉得奇怪的是错误末尾的方括号不包含配置中的 SMTP 主机。我期待看到localhost
那里。
我在本地机器上针对 smtp4dev 运行它。
谢谢!