【问题标题】:NLog not writing from referenced dllNLog 不从引用的 dll 写入
【发布时间】:2023-03-28 18:46:01
【问题描述】:

我创建了一个用于发送电子邮件的 dll。我在该项目中包含了 NLog,该项目记录到 c:\logs{logfilename.log}

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>

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

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

这行在邮件发送后调用我的日志记录方法。

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 版本是否相同?
  • 是的,都是 3.2.0.0
  • NLog.config 可能是您的问题,除非您在运行时进行配置。在您的输出目录中查看它。
  • 规则是完全一样的……不被引用的时候就有效,所以好像不太可能。
  • 用更多信息更新了帖子...有什么想法吗?

标签: c# logging dll nlog


【解决方案1】:

好的,所以我终于在记录跟踪后弄清楚发生了什么......我注意到它只显示错误规则已加载......所以我将“emaillog”规则移到“错误”上方规则,一切都很完美。

【讨论】:

    【解决方案2】:

    检查你的 nlog.config 如果不。在代码中注入一些配置是否有任何可能性..

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

    【讨论】:

    • 我认为没有注入任何配置,因为我没有对配置进行任何编程操作。我在配置中定义了所有内容。我将发布配置和相关代码,以防万一我的盲点中完全缺少“我不知道,我不知道”的东西。将在几分钟内更新原始帖子。
    • 我已经用配置和依赖它的代码更新了帖子。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多