如何通过电子邮件发送日志记录,NLOG错误? [英] How do I email errors logged with NLog?

查看:246
本文介绍了如何通过电子邮件发送日志记录,NLOG错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用NLOG首次,我想通了如何写入一个文本文件,但现在我要发送电子邮件至我自己。想提出的假设是,当你提供SMTP凭据NLOG。该组件调用System.Net.Mail命名空间和处理发送电子邮件。如果这是错了,请告诉我。如果你之前,我会AP preciate这样做的什么了,你完成发送电子邮件的任何信息。

I am using NLog for the first time, i figured out how to write to a text file, but now i want to send an email to myself. am making the assumption that when you supply SMTP credentials to NLog. The assembly calls the System.Net.Mail namespace and handles sending an email. If this is wrong please tell me. And if you have done this before i would appreciate any information on what it took you to accomplish send emails.

下面是我的配置。

    <?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>

我打电话,像这样的错误

I call the error like so

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不得不这样做的能力。使用电子邮件功能的死简单的例子,任何教程会工作。我发现了很多东西,但不能得到它的工作。如果你这样做了,还有什么我需要做的一些示例code或解释会有所帮助。我无法弄清楚它是什么我做错了。任何人有什么想法?

I am trying to get it to send the error, containing message, and the things it normally logs to a file, to my email. I know how to catch exceptions and email it to myself, but i thought NLog had the capabilities to do this. Any tutorials with dead simple examples of using email functionality would work. I found a lot of things but cant get it to work. If you have done this, some sample code or explanation of what else i need to do would help. I cant figure out what it is i am doing wrong. Anyone have any ideas?

推荐答案

UTF8 更改编码 UTF-8

假设有在SMTP设置中没有其他错误(通常是消息不被发送的原因),它应该工作。

Assuming there are no other errors in your SMTP settings (which is usually the cause of messages not being sent), it should work.

这篇关于如何通过电子邮件发送日志记录,NLOG错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆