In C#, how do I throw a Custom Exception that contains the Stack Trace? The Call Stack would also work for my logging requirements.
The custom exception is defined like this...
public class UserNameRequiredExcp : Exception
{
public UserNameRequiredExcp() : base() { ExceptionLogging.WriteToLog(this); }
public UserNameRequiredExcp(string message) : base(message) { ExceptionLogging.WriteToLog(this); }
public UserNameRequiredExcp(string message, Exception exception ) : base(message, exception) { ExceptionLogging.WriteToLog(this); }
}
It is thrown like this ...
public void SetUserName(string userName)
{
if (String.IsNullOrEmpty(userName))
{ throw new UserNameRequiredExcp(); }
_userName = userName;
}
When the ExceptionLogging method executes the stack trace is null?
internal static void WriteToLog(Exception exception)
{
string stackTrace = exception.StackTrace;
}