【发布时间】:2016-11-15 05:39:25
【问题描述】:
我有一个用于 Outlook 的 C# 插件(Add-In Express),我试图从中存储一些日志数据,但即使对 logger 的调用没有失败,也没有创建日志文件。我在 Win 10 环境中使用 VS 2013。
我的 NLog.Config 文件(存放在文件夹 OutlookAddin\bin\Debug,与 OutlookAddIn.dll.config 相同的位置)如下:
<?xml version="1.0" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target name="file" xsi:type="File"
layout="${longdate} ${logger} ${message}"
fileName="${specialfolder:ApplicationData}\FindAlike\NewMails.txt"
keepFileOpen="false"
encoding="iso-8859-2" />
</targets>
<rules>
<logger name="*" writeTo="file" />
</rules>
插件中的代码是声明:
public AddinModule()
{
Application.EnableVisualStyles();
InitializeComponent();
// Please add any initialization code to the AddinInitialize event handler
}
private ADXOutlookAppEvents adxOutlookEvents;
private DateTime LastReceivedDate = DateTime.Now;
private Timer mailCheckTimer;
public static RegistryKey SoftwareKey = Registry.CurrentUser.OpenSubKey("Software", true);
public static RegistryKey AppNameKey = SoftwareKey.CreateSubKey("FindAlike");
public static Logger logger = LogManager.GetCurrentClassLogger();
测试日志文件写入的例程是:
public static void TestNLog()
{
try
{
NLog.LogManager.ThrowExceptions = true;
logger.Info("test1");
logger.Warn("test2");
logger.Error("test3");
var fileTarget1 = (FileTarget)NLog.LogManager.Configuration.FindTargetByName("file");
var logEventInfo = new LogEventInfo { TimeStamp = DateTime.Now };
string fileName = fileTarget1.FileName.Render(logEventInfo);
if (!System.IO.File.Exists(fileName))
throw new Exception("Log file does not exist.");
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
}
}
调用TestNLog时,出现日志文件不存在的Message,虽然目标文件是正确的,说明配置文件已经读取成功。
当包含在可执行文件中时,相同的代码会按预期工作。
【问题讨论】:
-
请标记一个为答案:)