我有一個用於存儲某些日誌數據但未創建日誌文件的Outlook(加載項快速)的C#加載項即使對記錄器的調用不會失敗。我在Win 10環境中使用VS 2013。從Outlook加載項調用時,NLog不會寫入日誌文件
我NLog.Config文件(存儲在文件夾中OutlookAddin \ BIN \調試,在同一位置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時,雖然目標文件是正確的,但表明日誌文件不存在,但表明配置文件已被成功讀取。
相同的代碼在包含在可執行文件中時按預期工作。
請註明作爲一個答案:) – Julian