There are a system stored procedure sp_who which returns information regarding current sessions on given database.
There is an extended version of this stored procedure is also available.
Sharing experiences of day to day life as Developer.
public static void ProcessError(Exception exception, string otherDetails = "")
{
StackTrace stackTrace = new StackTrace();
string methodName = stackTrace.GetFrame(1).GetMethod().Name;
string className = stackTrace.GetFrame(1).GetMethod().DeclaringType.Name;
string classDetail = string.Format("{0}.{1}", className, methodName);
string exceptionDetail = string.Format("Error occurred in {0}. {1}Message:{2}",
classDetail,
Environment.NewLine,
exception.Message
);
if (!string.IsNullOrWhiteSpace(otherDetails))
{
exceptionDetail = string.Format("{0}{1}Other Details: {2}.",
exceptionDetail,
Environment.NewLine,
otherDetails
);
}
// send this detail for saving
LogError(exceptionDetail);
}
// to get name of calling method
string methodName = stackTrace.GetFrame(1).GetMethod().Name;
// to get name of calling class
string className = stackTrace.GetFrame(1).GetMethod().DeclaringType.Name;
Now lets decide where to save this detail
public enum LogMethods
{
Database,
File,
Email
};
...
...
public static void LogError(string exceptionDetail)
{
switch (errorLogMethod)
{
case LogMethods.Database:
LogErrorInDatabase(exceptionDetail);
break;
case LogMethods.File:
LogErrorInFile(exceptionDetail);
break;
case LogMethods.Email:
SendErrorLog(exceptionDetail);
break;
case default:
SendErrorLog(exceptionDetail);
break;
}
}
Now last step, implement save methods
public static void logErrorMessageInDB(string exceptionDetail, string methodName)
{
try
{
MyDataContext myDC = DataContext;
Log lerror = new Log
{
FullDescription = exceptionDetail,
MethodName = methodName,
CreateDataTime = DateTime.Now
};
iDC.Log.InsertOnSubmit(lerror);
iDC.SubmitChanges();
}
catch (Exception exp)
{
/*
LogErrorInFile(exp);
// OR
LogErrorInEventLog(exp);
*/
}
}