Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

asp.net

elmah

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?

like image 905
Mark Struzinski Avatar asked Sep 30 '08 13:09

Mark Struzinski


2 Answers

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.

like image 115
Stephen Kennedy Avatar answered Nov 13 '22 22:11

Stephen Kennedy


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.

like image 28
JamesEggers Avatar answered Nov 13 '22 23:11

JamesEggers