从Outlook加载项

时间:2016-11-15 05:39:26

标签: c# outlook add-in nlog

我有一个用于Outlook的C#加载项(加载项快速),我试图存储一些日志数据,但即使对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时,会显示日志文件不存在的消息,尽管目标文件正确,表明配置文件已成功读取。

当包含在可执行文件中时,相同的代码按预期工作。

2 个答案:

答案 0 :(得分:4)

除了@ SimonKarvis的回答,nlog.config的位置可能很难。单元测试也是如此。

我建议:

  1. 从C#创建配置,例如

     var target = new FileTarget
     {
           FileName = logfile,
           ReplaceFileContentsOnEachWrite = true,
           CreateDirs = createDirs
     };
     var config = new LoggingConfiguration();
    
     config.AddTarget("logfile", target);
    
     config.AddRuleForAllLevels(target);
    
     LogManager.Configuration = config;
    
  2. 或者从字符串加载配置:

    string configXml = "<nlog>...<nlog>";
    XmlDocument doc = new XmlDocument();
    doc.LoadXml(configXml);
    var config = new XmlLoggingConfiguration(doc.DocumentElement, Environment.CurrentDirectory);
    LogManager.Configuration = config;
    
  3. 或者至少但不是最后一次,找到nlog.config和&#34; feed&#34;的正确路径。它到NLog。

    var pathToNlogConfig = "c:\..";
    var config = new XmlLoggingConfiguration(pathToNlogConfig);
    LogManager.Configuration = config;
    

答案 1 :(得分:0)

另一个StackOverflow问题(How to use NLog for a DLL)建议NLog.config需要放在与调用外接程序的可执行文件相同的目录中。这解决了这个问题。但是,这使得分发非常困难,因为Outlook可执行文件的位置将根据Outlook版本而有所不同,并且需要管理员权限才能将文件复制到其中。也许另一个记录器不需要这个。

感谢来自@Julian的建议,以下代码以编程方式指定了NLog配置并从加载项成功运行:

    using NLog;
    using NLog.Config;
    using NLog.Targets;
    using System.Xml;
...

public static Logger logger = LogManager.GetCurrentClassLogger();

....   
    public static void ConfigNLog()
            {

            string xml = @"
            <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>
            </nlog>";

            StringReader sr = new StringReader(xml);
            XmlReader xr = XmlReader.Create(sr);
            XmlLoggingConfiguration config = new XmlLoggingConfiguration(xr, null);
            NLog.LogManager.Configuration = config;
        }