Chiramisu Chiramisu - 3 years ago 202
ASP.NET (C#) Question

Include "Application Name" in Exception Logging

I'm working out an implementation of ELMAH in a web application that logs exceptions to a SQL Server. That's inconsequential, however. My goal is to include the application name in the log so I can easily identify the offending application at a glance when checking error reports. I've searched far and wide spending hours trying to find a solution. I've included

Application Name=[myApp]
in my SQL Server connection string, but that only helps me to identify the app during a SQL Server trace and not in my actual error logging.

Has anyone done this? Here is an example of a log record in my database. I would like to see the Application Name listed as well. This happens to be the ELMAH implementation of the table, but it could just as well have been a custom table with the same field.

enter image description here

I would think this should be as simple as adding a value to the
or something, but I can't seem to find any solution like that or any other. I should probably also note that I'm using IIS 7.5 and .NET 4.0 in case that has anything to do with it. :)

Answer Source

As it is my practice to always fill out the assembly info for all my applications, I have elected to populate this field from My.Application.Info.Title which I just figured out how to access programmatically. To set this field in ELMAH, I set the ApplicationName value of my SqlErrorLog object like so:

Dim se As New Elmah.SqlErrorLog(ConnectionStrings("Elmah").ConnectionString)
se.ApplicationName = My.Application.Info.Title

The assembly info can be set by opening the project properties -> Application tab -> click Assembly Information... -> set "Title" value.

Now when you call the Log function for this object, it will create a database entry including the Application Name.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download