As you might already be aware, there are a lot of libraries and frameworks available, to log the errors that occur in your applications. However, there are some cases when you are unable to find the error in your error log. This happens, especially, when there are any unhandled exceptions.
So, sometimes, it is helpful to look in the Event Log available on your machine (or on your Server).
Let’s see how to create a small exception without handling it and how to view the error in the Event Log.
Create a new Project and select “Empty” template, as shown below. Though you can create any type of project, for the sake of simplicity, I am selecting an empty template.
Also, as shown below, select “Host in the Cloud” option to deploy the site in Azure as an App Service – Web App.
Click “OK” in the above step.
As shown in the above screen capture, provide meaningful names and select the corresponding resources. Finally, click on “Create” button.
It will take a couple of seconds to create a project in your local machine, as well as an App Service – Web App in your subscription.
Create a new web form named as “Default.aspx” and write the following code in the same:
- protected void Page_Load(object sender, EventArgs e)
- {
- throw new Exception("this is an unhanlded exception");
- }
Please note, this is just for simplicity. I created an exception and threw it directly in my Page_Load event.
As soon as you access the “Default.aspx” page, you will receive an error, as shown below.
In this case, I am able to see the error message directly in my browser because I have turned on my errors in the web.config and also, I am running the site in Debug mode. In real-time, for security and performance reasons, you will disable the Error Logs and run in Release mode.
Let’s assume that you are not sure of what is the problem. Now, let’s use Events Log to view the error message.
Go to Windows + R and type “EventVwr” as shown below.
Clicking on “OK” will open the Event Log as shown below.
I expanded the “Windows Logs” and selected “Application”. Selecting the “Application” will display all the errors that occurred in the application, in this local machine, as shown below.
Let’s access the page which throws an error. Now, you can see an error, as shown in the below screen capture, which provides all the StackTrack information.
Using this information, you can fix the error. Though, I have used the above technique in my local machine (which Is running Windows 10 Operating System), the process remains the same, even for other Windows Servers that generally run your Production Environment.
So far, it is simple and easy to log into the machine and navigate to Events Log to view any unhandled exceptions. Now, If you take Azure App Service, which is a Platform as a service, you will not have the facility of logging into the server and connecting to the Event Log Viewer as shown above.
But, there is a way to view those unhandled exceptions in the Event Logs. First, let’s see if our App Service – Web app has been created successfully, by navigation to your App Services in Azure.
Navigate to the Web Portal and you will see something, as shown below.
Remember, we haven’t deployed the Default.aspx page yet. Let’s go ahead and deploy the page by right clicking the Project and selecting Publish.
You can check the following article for more details on how to publish the Web Application to Azure.
Now, browse your article and you get an error, as shown below.
As in Azure, the Error mode is turned “On” by default, you are unable to view the error message.
In order to view the Event Log, navigate to the KUDU application by adding “.scm” to your URL, as shown below.
For more details about “KUDU”, please have a look at “Azure App Service And KUDU - The Deployment Framework”, here:
In Kudu, click on Tools -> Support, as shown below.
You will be navigated to “App Service App Support” console, as shown below.
Now, click on “Analyze” in the above screen capture which will open up the following page.
You can view the complete Error Message, along with the Stack Trace. That’s it. You have learned how to view the Events Log, even in a Platform as a service environment. This should help you fix the unhandled exceptions.
Hope you enjoyed the article. Please give your feedback.