【发布时间】:2015-06-18 16:58:05
【问题描述】:
我有一个 N 层项目,其中一个项目是错误记录类库。为此,我为 Elmah 添加了 Nuget 包。本质上,我所做的是从引发异常的方法中获取额外信息,将其打包到我创建的新异常中,然后将其提供给 ELMAH。
类库可以添加到解决方案中的其他项目(服务层,UI)。当没有 HTTP 上下文时,我会立即写入文件。
string details = BuildExceptionString(exception, extraDetails, arguments);
LoggingModuleException ex = new LoggingModuleException(details);
// We may have come from a website, or a backend layer. Handle both.
if (HttpContext.Current != null)
{
ErrorSignal.FromCurrentContext().Raise(ex); // ELMAH Signaling
}
else
{
// Not a web app so no httpcontext
System.IO.File.WriteAllText(@"C:\MyLogs\Log.txt", ex.Message);
}
我的问题是,当我从 UI (MVC) 层调用这个库代码时,我确实有一个 HTTP 上下文,它调用了 ELMAH。但是我不知道错误在哪里! 我的 UI 层需要 ELMAH 才能调用 Raise() 吗?
当我将 ELMAH 添加到我的类库时,我没有创建任何 .config 文件。我添加了一个 App.config,其中包含我从另一个问题中复制的一些 ELMAH 设置。我已经将它粘贴在下面,但我不能说它正在被类库使用......
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="elmah">
<section name="security" requirePermission="false" type="Elmah.SecuritySectionHandler, Elmah"/>
<section name="errorLog" requirePermission="false" type="Elmah.ErrorLogSectionHandler, Elmah" />
<section name="errorMail" requirePermission="false" type="Elmah.ErrorMailSectionHandler, Elmah" />
<section name="errorFilter" requirePermission="false" type="Elmah.ErrorFilterSectionHandler, Elmah"/>
<section name="errorTweet" requirePermission="false" type="Elmah.ErrorTweetSectionHandler, Elmah"/>
</sectionGroup>
</configSections>
<elmah>
<security allowRemoteAccess="yes" />
<errorLog type="Elmah.XmlFileErrorLog, Elmah" logPath="C:\MyLogs\Elmah\" />
</elmah>
<system.web>
<httpModules>
<add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah"/>
</httpModules>
<httpHandlers>
<add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
</httpHandlers>
</system.web>
<location path="elmah.axd">
<system.web>
<authorization>
<deny users="?"/>
</authorization>
</system.web>
</location>
</configuration>
【问题讨论】:
标签: elmah