NLog没有从引用的dll中写入

本文关键字:dll 引用 NLog | 更新日期: 2023-09-27 17:49:18

我创建了一个用于发送电子邮件的dll。我有NLog包含在该项目中,记录到c:'logs{logfilename.log} <——这是一个错误或事件日志。当在本地使用项目时,它工作得很好,并在测试期间写入文件。当我从另一个也有NLog的项目引用电子邮件dll时,它不会输出到日志文件。电子邮件dll中的配置位于引用它的新项目的bin目录中。我可以使用跟踪从新项目创建日志,但它没有打印电子邮件dll条目。有什么特别的我需要在我的新项目做得到电子邮件dll写日志?我已经搜索了这个问题的答案,但关键字没有产生我需要的结果。我刚接触NLog,请温柔点。

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 xsi:type="File"
        name="default"
        layout="${longdate} - ${level:uppercase=true}: ${message}${onexception:${newline}EXCEPTION': ${exception:format=ToString}}"
        fileName="C:'logs'default.log"
        keepFileOpen="false"
        archiveFileName="C:'logs'NTC_Utility'default.{##}.log"
        archiveNumbering="Sequence"
        archiveEvery="Day"
        maxArchiveFiles="30"
        />
<target xsi:type="File"
    name="error"
    layout="${longdate} - ${level:uppercase=true}: ${message}${onexception:${newline}EXCEPTION': ${exception:format=ToString}}"
    fileName="C:'logs'error.log"
    keepFileOpen="false"
    archiveFileName="C:'logs'NTC_Utility'error.{##}.log"
    archiveNumbering="Sequence"
    archiveEvery="Day"
    maxArchiveFiles="90"
        />
<target xsi:type="File"
        name="emailLog"
        layout="-------------------- ${message} (${longdate}) --------------------${newline}
From: ${event-context:item=From}${newline}
To: ${event-context:item=To}${newline}
BCC: ${event-context:item=Bcc}${newline}
CC: ${event-context:item=CC}${newline}
Subject: ${event-context:item=Subject}${newline}
Body: ${event-context:item=Body}${newline}
Attachments: ${event-  context:item=Attachments}${newline}--------------------------------------------------------------------${newline}"
        fileName="C:'logs'EmailLog.log"
        keepFileOpen="false"
        archiveFileName="C:'logs'NTC_Utility'EmailLog_.{##}.log"
        archiveNumbering="Sequence"
        archiveEvery="Day"
        maxArchiveFiles="90"
        />
</targets>
<rules>
<logger name="*" writeTo="error" level="Error" final="true" />
<logger name="*" writeTo="emailLog" level="Info" final="true" />
<logger name="*" writeTo="default" minLevel="Debug" />
</rules>
</nlog>

这是我的Log.cs从编译的实用程序dll

using NLog;
namespace NTC.Utility
{
    internal static class Log
    {
        public static Logger Instance { get; private set;}
        static Log()
        {
            LogManager.ReconfigExistingLoggers();
            Instance = LogManager.GetCurrentClassLogger();
        }
    }
}

这一行在邮件发送后调用我的Logging Method。

LogEmailSent(imperEmail);

调用这个方法…

    private void LogEmailSent(EmailMessage email)
    {            
        Logger logger = LogManager.GetCurrentClassLogger();
        LogEventInfo thisEvent = new LogEventInfo(LogLevel.Info, "default","Email Sent");
        thisEvent.Properties["From"] = email.From;
        thisEvent.Properties["To"] = EmailCollectionToCsv(email.ToRecipients);            
        thisEvent.Properties["Bcc"] = EmailCollectionToCsv(email.BccRecipients);
        thisEvent.Properties["CC"] = EmailCollectionToCsv(email.CcRecipients);
        thisEvent.Properties["Subject"] = email.Subject;
        thisEvent.Properties["Body"] = email.Body;
        thisEvent.Properties["Attachments"] = AttachmentCollectionToCsv(email.Attachments);
        logger.Log(thisEvent);
    }

NLog没有从引用的dll中写入

好的,所以我终于弄清楚记录跟踪后发生了什么…我注意到它只显示加载....的错误规则所以我把"emaillog"规则移到了"error"规则之上,一切都很顺利。

检查你的nlog.config如果不是。有没有可能在代码中注入了一些配置?

http://www.codeproject.com/Articles/10631/Introduction-to-NLog