【问题标题】:ELMAH in a class library, does the web UI consumer need ELMAH too?类库中的 ELMAH,Web UI 消费者是否也需要 ELMAH?
【发布时间】: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


    【解决方案1】:

    我找到了答案。

    线索在于,如果您需要一些 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>
    

    【讨论】:

      猜你喜欢
      • 2010-09-10
      • 2012-07-29
      • 2016-11-10
      • 2014-11-03
      • 1970-01-01
      • 1970-01-01
      • 2011-01-05
      • 2016-06-06
      • 2020-05-03
      相关资源
      最近更新 更多