gpt4 book ai didi

c# - 企业库异常处理 6.0 XML 日志记录不写入文件

转载 作者:行者123 更新时间:2023-11-30 20:53:24 25 4
gpt4 key购买 nike

我正在尝试设置 Enterprise Library Exception Handling 6.0,以使用 Enterprise Library Logging Application Block 中的 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");
}
}
}

关于c# - 企业库异常处理 6.0 XML 日志记录不写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20012192/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com