문제

엔터프라이즈 라이브러리 예외 처리 6.0을 설정하여 엔터프라이즈 라이브러리 로깅 응용 프로그램 블록에서 XmlexceptionFormatter를 사용하여 오류를 기록하려고합니다.나는 오류를받지 못하고, 나는 로깅을 자체로 일할 수 있습니다.

여기에 내 app.config가 콘솔 응용 프로그램에서 테스트 중입니다.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="loggingConfiguration" 
             type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
    <section name="exceptionHandling" 
             type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
  </configSections>

  <loggingConfiguration name="Logging Application Block" 
                        tracingEnabled="false" 
                        defaultCategory="General" 
                        logWarningsWhenNoCategoriesMatch="true">
    <listeners>
      <add name="XML Trace Listener" 
           type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.XmlTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
           listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.XmlTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
           fileName="C:\logs\TestDriver-XML.log" 
           traceOutputOptions="None" />
    </listeners>
    <categorySources>
      <add switchValue="All" name="General">
        <listeners>
          <add name="XML Trace Listener" />
        </listeners>
      </add>
    </categorySources>
    <specialSources>
      <allEvents switchValue="All" name="All Events">
        <listeners>
          <add name="XML Trace Listener" />
        </listeners>
      </allEvents>
      <notProcessed switchValue="All" name="Unprocessed Category">
        <listeners>
          <add name="XML Trace Listener" />
        </listeners>
      </notProcessed>
      <errors switchValue="All" name="Logging Errors &amp; Warnings">
        <listeners>
          <add name="XML Trace Listener" />
        </listeners>
      </errors>
    </specialSources>
  </loggingConfiguration>

  <exceptionHandling>
    <exceptionPolicies>
      <add name="Policy">
        <exceptionTypes>
          <add name="All Exceptions" 
               type="System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
               postHandlingAction="None">
            <exceptionHandlers>
              <add name="Logging Exception Handler" 
                   type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                   logCategory="General" 
                   eventId="100" 
                   severity="Error" 
                   title="Enterprise Library Exception Handling"
                   formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.XmlExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                   priority="0" />
            </exceptionHandlers>
          </add>
        </exceptionTypes>
      </add>
    </exceptionPolicies>
  </exceptionHandling>

  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
</configuration>
.

내 테스트는 다음과 같습니다.

class Program
{
    public static void Main(string[] args)
    {
        TestExceptionManager();

        Console.ReadLine();
    }

    private static void TestExceptionManager()
    {
        ExceptionManager exceptionManager = new ExceptionManager();
        exceptionManager.Process(MyExceptionCode, "Policy");
    }

    private static void MyExceptionCode()
    {
        throw new Exception("A basic Exception");
    }
}
.

이 항목을 올바르게 기록 할 수있는 제안이 있습니까?

도움이 되었습니까?

해결책

버전 6에서는 팩토리 메소드를 사용해야합니다.

using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using System;

namespace ExceptionHandlingExamples
{
    class Program
    {
        public static void Main(string[] args)
        {
            try
            {
                TestExceptionManager();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            Console.ReadLine();
        }

        private static void TestExceptionManager()
        {
            IConfigurationSource config = ConfigurationSourceFactory.Create();
            ExceptionPolicyFactory factory = new ExceptionPolicyFactory(config);
            Logger.SetLogWriter(new LogWriterFactory().Create());

            ExceptionManager exceptionManager = factory.CreateManager();

            exceptionManager.Process(MyExceptionCode, "Policy");
        }

        private static void MyExceptionCode()
        {
            throw new Exception("A basic Exception");
        }
    }
}
.

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