我有一个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才能使调用引发()工作吗?
当我将ELMAH添加到类库时,没有创建任何.config文件。我添加了一个带有一些ELMAH设置的App.config,我复制了另一个问题。我把它贴在下面,但我不能说类库正在使用它.
<?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>发布于 2015-06-19 09:53:10
我找到了答案。
线索在于,如果您需要一些appSettings,类库不使用自己的App.config,而是从调用者的.config中提取它们。因此,对于调用这个类库的MVC站点,appSettings应该位于web.config中。
因此,为了让已安装了ELMAH的类库在有http上下文(来自网站)时使用ELMAH登录,网站的web.config需要ELMAH配置信息,类库将读取和使用这些信息。
<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" />
</sectionGroup>
</configSections>//.
<system.web>
//...
<httpModules>
<add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" />
<add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" />
<add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" />
</httpModules>//.
</runtime>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules>
<add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" preCondition="managedHandler" />
<add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" preCondition="managedHandler" />
<add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" preCondition="managedHandler" />
</modules>
</system.webServer>
<elmah>
<security allowRemoteAccess="false" />
</elmah>
<location path="elmah.axd" inheritInChildApplications="false">
<system.web>
<httpHandlers>
<add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
</httpHandlers>
</system.web>
<system.webServer>
<handlers>
<add name="ELMAH" verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" preCondition="integratedMode" />
</handlers>
</system.webServer>
</location>https://stackoverflow.com/questions/30921421
复制相似问题