【发布时间】:2016-01-12 22:08:28
【问题描述】:
我正在运行一个多线程 C# .NET 4.5 应用程序。
在我的本地 Windows 7 笔记本电脑上运行,NLog 记录我的所有消息。 我使用 Visual Studio 发布应用程序...将已发布的应用程序复制到 Windows Server 2008...并在服务器上运行该应用程序:这会导致跳过日志消息。有人可以帮助我了解为什么以及如何解决此问题或建议 NLog 的替代方案吗?
我的配置文件如下:
我在没有“async = true”的情况下试过这个
<targets async="true">
<target xsi:type="ColoredConsole"
name="ColoredConsole"
layout="${date} ${level} ${message} ${event-context:item=AlgID} " />
<target name="xmlfile" xsi:type="File"
fileName="C:\QRT\Logs\LogEmiter.Nlog.xlog" keepFileOpen="true"
layout="${log4jxmlevent}" />
<target xsi:type="File"
name ="LogFile" fileName="C:\QRT\Logs\QRTLog-${shortdate}.log"
layout ="${date:format=yyyy-MM-dd HH\:mm\:ss.fff}|${level}|${threadid}|${threadname}|${event-properties:item=FromID}|${message}${exception:format=tostring} "/>
<target xsi:type="File"
name ="TapeLogFile" fileName="C:\QRT\Logs\QRTMarketLog.txt"
layout ="${date:format=yyyy-MM-dd HH\:mm\:ss.fff}|${level}|${message}"/>
</targets>
<rules>
<!-- add your logging rules here -->
<logger name ="TapeLogFile" minlevel="Trace" writeTo="TapeLogFile" />
<logger name ="TapeLogFile" minlevel="Trace" writeTo="ColoredConsole" final="true"/>
<logger name="*" minlevel="Trace" writeTo="ColoredConsole" />
<logger name="*" minlevel="Trace" writeTo="xmlfile" />
<logger name="*" minlevel="Trace" writeTo="LogFile" />
<!--
Write all events with minimal level of Debug (So Debug, Info, Warn, Error and Fatal, but not Trace) to "f"
<logger name="*" minlevel="Debug" writeTo="f" />
-->
</rules>
</nlog>
我以这种方式记录消息:
在我的典型课程中,我会这样初始化我的 NLog 助手类:
private void InitliazlieLogger()
{
LogManager.ThrowExceptions = true;
m_logger = new NLogHelper(LogManager.GetCurrentClassLogger());
m_logger.Set("FromID", "Email"); // Class setting.
}
private void DoSomething(int _x)
{
m_logger.Debug ("Print this statement: {0}", _x);
}
我有一个 NLog 助手类...
public NLogHelper(Logger Logger)
{
m_logger = Logger;
m_properties = new Dictionary<string, object>();
}
public void Debug(string Message, params object[] Args)
{
m_logger.Debug()
.Message(Message, Args)
.Properties(m_properties)
.Write();
notify(Severity.Debug, Message);
}
问题是一些日志消息被跳过了。 我添加了自己的日志记录类,它手动写入文件并在 NLogHelper.Debug 中插入了对 is 的调用,我发现我手动编写的文件有一组完整的日志消息,但 NLog 输出缺少一些。
同样,这适用于我的笔记本电脑,但在 Windows Server 2008 上失败。我的服务器安装了 .NET 4.6.1。我的 Windows 7 机器上似乎只有 4.5.2。会这样吗?
附带说明:如果我将所有源文件上传到服务器并在服务器上使用 Visual Studio 编译完全相同的代码,NLog 似乎可以工作?!?
-困惑。
谢谢 -埃德
【问题讨论】:
-
'm_logger.Debug()' 将在源处于调试模式时记录。不是吗?这意味着当您在服务器上发布源代码时,这些二进制文件将以 RELEASE 模式运行。这意味着它将跳过日志记录语句。你不觉得吗?
-
您提到,在您的服务器上,您从 VS 运行源代码,因此您再次以 DEBUG 模式运行它。对吗?
-
你是对的。我尝试了几种在服务器上运行的方法。我发现“发布”需要在目标系统上“安装”,因此很难找到配置文件,所以我现在只需复制整个 bin/Debug 目录。无论如何,Logger.Debug 日志工作。丢失的是 Logger.Info。