2010-08-22 24 views
6

更新 - 現在無需回答,我已在下面解決。您好,我想在.NET中實現一個自定義的跟蹤監聽器,但有一個問題通過配置文件添加跟蹤監聽器。無法通過應用程序配置連接自定義tracelistener類 - ConfigurationErrorsException

我發現堆棧溢出的類似帖子,但它似乎沒有幫助(How to define custom TraceListener in app.config)。

異常消息是:

ConfigurationErrorsException - 「無法創建ApplicationFramework.TraceListeners.TextLogTraceListener,ApplicationFramework.TraceListeners,版本= 1.0.0.0,文化=中立,公鑰=空」。

正如你在下面的代碼中看到的,我甚至在沒有嘗試的情況下使用了AssemblyQualified名稱。

config和dll存在於引用偵聽器的應用程序中。

任何人都可以發現我在這裏可能會做錯嗎?

C#代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.IO; 

namespace ApplicationFramework.TraceListeners 
{ 
    public class TextLogTraceListener : System.Diagnostics.TextWriterTraceListener 
    { 
     public override void Write(string message) 
     { 
      using (FileStream fs = new FileStream("C:\\Test\\trace.log", FileMode.Append)) 
      { 
       StreamWriter sw = new StreamWriter(fs); 

       sw.Write(message); 
      } 
     } 

     public override void WriteLine(string message) 
     { 
      using (FileStream fs = new FileStream("C:\\Test\\trace.log", FileMode.Append)) 
      { 
       StreamWriter sw = new StreamWriter(fs); 

       sw.Write(message); 
      } 
     } 
    } 
} 

配置:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 

    <system.diagnostics> 

     <trace autoflush="true" indentsize="4"> 
     <listeners> 
      <add name="TextListener" 
       type="ApplicationFramework.TraceListeners.TextLogTraceListener, ApplicationFramework.TraceListeners, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" 
       initializeData="trace.log" /> 
      <remove name="Default" /> 
     </listeners> 
     </trace> 

    </system.diagnostics> 

</configuration> 

簡單的跟蹤呼叫引用的應用程序:

Trace.WriteLine("Test"); 

回答

12

不用擔心,我現在已經解決了這個問題。

我需要重寫構造函數重載之一:

公共TextLogTraceListener(字符串名稱):基地(名) {

}

+0

+1 Thx爲此,從無用的錯誤消息中看不出這是你需要做的。 – 2013-05-07 18:10:43

0

我想如果你從網站中刪除的InitializeData配置和派生的構造函數沒有任何參數,你會很好,你似乎並不需要你使用的構造函數,因爲你手動定義FileStream。

相關問題