2016-07-16 69 views
0

我正在爲連接到硬件設備上的供應商Rest Api的REST端點進行集成測試。我的軟件必須定期在物理設備上進行測試。我創建了一個測試項目(VSTest VS 2015),我想將日誌記錄添加到我的測試工具中。我意識到log4net上有大量的文檔,但我仍然無法使它工作。我的目標是登錄到控制檯以及一個文件。供應商想要一個日誌文件來驗證我的測試已完成。如何使用VS 2015配置log4net進行集成測試?

首先,初始化log4net以讀取獨立文件。

using log4net.Config; 
using log4net; 
using Microsoft.VisualStudio.TestTools.UnitTesting; 
using System.Diagnostics; 

namespace MyProgram 
{ 
    [TestClass] 
    public class AssemblyInitializer 
    { 
     private static ILog log = null; 

     [AssemblyInitialize] 
     public static void Configure(TestContext Context) 
     { 
      Debug.WriteLine("Starting log4net setup and configuration"); 

      XmlConfigurator.Configure(new FileInfo("log4net.properties")); 
      log = LogManager.GetLogger(typeof(AssemblyInitializer)); 
      log.Debug("log4net initialized for Integration Test"); 

      Debug.WriteLine("Completed log4net setup and configuration"); 

     } 
    } 
} 

我log4net.properties文件:

<?xml version="1.0" encoding="UTF-8"?> 
<configuration> 
<log4net> 
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender"> 
     <layout type="log4net.Layout.PatternLayout"> 
      <conversionpattern value="%date [%thread] %-5level %logger{1} - %message%newline"/> 
     </layout> 
    </appender> 
    <appender name="FileAppender" type="log4net.Appender.FileAppender"> 
     <file value="IntegrationTests.log" /> 
     <appendToFile value="true" /> 
     <layout type="log4net.Layout.PatternLayout"> 
      <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> 
     </layout> 
    </appender> 
    <root> 
     <level value="DEBUG"> 
      <appender-ref ref="ConsoleAppender"/> 
      <appender-ref ref="FileAppender"/> 
     </level> 
    </root> 
</log4net> 
</configuration> 

我不認爲測試資源管理器讀取Assembly.Info文件,但我包括在以防萬一的參考。

[assembly: log4net.Config.XmlConfigurator(Watch = false)] 

我已經試過幾乎所有我能想到的。我沒有想法。任何幫助,將不勝感激。

+4

你實際上沒有說錯了什麼,所以我猜這個文件沒有在你期望的位置創建。但是有一些注意事項:1)獨立的log4net配置文件不需要被包裝在''部分中。 2)您正在從「log4net.properties」文件加載配置文件,但是它是否存在於您期望的位置? 3)啓用log4net內部調試,並檢查輸出。 – stuartd

+0

您是否考慮過使用NLog來代替?我發現圍繞他們的配置方法更容易。據我所知,它提供了所有相同的功能。 –

+0

謝謝您的意見。我只是回到這個問題,所以很抱歉拖延。我刪除了我正在使用的標籤,以擺脫VS中的綠色漣漪。我驗證了正在讀取log4nt.properties文件。我繼續寫下了一個新的功能,這個功能正在工作 - 我會在下面發表。 – Shawn

回答

0

下面的代碼工作。它不是相同的實現,但我正在使兩個記錄器工作。看到我上面的評論。我相信日誌文件可能位於未知位置,或者我應該一直訪問存儲庫。下面的代碼在我的測試運行之前工作良好並初始化,並根據需要記錄我的測試結果。

using log4net; 
using log4net.Config; 
using log4net.Repository.Hierarchy; 
using log4net.Core; 
using log4net.Appender; 
using log4net.Layout; 
using Microsoft.VisualStudio.TestTools.UnitTesting; 

namespace MyProgram 
{ 
    [TestClass] 
    public class AssemblyInitializer 
    { 

     private static ILog log = null; 

     [AssemblyInitialize] 
     public static void Configure(TestContext Context) 
     { 
      Debug.WriteLine("Starting log4net setup and configuration"); 

      Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository(); 
      hierarchy.Root.RemoveAllAppenders(); 

      PatternLayout patternLayout = new PatternLayout(); 
      patternLayout.ConversionPattern = "%date [%thread] %-5level %logger - %message%newline"; 
      patternLayout.ActivateOptions(); 

      FileAppender fileAppender = new FileAppender(); 
      fileAppender.Name = "Integration Test FileAppender"; 
      fileAppender.File = @"Logs\IntegrationTest.log"; 
      fileAppender.AppendToFile = false; 
      fileAppender.Layout = patternLayout; 
      fileAppender.ActivateOptions(); 
      hierarchy.Root.AddAppender(fileAppender); 

      ConsoleAppender consoleAppender = new ConsoleAppender(); 
      consoleAppender.Name = "Integration Test ConsoleAppender"; 
      consoleAppender.Target = Console.Out.ToString(); 
      consoleAppender.ActivateOptions(); 
      consoleAppender.Layout = patternLayout; 
      hierarchy.Root.AddAppender(consoleAppender); 

      hierarchy.Root.Level = Level.Debug; 
      hierarchy.Configured = true; 

      log = LogManager.GetLogger(typeof(AssemblyInitializer)); 
      log.Debug("log4net initialized for Integration Test"); 

      Debug.WriteLine("Completed log4net setup and configuration"); 

     } 
    } 
} 
-1

我打算擴充我的評論。因爲Log4Net也讓我瘋狂。

這就是我的我的測試項目中的NLog的App.config中的所有內容。

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
<configSections> 
    <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/> 
    </configSections> 
    <appSettings> 
    </appSettings> 
    <nlog autoReload="true" 
     throwExceptions="true" 
     internalLogLevel="Debug" internalLogFile="c:\temp\nlog-test.log" > 
    <targets> 
     <target type="Console" name="console" error="true"/> 
    </targets> 
    <rules> 
     <logger name="*" writeTo="console" /> 
    </rules> 
    </nlog> 
</configuration> 

你定義一個目標是用於記錄一個目的地,然後指定記錄器寫入該目標。

他們希望您使用LoggingManger爲每個要記錄的類創建記錄器(源)。但我只是將一個記錄器包裝在一個靜態類中,並創建了一些pass-thru方法。你失去了記錄器可以爲你自動捕獲的一些細節(例如記錄調用的確切位置),但是我通常在我正在寫入的日誌中有充足的細節。 〜 Nlog在Github https://github.com/NLog/NLog上。 Log4Net仍然是svn。這告訴我一些事情。

在NuGet上還有一些可用於Nlog的Windows窗體目標。

+0

''他們希望你使用LoggingManger爲你想要登錄的每個類創建一個記錄器(一個源)。'這就是我使用[this.Log()](https://github.com/ferventcoder/) this.Log) – hometoast

相關問題