2009-06-18 7 views
11

Tôi muốn thực hiện ghi nhật kí với EntLib Logging và treo hai TraceListeners cho danh mục "Debugging". Người ta sẽ viết những thông điệp đó vào file và chúng sẽ xuất chúng ra đầu ra dấu vết hệ thống giống như cách Debug.Write thực hiện (để tôi có thể theo dõi chúng với Sysinternals DbgView), nhưng tôi không thể tìm cách thiết lập trình lắng nghe thứ hai với trình định dạng Tôi cần. Tất cả những gì tôi thực sự cần chỉ là thông điệp nhưng nó xuất ra toàn bộ các công cụ, như EventId, Priority vv. Làm cách nào để cắt bỏ tất cả những thứ này?Làm thế nào để viết thư chỉ để gỡ lỗi đầu ra với Enterprise Library Logging?

Trả lời

15

Tôi tìm thấy một hương đẹp trên MSDN: Creating a Custom Trace Listener

Nó thực hiện chính xác những gì tôi cần. Dưới đây là một mã đầy đủ tôi đã kết thúc với:

using System; 
using System.Diagnostics; 
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration; 
using Microsoft.Practices.EnterpriseLibrary.Logging; 
using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration; 
using Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners; 

namespace Common.Utils 
{ 
    [ConfigurationElementType(typeof(CustomTraceListenerData))] 
    public class FormattedDebugWriterTraceListener : CustomTraceListener 
    { 
     public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, object data) 
     { 
      if (data is LogEntry && this.Formatter != null) 
      { 
       this.WriteLine(this.Formatter.Format(data as LogEntry)); 
      } 
      else 
      { 
       this.WriteLine(data.ToString()); 
      } 
     } 

     public override void Write(string message) 
     { 
      Debug.Write(message); 
     } 

     public override void WriteLine(string message) 
     { 
      Debug.WriteLine(message); 
     } 

    } 
} 

Tập tin cấu hình:

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
    <configSections> 
    <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> 
    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> 
    </configSections> 
    <loggingConfiguration name="Logging Application Block" tracingEnabled="true" 
    defaultCategory="General" logWarningsWhenNoCategoriesMatch="true"> 
    <listeners> 
     <add listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.CustomTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" 
     traceOutputOptions="None" type="Common.Utils.FormattedDebugWriterTraceListener, Common.Utils" 
     name="FormattedDebugWriterTraceListener" initializeData="" formatter="SimpleMessageFormatter" /> 
     <add fileName="log\Debugging.log" rollSizeKB="0" timeStampPattern="yyyy-MM-dd" 
     rollFileExistsBehavior="Overwrite" rollInterval="Week" formatter="GeneralTextFormatter" 
     header="----------------------------------------" footer="----------------------------------------" 
     listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" 
     traceOutputOptions="None" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" 
     name="RollingFlatFileTraceListener" /> 
    </listeners> 
    <formatters> 
     <add template="{message}&#xD;&#xA;" type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" 
     name="SimpleMessageFormatter" /> 
     <add template="Timestamp: {timestamp}&#xD;&#xA;Message: {message}&#xD;&#xA;Category: {category}&#xD;&#xA;Priority: {priority}&#xD;&#xA;EventId: {eventid}&#xD;&#xA;Severity: {severity}&#xD;&#xA;Title:{title}&#xD;&#xA;Machine: {machine}&#xD;&#xA;Application Domain: {appDomain}&#xD;&#xA;Process Id: {processId}&#xD;&#xA;Process Name: {processName}&#xD;&#xA;Win32 Thread Id: {win32ThreadId}&#xD;&#xA;Thread Name: {threadName}&#xD;&#xA;Extended Properties: {dictionary({key} - {value}&#xD;&#xA;)}" 
     type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" 
     name="GeneralTextFormatter" /> 
    </formatters> 
    <categorySources> 
     <add switchValue="All" name="Debugging"> 
     <listeners> 
      <add name="FormattedDebugWriterTraceListener" /> 
      <add name="RollingFlatFileTraceListener" /> 
     </listeners> 
     </add> 
     <add switchValue="All" name="General" /> 
    </categorySources> 
    <specialSources> 
     <allEvents switchValue="All" name="All Events" /> 
     <notProcessed switchValue="All" name="Unprocessed Category" /> 
     <errors switchValue="All" name="Logging Errors &amp; Warnings" /> 
    </specialSources> 
    </loggingConfiguration> 
</configuration> 

Và việc sử dụng đi như thế này:

Debug.Write("Debug.Write test"); 
Logger.Write("EntLib test", "Debugging"); 

Cả hai kết thúc trong kết xuất debug dễ dàng theo dõi bởi DbgView.

+0

Cảm ơn bạn rất nhiều vì đã bao gồm XML - nó hỗ trợ tôi rất nhiều với thứ gì đó mà tôi không thể làm được từ hướng dẫn! (Tôi không thấy nơi họ đề cập đến việc thiết lập thuộc tính kiểu dữ liệu người nghe, và nó đã khiến tôi phát điên) – GrahamMc

0

Trong cấu hình EntLib cho ứng dụng của bạn, bạn chỉ định Trình định dạng bạn muốn sử dụng. Trình định dạng mặc định bao gồm tất cả thông tin này. Để loại bỏ thông tin mà bạn không quan tâm, hãy xóa chúng khỏi TextFormatter mà bạn hiện đang sử dụng hoặc tạo một trình định dạng văn bản mới chứa các trường bạn muốn và thay đổi "Gỡ lỗi" để sử dụng trình định dạng mới của bạn.

+2

Đó chính xác là những gì tôi đã làm, nhưng có vẻ như DefaultTraceListener không hỗ trợ trình định dạng. – bychkov