如何只编写消息以调试Enterprise Library Logging的输出? [英] How to write just message to debug output with Enterprise Library Logging?

查看:96
本文介绍了如何只编写消息以调试Enterprise Library Logging的输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用EntLib Logging实现日志记录,并为类别"Debugging"连接两个TraceListeners.一个将这些消息写入文件,另一个将它们输出到系统跟踪输出,就像Debug.Write一样(以便我可以使用Sysinternals DbgView监视它们),但是我找不到如何使用格式化程序来设置第二个侦听器,我需要.我真正需要的只是消息,但它会输出一堆东西,例如EventId,Priority等.我该如何切掉所有这些东西?

I want to implement logging with EntLib Logging and hook up two TraceListeners for category "Debugging". One will write those messages to file and other will output them to system trace output the same way Debug.Write does (so that I could monitor them with Sysinternals DbgView), but I can't find how to setup this second listener with formatter that I need. All I really need is just message but it outputs whole bunch of stuff, like EventId, Priority etc. How do I cut all this stuff out?

推荐答案

我在MSDN上找到了一个不错的演练:创建自定义跟踪侦听器

I found a nice walkthrough on MSDN: Creating a Custom Trace Listener

它确实满足我的需求.这是我最后得到的完整代码:

It does exactly what I need. Here is a full code I ended up with:

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}&#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>

用法如下:

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

两者最终都出现在DbgView可以轻松跟踪的调试输出中.

Both end up in debug output easily traceable by DbgView.

这篇关于如何只编写消息以调试Enterprise Library Logging的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆