有谁知道在本地主机上的 MVC3 应用程序上运行测试时如何禁用 NLog 应用程序发送电子邮件?我希望 NLog 仅在应用程序部署到网络服务器时出错时才发送电子邮件。为了实现这一点,我必须写什么?
谢谢
有谁知道在本地主机上的 MVC3 应用程序上运行测试时如何禁用 NLog 应用程序发送电子邮件?我希望 NLog 仅在应用程序部署到网络服务器时出错时才发送电子邮件。为了实现这一点,我必须写什么?
谢谢
您可以尝试通过更改邮件设置来“禁用”邮件发送:
<system.net>
<mailSettings>
<smtp deliveryMethod="SpecifiedPickupDirectory">
<specifiedPickupDirectory pickupDirectoryLocation="c:\whateverfolder"/>
</smtp>
</mailSettings>
</system.net>
这会将电子邮件放在您计算机上的一个文件夹中。这并不能真正阻止邮件,但会阻止它们被发送。
如果您正在运行手动或自动测试,还可以更轻松地验证电子邮件是否正在“发送”。
您可以为此使用过滤器。如果服务器名称是 localhost,您可以使用when-filter过滤邮件目标的消息。这假设您使用 localhost 请求站点。但是,您可以为条件使用任何布局渲染器。
请参阅下面的示例配置,它适用于我。
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target xsi:type="Mail" name="m" layout="${message}" subject="Log"
to="mail@domain.com" from="app@domain.com" body="${message}"
smtpServer="localhost">
</target>
</targets>
<rules>
<logger name="*" minlevel="Fatal" writeTo="m" >
<filters>
<when condition="equals('${aspnet-request:serverVariable=SERVER_NAME}','localhost')" action="Ignore" />
</filters>
</logger>
</rules>
</nlog>