Is there any way to programmatically set the application name in Elmah?

后端 未结 2 2073
野性不改
野性不改 2021-01-02 00:33

I need to change the app name based on what configuration I\'m using in Visual Studio. For example, if I\'m in Debug configuration, I want the app name to show as \'App_Debu

2条回答
  •  北海茫月
    2021-01-02 01:09

    By default, Elmah uses the AppPool's application GUID as the default application name. It uses this as the key to identify the errors in the Elmah_Error table when you look at the web interface that's created through it's HTTP Module.

    I was tasked to explore this option for my company earlier this year. I couldn't find a way to manipulate this by default since Elmah pulls the application name from HttpRuntime.AppDomainAppId in the ErrorLog.cs file. You could manipulate it by whatever key you want; however, that is the AppPool's GUID.

    With that said, I was able to manipulate the ErrorLog.cs file to turn Elmah into a callable framework instead of a handler based one and allow for me set the ApplicationName. What I ended up doing was modifying ErrorLog.cs to include a property that allowed me to set the name as below:

    public virtual string ApplicationName
    {
        get 
        {
            if (_applicationName == null) {  _applicationName = HttpRuntime.AppDomainAppId; }
            return _applicationName;
        }
        set { _applicationName = value; }
    }
    

    What you will probably need to do is adjust this differently and set the ApplicationName not to HttpRuntime.AppDomainAppId but, instead, a value pulled from the web.config. All in all, it's possible. The way I did it enhanced the ErrorLog.Log(ex) method so I could use Elmah has a callable framework beyond web applications. Looking back I wish I did the app/web.config approach instead.

    One thing to keep in mind when changing the application name in Elmah. The http handler that generates the /elmah/default.aspx interface will no longer work. I'm still trying to find time to circle back around to such; however, you may need to look into creating a custom interface when implementing.

提交回复
热议问题