微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

.net – 如何使用NLog记录的电子邮件错误?

我第一次使用NLog,我想出了如何写一个文本文件,但现在我想发送一封电子邮件给自己。我假定当您提供SMTP凭据给NLog时。程序集调用System.Net.Mail命名空间并处理发送电子邮件。如果这是错误的请告诉我。如果你这样做,我会欣赏任何信息,你采取了什么,你完成发送电子邮件

下面是我的配置。

<?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 name="logfile" xsi:type="File" fileName="C:\Users\keithb\Desktop\TestLog.txt" />-->
    <target name="Mail" xsi:type="Mail" html="true" subject="Error Received" body="${message}"         
         to="user2@someemaill.com"
         from="user@someemail.com"
         Encoding="UTF8"
         smtpUsername="user@someemail.com"
         enableSsl="False"
         smtpPassword="pa$$word"
         smtpAuthentication="Basic"
         smtpServer="mail.someemail.com"
         smtpPort="25" />
  </targets>
  <rules>
    <!--<logger name="*" minlevel="Debug" writeto="logfile" />-->
    <logger name="*" level="Error" writeto="Mail" />
    <logger name="*" level="Fatal" writeto="Mail" />
  </rules>
</nlog>

调用错误这样

Imports NLog

Public Class HandleGetRouteInfo
    Private Shared logger As Logger = LogManager.GetCurrentClassLogger()

    Public Shared Function GetRouteInfo(ByVal routeInfo As RequestGetRouteInfo) As GetRouteInfoResponse
        'Todo: Check route ID,username,password
        logger.Fatal("User" & routeInfo.UserName & "has entered the GetRouteInfo Method.")
        logger.Error("User" & routeInfo.UserName & "has entered the GetRouteInfo Method.")
        Dim str As String = logger.Name
End Function
End Class

我试图让它发送错误,包含消息,它通常记录到文件的东西,到我的电子邮件。我知道如何捕获异常并通过电子邮件发送给我自己,但我认为NLog有能力做到这一点。任何使用电子邮件功能的简单示例的教程都可以工作。我发现了很多东西,但不能得到它的工作。如果你这样做,一些示例代码或解释我需要做什么将有所帮助。我不知道它是什么,我做错了。任何人都有什么想法?

将您的编码从UTF8更改为UTF-8。

假设您的SMTP设置中没有其他错误(这通常是未发送邮件的原因),它应该工作。

原文地址:https://www.jb51.cc/vb/256295.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐