Click here to Skip to main content
15,897,718 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Suppose in an application at a time multiple users are online. And because of some reason 4 users got exceptions. As we have global exception handling implemented in our Global.asax file, so we show a custom error page to all the user and log the error using below code:

C#
var currentException = Server.GetLastError();
logger.LogDetails(currentException); // some logging code
Server.ClearError();


User 1 got Divide by zero exception.
User 2 got invalid cast exception.
User 3 got Index out of range exception.
User 4 got some Sqlexception.

What will this Server.GetLastError() return in this case?

Also, I need to log the exceptions. So think of it as, I want to log exception based on user as key and Exception details as description. So for User 1, there will be a log entry something like User=U1, ExceptionDescription="Divide By zero".

I can get current user using HTTPContext but still not able to get exception that is occurred in their session. How can I achieve this?

What I have tried:

It seems "Server" is not specific to a user. So if we try to read GetLastError from this it'll return whatever the last exception occurred.
Posted
Updated 6-Dec-16 8:16am
v4

1 solution

C#
GetLastError: is thread safe. It retrieves the calling thread's last-error code value. The last-error code is maintained on a per-thread basis. Multiple threads do not overwrite each other's last-error code. 


C#
var currentException = Server.GetLastError();
var user = HttpContext.Current.User;
logger.LogDetails(currentException, user); // some logging code
Server.ClearError();
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900