c# – 将Enterprise Library 5迁移到6
作者:互联网
试图从Enterprise Library 5.0移动到6.0,我遇到了一些麻烦.也许这会有助于其他人.
以下配置是(静默)异常记录到数据库的示例:
<configSections>
<section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging" requirePermission="true" />
<section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling" requirePermission="true" />
</configSections>
<loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
<listeners>
<add name="Database Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.Database.FormattedDatabaseTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging.Database"
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Database.Configuration.FormattedDatabaseTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging.Database"
databaseInstanceName="LogContext" writeLogStoredProcName="WriteLog"
addCategoryStoredProcName="AddCategory" formatter="Text Formatter" />
</listeners>
<formatters>
<add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging"
template="Timestamp: {timestamp}{newline}..."
name="Text Formatter" />
</formatters>
<categorySources>
<add switchValue="All" name="Exception">
<listeners>
<add name="Database Trace Listener" />
</listeners>
</add>
</categorySources>
</loggingConfiguration>
<exceptionHandling>
<exceptionPolicies>
<add name="UiPolicy">
<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"
logCategory="Exception" 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>
我正在使用以下版本5.0.414.0的程序集:
Microsoft.Practices.EnterpriseLibrary.ExceptionHandling
Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging
Microsoft.Practices.EnterpriseLibrary.Logging
Microsoft.Practices.EnterpriseLibrary.Logging.Database
使用Enterprise Library 5.0在asp.net MVC应用程序中使用所有这些就像这样简单:
// ...
catch (Exception ex)
{
if (ExceptionPolicy.HandleException(ex, "UiPolicy")) throw;
// do something else
}
将程序集升级到Enterprise Library 6.0后,我收到以下异常消息:
Must set an ExceptionManager in the ExceptionPolicy class using the SetExceptionManager method.
那么该怎么办?
解决方法:
一步步
为了摆脱这个异常(归功于Randy’s answer),我必须使用ExceptionManager,在我的情况下通过工厂.我在global.asax的Application_Start()方法中使用它:
protected void Application_Start()
{
IConfigurationSource source = ConfigurationSourceFactory.Create();
var exceptionPolicyFactory = new ExceptionPolicyFactory(source);
var exceptionManager = exceptionPolicyFactory.CreateManager();
ExceptionPolicy.SetExceptionManager(exceptionManager);
}
但是我的处理和记录设置只有一半.我现在在exceptionPolicyFactory.CreateManager()中得到以下异常
The LogWriter has not been set for the Logger static class. Set it invoking the Logger.SetLogWriter method.
为此,我补充道
var logwriterFactory = new LogWriterFactory(source);
var logWriter = logwriterFactory.Create();
Logger.SetLogWriter(logWriter);
现在它说
Database provider factory not set for the static DatabaseFactory. Set a provider factory invoking the DatabaseFactory.SetProviderFactory method or by specifying custom mappings by calling the DatabaseFactory.SetDatabases method.
在logwriterFactory.Create()上.所以还有一件事要补充:
DatabaseFactory.SetDatabaseProviderFactory(new DatabaseProviderFactory(source));
完整解决方案
现在,我的Application_Start()就是
protected void Application_Start()
{
IConfigurationSource source = ConfigurationSourceFactory.Create();
DatabaseFactory.SetDatabaseProviderFactory(new DatabaseProviderFactory(source));
var logwriterFactory = new LogWriterFactory(source);
var logWriter = logwriterFactory.Create();
Logger.SetLogWriter(logWriter);
var exceptionPolicyFactory = new ExceptionPolicyFactory(source);
var exceptionManager = exceptionPolicyFactory.CreateManager();
ExceptionPolicy.SetExceptionManager(exceptionManager);
}
而配置和实际的异常处理代码保持不变:
ExceptionPolicy.HandleException(exceptionToHandle, "UiPolicy");
标签:c,exception-handling,asp-net-mvc,enterprise-library,logging 来源: https://codeday.me/bug/20190608/1200901.html