我想用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