我正在使用log4net。Log4Net SmtpAppender不發送郵件
當應用程序發生錯誤時,我想發送電子郵件只是爲了發生錯誤。我應該怎麼做?
我的配置文件:
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
<log4net>
<appender name="SMTPAppender" type="log4net.Appender.SMTPAppender">
<authentication value="Basic" />
<to value="[email protected]" />
<from value="[email protected]" />
<username value="user" />
<password value="pass" />
<subject value="ERROR" />
<smtpHost value="host" />
<port value="25" />
<lossy value="true" />
<evaluator type="log4net.Core.LevelEvaluator">
<threshold value="WARN" />
</evaluator>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%newline%date [%thread] %-5level %logger %newline %message%newline%newline%newline" />
</layout>
</appender>
<root>
<level value="INFO"></level>
</root>
<logger name="SMTPAppender">
<level value="INFO"></level>
<appender-ref ref="SMTPAppender"></appender-ref>
</logger>
</log4net>
</configuration>
我想在NUnit的測試:
[TestFixture]
public class TestClass
{
[Test]
public void Test()
{
ILog logger = LogManager.GetLogger(typeof(TestClass));
logger.Error("test mail");
}
}
我可能做錯了什麼。
編輯:
<logger name="Yuartz.Tests.TestClass">
<level value="INFO"></level>
<appender-ref ref="SMTPAppender"></appender-ref>
</logger>
namespace Yuartz.Tests
{
using log4net;
using log4net.Config;
[TestFixture]
public class TestClass
{
[Test]
public void Test()
{
XmlConfigurator.Configure();
var logger = LogManager.GetLogger("SMTPAppender");
logger.Error("test mail");
logger.Info("test mail");
}
}
}
如果你的日誌配置是不是在app.config中,我會建議你將它添加到那裏。並確保log4net正確配置,否則調用XmlConfigurator.Configure(); – adt
請「刪除單位」 - 測試標籤。它與你的問題沒有關係 – Morten