Elmah应用程序名称配置

时间:2017-04-20 15:03:47

标签: asp.net vb.net web-config elmah

我想使用Elmah将多个客户的ASP.net Web窗体应用程序中的错误记录到中央错误数据库,每个错误都记录一个唯一的应用程序名称。

我意识到这可以在web.config中的elmah.errorLog.applicationName属性中轻松设置,但我们所有客户都有标准的web.config文件,其中包含appSettings.config和connectionStrings.config文件中每个系统的唯一设置由web.config中的configSourcefile属性引用。

由于web.config中的elmah sectionGroup不允许使用fileconfigSource,如何为Elmah设置applicationName每个系统都是唯一的?

我不想以编程方式在web.config中设置属性,因为将来覆盖web.config时可能会丢失该属性。

我尝试在Global.asax.Application_Start()中设置ErrorLog.ApplicationName属性,就像这样,但我设置的值被忽略了。

Dim logger As Elmah.ErrorLog = Elmah.ErrorLog.GetDefault(Nothing)
logger.ApplicationName = "Testing 123"

任何建议都会非常感激,因为Elmah符合我们的所有需求,我们只需要能够唯一地识别它在中央错误数据库中记录的错误。

2 个答案:

答案 0 :(得分:2)

发现this SO post后,我现在设法为Elmah设置外部配置文件。

elmah的{​​{1}}部分如下所示:

web.config

其内容如下; elmahErrorLog.config:

  <elmah>
        <errorLog configSource="Config\Elmah\elmahErrorLog.config" />        
        <errorMail configSource="Config\Elmah\elmahErrorMail.config" />
        <errorFilter configSource="Config\Elmah\elmahErrorFilter.config" />        
        <security allowRemoteAccess="false" />
  </elmah>

<errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="ErrorLog" applicationName="My Unique Application Name" /> (此配置可阻止本地主机请求的电子邮件通知):

elmahErrorFilter.config

<errorFilter> <test> <and> <equal binding="Context.Request.IsLocal" value="True" type="Boolean" /> <regex binding="FilterSourceType.Name" pattern="mail" /> </and> </test> </errorFilter>

elmahErrorMail.config

答案 1 :(得分:1)

如果您的部署系统允许每个客户进行单独的应用程序设置,您可以在ELMAH中使用错误过滤,以使用应用程序设置中的应用程序名称来丰富所有错误。查看此博客文章了解详细信息:Enrich ELMAH errors using error filtering hook

简而言之,您解除所有错误并记录一个新错误,包括来自config的应用程序名称:

void ErrorLog_Filtering(object sender, ExceptionFilterEventArgs args)
{
    var httpContext = args.Context as HttpContext;
    if (httpContext != null)
    {
        var error = new Error(args.Exception, httpContext);
        error.ApplicationName = ConfigurationManager.AppSettings["AppName"];
        ErrorLog.GetDefault(httpContext).Log(error);
        args.Dismiss();
    }
}