엔터프라이즈 라이브러리 로깅으로 디버그 출력에 메시지를 작성하는 방법은 무엇입니까?

StackOverflow https://stackoverflow.com/questions/1011106

  •  06-07-2019
  •  | 
  •  

문제

Entlib 로깅으로 로깅을 구현하고 "디버깅"카테고리에 대한 두 개의 tracelisteners를 연결하고 싶습니다. 하나는 해당 메시지를 파일에 작성하고 다른 메시지는 디버그와 같은 방식으로 시스템 추적 출력에 출력하여 (sysinternals dbgview로 모니터링 할 수 있도록) 그러나 Formatter를 사용 하여이 두 번째 리스너를 설정하는 방법을 찾을 수는 없습니다. 나는 필요하다. 내가 정말로 필요한 것은 단지 메시지이지만 EventId, 우선 순위 등과 같은 모든 것들을 출력합니다. 어떻게이 모든 것을 잘라 내야합니까?

도움이 되었습니까?

해결책

MSDN에서 멋진 연습을 발견했습니다. 사용자 정의 추적 리스너 생성

그것은 내가 필요한 것을 정확히 수행합니다. 다음은 다음과 같은 전체 코드입니다.

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에서 쉽게 추적 할 수있는 디버그 출력으로 끝납니다.

다른 팁

앱의 Entlib 구성에서 사용하려는 Formatter를 지정합니다. 기본 포맷터에는이 모든 정보가 포함되어 있습니다. 정보를 제거하려면 관심이없는 정보를 제거하려면 현재 사용중인 Textformatter에서 제거하거나 원하는 필드가 포함 된 새 텍스트 포맷터를 만들고 새 Formatter를 사용하기 위해 "디버깅"을 변경하십시오.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top