Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 以编程方式配置NLog,web.config_C#_Asp.net Mvc_Asp.net Mvc 4_Nlog - Fatal编程技术网

C# 以编程方式配置NLog,web.config

C# 以编程方式配置NLog,web.config,c#,asp.net-mvc,asp.net-mvc-4,nlog,C#,Asp.net Mvc,Asp.net Mvc 4,Nlog,我的web.config中有以下Nlog配置部分(修改为仅显示相关信息) 但是,当我记录任何错误时,电子邮件不会被发送。我还有一个额外的文件目标(代码段中未显示),其中包含错误消息。这告诉我错误被记录下来了,但不知怎么的,不是通过电子邮件发送的 如果不是通过代码更新配置,而是硬编码web.config中的值,则会发送电子邮件。我已通过代码验证我使用的smtp值是否有效 我确实搜索了许多类似的问题,但我还没有找到一个提到适合我的解决方案 编辑: 根据Xharze的回答,我启用了异常、内部日志记录,

我的web.config中有以下Nlog配置部分(修改为仅显示相关信息)

但是,当我记录任何错误时,电子邮件不会被发送。我还有一个额外的文件目标(代码段中未显示),其中包含错误消息。这告诉我错误被记录下来了,但不知怎么的,不是通过电子邮件发送的

如果不是通过代码更新配置,而是硬编码web.config中的值,则会发送电子邮件。我已通过代码验证我使用的smtp值是否有效

我确实搜索了许多类似的问题,但我还没有找到一个提到适合我的解决方案

编辑:


根据Xharze的回答,我启用了异常、内部日志记录,并且在进行更改后还输出了目标的值。内部日志显示关于
mailadress
格式不正确的异常。所以我检查了所有接受电子邮件地址的目标值,发现了问题。目标的
from
属性接受一个电子邮件地址,而我提供了一个显示名称

你能试着启用抛出异常和内部日志,然后发布它吗?它可能提供更多信息。请参阅github.com/NLog/NLog/wiki/Logging疑难解答。

您能否尝试启用抛出异常和内部日志,并发布它?它可能提供更多信息。见@Xharze,谢谢你,伙计。启用异常、内部日志记录和输出目标值的组合有助于识别问题。在我的产品代码中,我将目标的
From
属性设置为显示名称,而不是实际的From email id。无论如何,如果您可以发布包含评论内容的答案,我会将其标记为已接受的答案。
<nlog>
    <targets async="true">
      <target name="mail" type="Mail"
              body="${date:format=yyyy-MM-dd HH\:mm\:ss} - ${level} [${logger}] - ${message} ${newline}${newline}${event-context:item=OtherInfo}${newline}${newline}${exception:maxInnerExceptionLevel=2:format=ToString}${newline}${newline}" 
              subject="[${machinename}] ${logger}"
              to="mail@domain.com" encoding="UTF-8" from="anotheremail@domain.com" smtpServer="" enableSsl="true" smtpAuthentication="Basic"
      />
    </targets>
    <targets>
      <target name="mailsync" type="Mail" body="${date:format=yyyy-MM-dd HH\:mm\:ss} - ${level} [${logger}] - ${message} ${newline}${newline}${event-context:item=OtherInfo}${newline}${newline}${exception:maxInnerExceptionLevel=2:format=ToString}${newline}${newline}" subject="[${machinename}] ${logger}"
              to="mail@domain.com" encoding="UTF-8" from="anotheremail@domain.com" smtpServer="" enableSsl="true" smtpAuthentication="Basic"
      />
    </targets>
    <rules>
      <logger name="*" level="Error" writeTo="mail" />
    </rules>
</nlog>
var config = LogManager.Configuration;
const string targetName = "mail";

var wrapper = (AsyncTargetWrapper) config.FindTargetByName(targetName);
wrapper.WrappedTarget.SmtpServer = "hostname";
wrapper.WrappedTarget.SmtpUserName = "username";
wrapper.WrappedTarget.SmtpPassword = "password";

config.RemoveTarget(targetName);
config.AddTarget(targetName, wrapper);

LogManager.Configuration = config;