【问题标题】:Configuring NLog E-Mail Message Body Output配置 NLog 电子邮件消息正文输出
【发布时间】:2011-04-20 22:20:41
【问题描述】:

我正在使用 NLog v2 Beta 记录一个 VB.NET .DLL,它创建并向第三方服务发送消息。登录到文件时我让它工作得很好,但现在希望它也通过电子邮件将它自动捕获的错误发送给我。以下是我的 NLog.config 文件的相关位:

<?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="papercut" xsi:type="BufferingWrapper" bufferSize="100">
            <target xsi:type="PostFilteringWrapper" defaultFilter="level >= LogLevel.Debug">
                <target xsi:type="Mail"
                    name="papercut"
                    subject="Your app has errors"
                    to="ToAddress@Domain.com"
                    from="FromAddress@Domain.com"
                    smtpServer="127.0.0.1"
                    smtpPort="25"
                    body={longdate}|{message} />
            </target>
        </target> 
    </targets>

    <rules>
        <logger name="*" minlevel="Debug" writeTo="papercut"  />
    </rules>
</nlog>

默认情况下,它只会在电子邮件正文中列出记录的消息。我想在此之前记录日期/时间,所以一直在玩 body= 部分无济于事(它要么没有正确评估变量或崩溃 NLog)。有人可以给我一个关于如何配置 NLog 来做到这一点的指针吗?

【问题讨论】:

    标签: nlog


    【解决方案1】:

    似乎您在正文配置中缺少 $,这可能是 NLog 崩溃的原因..

    body = "${longdate}| ${message}"
    

    有关邮件目标的更多信息

    https://github.com/NLog/NLog/wiki/Mail-target

    您也可以为 NLog 错误本身启用日志记录,如下所示..

    <nlog 
        xmlns="http://www.nlog-project.org/schemas/NLog.xsd" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
        autoReload="true"
        throwExceptions="true"
        internalLogFile="Nloglog.log"
        internalLogLevel="Warn"
        >
    
    
    .....
    
    
    </nlog>
    

    【讨论】:

    • 你能在body属性周围加一些引号吗?
    • @guanome 我已经添加了引号。感谢您指出这一点。
    猜你喜欢
    • 2021-02-06
    • 1970-01-01
    • 1970-01-01
    • 2019-08-04
    • 1970-01-01
    • 2012-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多