Simple way configure Logging in ASP.NET WebForms Application

C# PROGRAMMING

This article shows how to set up logging using ELMAH.

ELMAH (Error Logging Modules and Handlers) is an application-wide error logging facility that is completely pluggable. It can be dynamically added to a running ASP.NET web application, or even all ASP.NET web applications on a machine, without any need for re-compilation or re-deployment.

Once ELMAH has been dropped into a running web application and configured appropriately, you get the following facilities without changing a single line of your code:

  • Logging of nearly all unhandled exceptions.
  • A web page to remotely view the entire log of recorded exceptions.
  • A web page to remotely view the full details of anyone logged exception, including colored stack traces.
  • In many cases, you can review the original yellow screen of death that ASP.NET generated for a given exception, even with customErrors mode turned on.
  • An e-mail notification of each error at the time it occurs.
  • An RSS feed of the last 15 errors from the log.

How to add logging?

Install nuget package into ASP.NET Web Forms application as shown below

Nuget Console

After installation is complete, few lines of code will be added into web.config file as shown below

https://gist.github.com/ssukhpinder/3e7e73d5102cde2982a8b4462abc0c96

Github Sample

https://github.com/ssukhpinder/LoggingExampleASP.NET

Demo

Logging Demo As per below steps

In above sample, we deliberately added divide by zero exception in “About.aspx” to show unhandled exception using ELMAH.

Step 1: Ctrl + F5 to run the application locally.

Step 2: Application will generate unhandled exception when route to “About” page as shown below

Exception Generated

Step 3: Open another tab & redirect to https://localhost:44369/elmah.axd (NOTE that port no may vary) and generated exception will be shown as below

ELMAH Console to view exceptions

User view each exception details & export them into log file as well.

For more technical articles on angular & other languages:

https://github.com/ssukhpinder/LoggingExampleASP.NET

Follow me on LinkedIn Instagram Facebook Twitter

Leave a comment