异常处理应用程序块未写入事件日志

本文关键字:入事件 日志 程序块 应用 应用程序 异常处理 | 更新日期: 2023-09-27 18:32:59

我正在尝试配置异常处理应用程序块以将异常写入事件日志。

我创建了以下配置:

<exceptionHandling>
    <exceptionPolicies>
        <add name="Default">
            <exceptionTypes>
                <add name="All Exceptions"
                     type="System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
                     postHandlingAction="NotifyRethrow">
                    <exceptionHandlers>
                        <add name="Logging Exception Handler"
                             type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                             logCategory="General"
                             eventId="100"
                             severity="Error"
                             title="Enterprise Library Exception Handling"
                             formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling"
                             priority="0" />
                    </exceptionHandlers>
                </add>
            </exceptionTypes>
        </add>
    </exceptionPolicies>
</exceptionHandling>
<loggingConfiguration name="Default"
                      tracingEnabled="true"
                      defaultCategory="General">
    <listeners>
        <add name="Event Log Listener"
             type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
             listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
             source="Enterprise Library Logging"
             formatter="Text Formatter"
             log=""
             machineName="."
             traceOutputOptions="None" />
    </listeners>
    <formatters>
        <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
             template="Timestamp: {timestamp}{newline}&#xA;Message: {message}{newline}&#xA;Category: {category}{newline}&#xA;Priority: {priority}{newline}&#xA;EventId: {eventid}{newline}&#xA;Severity: {severity}{newline}&#xA;Title:{title}{newline}&#xA;Machine: {localMachine}{newline}&#xA;App Domain: {localAppDomain}{newline}&#xA;ProcessId: {localProcessId}{newline}&#xA;Process Name: {localProcessName}{newline}&#xA;Thread Name: {threadName}{newline}&#xA;Win32 ThreadId:{win32ThreadId}{newline}&#xA;Extended Properties: {dictionary({key} - {value}{newline})}"
             name="Text Formatter" />
    </formatters>
    <categorySources>
        <add switchValue="All" name="General">
            <listeners>
                <add name="Event Log Listener" />
            </listeners>
        </add>
    </categorySources>
    <specialSources>
        <allEvents switchValue="All" name="All Events" />
        <notProcessed switchValue="All" name="Unprocessed Category" />
        <errors switchValue="All" name="Logging Errors &amp; Warnings">
            <listeners>
                <add name="Event Log Listener" />
            </listeners>
        </errors>
    </specialSources>
</loggingConfiguration>

我尝试使用以下方法测试此配置:

ExceptionPolicy.HandleException(new Exception("TEST"), "Default");

此代码执行没有任何问题,但不会写入事件日志。我怀疑这只是一个配置错误,但我无法确定导致这种情况的原因。

关于可能出现的问题或如何调试它的任何建议?

异常处理应用程序块未写入事件日志

通过从最新的 Nuget 版本切换到版本 5.0.414.0 来修复。