14

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_Debug' in the Application field in the Elmah_Error table. Does anyone have any experience with this? Or is there another way to do it?

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
Mark Struzinski
  • 32,945
  • 35
  • 107
  • 137

2 Answers2

53

This can now be done purely in markup. Just add an applicationName attribute to the errorLog element in the <elmah> section of the web.config file. Example:

<errorLog type="Elmah.SqlErrorLog, Elmah" 
    connectionStringName="connectionString" applicationName="myApp"  />

I've tested this and it works both when logging an exception and when viewing the log via Elmah.axd.

In the case of the OP, one would imagine it can be set programatically too but I didn't test that. For me and I imagine for most scenarios the markup approach is sufficient.

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
6

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.

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
JamesEggers
  • 12,885
  • 14
  • 59
  • 86
  • 3
    Pretty minor quibble, but I like this pattern better: return _applicationName ?? (_applicationName = HttpRuntime.AppDomainAppId); – Joel Mueller Apr 24 '09 at 19:47
  • In the stored procedures in Elmah's database there are filters on application name. I've changed all the stored procedures in my environment to ignore this filter so that I can view all errors in a single console. – Ben Liyanage Aug 15 '12 at 21:22