2009-07-24 29 views
60

Tôi đã triển khai trình theo dõi tùy chỉnh (bắt nguồn từ TextWriteTraceListener) và bây giờ tôi muốn đặt ứng dụng của tôi để sử dụng nó thay vì tiêu chuẩn TextWriteTraceListener.Cách xác định TraceListener tùy chỉnh trong app.config

Đầu tiên tôi đã thêm mặc định TextWriteTraceListener để đảm bảo rằng nó hoạt động tốt và nó hoạt động. Dưới đây là App.config của tôi:

<configuration> 
    <system.diagnostics> 
     <trace autoflush="true" indentsize="4"> 
      <listeners> 
       <add name="TextListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="trace.log" /> 
      <remove name="Default" /> 
      </listeners> 
     </trace> 
    </system.diagnostics> 
</configuration> 

Bây giờ dấu vết của tôi nghe được định nghĩa trong namespace MyApp.Utils và nó được gọi là FormattedTextWriterTraceListener. Vì vậy, tôi đã thay đổi kiểu trong cấu hình trên để MyApp.Utils.FormattedTextWriterTraceListener và nó hiện trông như thế:

<configuration> 
    <system.diagnostics> 
     <trace autoflush="true" indentsize="4"> 
      <listeners> 
       <add name="MyTextListener" type="MyApp.Utils.FormattedTextWriterTraceListener" initializeData="trace.log" /> 
      <remove name="Default" /> 
      </listeners> 
     </trace> 
    </system.diagnostics> 
</configuration> 

Tuy nhiên bây giờ khi tôi cố gắng đăng nhập một cái gì đó tôi nhận được một ConfigurationErrorsException với thông điệp:

Couldn't find type for class MyApp.Utils.FormattedTextWriterTraceListener.

Có ai biết làm thế nào tôi có thể thiết lập người nghe tùy chỉnh này trong cấu hình và nếu nó thậm chí có thể?

Trả lời

75

Cố gắng xác định một hội đồng quá, như vậy:

<configuration> 
    <system.diagnostics> 
     <trace autoflush="true" indentsize="4"> 
      <listeners> 
       <add name="TextListener" 
        type="MyApp.Utils.FormattedTextWriterTraceListener, MyApp" 
        initializeData="trace.log" /> 
      <remove name="Default" /> 
      </listeners> 
     </trace> 
    </system.diagnostics> 
</configuration> 
+0

tên hội là 'MyApp' và tôi nói thêm rằng để loại lập luận – RaYell

+1

tôi thay đổi nội dung bài viết, cố gắng để xác định như thế –

+1

này được đưa ra một thông điệp khác nhau (tương tự loại ngoại lệ) 'Không thể tạo MyApp.Utils.FormattedTextWriterTraceListener, MyApp. – RaYell

3

Tôi đã đấu tranh với điều này thời gian gần đây và chỉ trong trường hợp nó giúp mọi người ...

tôi biết loại của tôi tồn tại vì vậy tôi đã viết như sau:

Assembly assembly = System.Reflection.Assembly.GetAssembly(typeof("yourclassname")); 
Type myClassType = assembly.GetType("yournamespace.yourclassname"); 

trong trường hợp của tôi, myClassType.AssemblyQualifiedName chứa chuỗi tôi cần thiết trong file app.config của tôi trong thuộc tính type.

Ví dụ:

enter image description here

<system.diagnostics> 
    <sources> 
     <source name="System.ServiceModel" switchValue="Information,ActivityTracing" propagateActivity="true"> 
     <listeners> 
      <add name="CircularTraceListener" /> 
     </listeners> 
     </source> 
    </sources> 
    <sharedListeners> 
     <add name="CircularTraceListener" type="Microsoft.Samples.ServiceModel.CircularTraceListener, CircularTraceListener, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" 
      initializeData="C:\MyWebService\APILog\CircularTracing-service.svclog" maxFileSizeKB="1000" /> 
    </sharedListeners> 
    <trace autoflush="true" /> 
    </system.diagnostics> 
Các vấn đề liên quan