我想用EntLib Logging實現日誌記錄,併爲類別「Debugging」掛接兩個TraceListeners。一個會將這些消息寫入文件,另一個會將它們輸出到系統跟蹤輸出,與Debug.Write一樣(這樣我就可以使用Sysinternals DbgView監視它們),但是我找不到如何使用格式化程序設置第二個監聽器我需要。我真正需要的僅僅是消息,但是它輸出了大量的東西,比如EventId,Priority等等。我怎麼把這些東西都剪掉了?如何使用Enterprise Library Logging將消息寫入調試輸出?
11
A
回答
15
我發現在MSDN一個很好的演練:Creating a Custom Trace Listener
它不正是我需要的。這是一個完整的代碼,我結束了:
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);
}
}
}
配置文件:
<?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}
" 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}
Message: {message}
Category: {category}
Priority: {priority}
EventId: {eventid}
Severity: {severity}
Title:{title}
Machine: {machine}
Application Domain: {appDomain}
Process Id: {processId}
Process Name: {processName}
Win32 Thread Id: {win32ThreadId}
Thread Name: {threadName}
Extended Properties: {dictionary({key} - {value}
)}"
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 & Warnings" />
</specialSources>
</loggingConfiguration>
</configuration>
和使用是這樣的:
Debug.Write("Debug.Write test");
Logger.Write("EntLib test", "Debugging");
無論是在調試輸出最終被容易跟蹤DBGVIEW。
0
在您的應用程序的EntLib配置中,指定您希望使用哪個Formatter。默認格式化程序包含所有這些信息。要刪除您不感興趣的信息,請將它們從您當前正在使用的TextFormatter中刪除,或者創建一個包含所需字段的新文本格式化程序,並更改「調試」以使用新的格式程序。
+2
這正是我所做的,但看起來像DefaultTraceListener不支持格式化程序。 – bychkov 2009-06-18 15:22:54
相關問題
- 1. 將調試消息寫入Xcode輸出窗口
- 2. 如何使用Enterprise-Library 5.0更新poco
- 3. Android的調試輸出按鍵消息
- 4. 如何使用鍵盤輸入在屏幕上寫入消息?
- 5. @Enterprise Library Unity屬性注入
- 6. Corrupt Enterprise Library 5.0.msi?
- 7. 使用Prism 4.0配置Enterprise Library 5.0
- 8. iLMerge Enterprise Library 5.0
- 9. 如何在一個MySQL Workbench窗格中寫出調試消息?
- 10. Enterprise Library 5.0記錄應用程序塊:使用DatabaseTraceListener記錄Custome消息
- 11. 如何使用Enterprise Library 5.0實現使用System.Transactions.Transaction?
- 12. 如何在NuGet中輸出來自install.ps1的調試消息
- 13. 如何調試Java消息輸出始終爲空的原因?
- 14. 如何在C#中使用Enterprise Library處理oracle異常?
- 15. 如何使用Android的API將中文消息寫入NDEF消息?
- 16. 如何減少Enterprise Library 5.0記錄內存使用情況?
- 17. MVC UpdateModel可以使用Enterprise Library VAB嗎?
- 18. 如何調試消息
- 19. .net Mef with Enterprise Library 5.0
- 20. Enterprise Library 4.1和.Net4
- 21. 我如何學習Enterprise Library 4.0?
- 22. 如何使用Eclipse和Android輸出其他調試信息JUnit
- 23. Enterprise Library Logger的配置
- 24. 使用Enterprise Library 4.1的動態DateTimeRangeValidator?
- 25. 使用ruby和stomp將消息寫入ActiveMQ
- 26. 可能使用Enterprise Library從.Net調用Oracle FUNCTION?
- 27. commons-logging在Cassandra中調試
- 28. 如何將簡單消息插入到JUnit輸出中?
- 29. 如何將SQL消息選項卡輸出插入到表中?
- 30. 如何禁用WebSocket的編寫調試消息
非常感謝您使用XML - 它極大地幫助我解決了從演練中無法解決的問題! (我沒有看到他們提到的設置偵聽器數據類型屬性的地方,這讓我發瘋) – GrahamMc 2016-04-08 13:52:14