At the moment raygun catches the errors I send using this line
public void SendException(Exception e)
{
new RaygunClient(AppConfiguration.RaygunApiKey).Send(e);
}
I'm looking to edit the exception so that in the message it'll give me more detail about the user and the machine along with the stacktrace. I'm wondering how do I edit the existing message or create a new message that'll also carry the stacktrace.
What I've tried is below but won't bring the stack trace over. May I ask how do I copy stack trace over.
public void SendException(Exception e)
{
var exception = new Exception("Error by " + _user.FullName + " on " + Environment.UserName + " on Machine " + Environment.MachineName, e);
new RaygunClient(AppConfiguration.RaygunApiKey).Send(exception);
}
I am aware that machine name is already provided within the exception.
Related
Does anyone know why Redemption won't successfully logon?
What I'm doing is removing the default folders from a newly created PST. This is not an issue with any machines internally but on a clients machine, they get the following error.
Exception Source: Redemption.RDOSession
Exception Type: System.Runtime.InteropServices.COMException
Exception Message: Not logged on. Please log on first
Exception Target Site: Logoff
public static void DeleteStoreFolders(string sStorePath)
{
RDOSession session = null;
RDOPstStore RDOstore = null;
try
{
_logger.LogDebug("init RDOSession");
session = RedemptionLoader.new_RDOSession();
_logger.LogDebug("RDOSession created");
_logger.LogDebug("Logging into store: " + sStorePath);
RDOstore = session.LogonPstStore(sStorePath);
_logger.LogDebug("Logged into store: " + sStorePath);
foreach (RDOFolder folder in RDOstore.IPMRootFolder.Folders)
{
_logger.LogDebug("Deleting folder " + folder);
folder.Delete();
_logger.LogDebug("Folder " + folder + " deleted");
if (folder != null)
Marshal.FinalReleaseComObject(folder);
}
}
catch (Exception ex)
{
_logger.LogError("Unable to delete default folders. " + ex.StackTrace);
}
finally
{
if (session != null)
{
session.Logoff();
Marshal.FinalReleaseComObject(session);
}
if (RDOstore != null)
Marshal.FinalReleaseComObject(RDOstore);
}
}
It would be great to see the log generated on the problematic machine in addition to the exception details.
Also, I'd suggest replacing the FinalReleaseComObject methods with ReleaseComObject ones. The Marshal.FinalReleaseComObject method releases all references to a Runtime Callable Wrapper (RCW) by setting its reference count to 0. That is not really a good idea. The Marshal.ReleaseComObject method decrements the reference count of the Runtime Callable Wrapper (RCW) associated with the specified COM object. So, if you increase the reference counter, you need to decrease it accordingly, but not set it to zero!
I have a try\catch block that handles opening a connection and inserting data into a database.
catch (SqlException ex)
{
string sqlError = null;
for (int i = 0; i < ex.Errors.Count; i++)
{
sqlError += "Error Message " + ex.Errors[i].Message + "\n" + "Stored Procedure " + ex.Errors[i].Procedure + " \n " + "Line Number " + ex.Errors[i].LineNumber;
}
LogTools.WriteLog("Sql Server error " + sqlError);
Variables.InstallStatusDetail = "Database connection failed";
if (!sqlError.Contains("connection to SQL Server"))
{
if (Variables.WriteToDatabase)
{ HostedDataBase.InsertRunStatusIntoInstallStatus('E', sqlError, null); }
}
}
I want to log sqlexceptions to the database that wont interfere with connecting and logging to the database. The problem occurs when the database cannot be found, or a login does not have the proper permissions, etc. The exception is raised, and it tries to log to the database, so when it does that, it calls the function that writes to the database and tries to access once again, but once again the same exception is raised, resulting in a loop of failed attempts to write to the database (either because the DSN cannot be found, or the user does not have proper permissions).
How can I handle sql errors that would prevent me from being able to access and write to the database, and at the same time still be able to write sql errors that would not cause this loop?
I'm slightly confused by your question but I will attempt to answer it. You have to be careful with how you handle exceptions. If you are going to attempt to reconnect to the database even though an exception was raised the first time, you may want to have some conditions checking what sort of exception was raised. This way, you know to only attempt to re-connect if it was an exception which will not be repeated over and over.
IE.
catch (SqlException ex)
{
Error = ex.ToString()
WriteToLog(Error);
CheckError();
}
Void CheckError()
{
//conditions based on error output.
}
void WriteToLog(string Error)
{
// write your error output to log
}
You should put your logging in it's own try..catch block, like this:
catch (SqlException ex)
{
string sqlError = null;
for (int i = 0; i < ex.Errors.Count; i++)
{
sqlError += "Error Message " + ex.Errors[i].Message + "\n" + "Stored Procedure " + ex.Errors[i].Procedure + " \n " + "Line Number " + ex.Errors[i].LineNumber;
}
LogTools.WriteLog("Sql Server error " + sqlError);
Variables.InstallStatusDetail = "Database connection failed";
if (!sqlError.Contains("connection to SQL Server"))
{
if (Variables.WriteToDatabase)
{
try {
//I am assuming this is where you are trying to write the error back into the database
HostedDataBase.InsertRunStatusIntoInstallStatus('E', sqlError, null);
} catch {
//ignore errors here
}
}
}
}
Unfortunately if you are writing to the same database that you don't have access to, I'm afraid you cannot do that.
I'd suggest you to use something like Log4net which can have multiple appenders (eventlog, database, file, etc). Also, log4net operates something like a fire and forget. Even if log4net has any issues logging the errors, it won't throw exceptions.
I'm working on an application using c#-4.0 and EntireX Broker 9.5.1 I'm attempting to convert to tpcip connections to use ssl. I was successful in upgrading my java code to do the same thing thanks to Java/ Software AGs use of jsse for handling the SSL/ceritificate validation. However, when connecting to the same server/port under .net I'm getting the following error:
02150403: Certificate expired or invalid
The error message sounds like it's communicating. So how can I get .Net to successfully validate the certificate?
PS: The certificate is signed by Verisign.
UPDATE:
Both IE and Firefox successfully connect on https://server.domain:port without throwing certificate errors. My application is actually connecting to ssl://server:port?verify_server=y
verify_server=n throws the same certificate error.
The certificate is not expired. The stack trace from the exception stops right at Broker.Logon();.
at SoftwareAG.EntireX.NETWrapper.Runtime.Broker.Logon(String password)
at FAServer.EntireXDotNetClasses.EntireXWrapper.CreateBroker() in C:\Users\tfburton\Documents\Visual Studio 2010\Projects\NEW FADCOM\FAServer\EntireXDotNetClasses\EntireXWrapper.cs:line 1302
UPDATE:
Here's how I'm creating my Broker object.
try
{
Broker mybroker;
try { mybroker = new Broker(BrokerName, BrokerUser); }
catch (Exception e)
{
Utilities.LogEntry("Error Creating broker instance -- BEFORE LOGON. ", e, true);
throw new WrapperException("Error Creating broker instance -- BEFORE LOGON. "
+ Environment.NewLine + e.Message);
}
try //{ mybroker.Logon(BrokerPass); }
{
mybroker.Password = BrokerPass;
mybroker.Logon(); //<-- stracktrace points here
}
catch (AccessViolationException ave)
{
Utilities.LogEntry("Error During Broker Logon.", ave,
EventLogEntryType.Error);
throw new WrapperException(ave); //wrap and duck
}
}
catch ( Exception e )
{
Utilities.LogEntry("Error Creating broker instance. ", e, true);
XException be = e as XException;
if ( null != be ) //<-- resolves to true
{
String msg = "Certificate error connecting to: " + BrokerName;
throw new WrapperException(msg, e);
}
throw new WrapperException( "Error Creating broker instance. "
+ Environment.NewLine + e.Message); //<-- error caught and re-thrown here
}
Turns out I was barking up the wrong tree.
I'm not sure if .NET has an equivalent for JSSE.
My solution turned out to be a lot simpler, concatinate the root certs in a text file and add
trust_store=path\to\file\myrrootcerts.pem
as a parameter to the url aka
BROKER-ID
I want to change the createuserwizard.step= start if the mail sending fails and not to go to successful creation step.
catch (SmtpException ex)
{
Membership.DeleteUser(textboxemail.Text.Trim());
Literal errorMessage=(Literal) CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("ErrorMessage");
errorMessage.Text = "Account creation failed due to email notification."+ ex.Message + " errorcode" +ex.StatusCode + "; inner exception; " + ex.InnerException;
CreateUserWizard1.CreateUserStep.StepType = WizardStepType.Start;
}
but the exception says the steptype can't be changed. So how to do this. I mean to stop from going to success step.
You are getting that exception because you need to use the Wizard.MoveTo method.
Like this:
CreateUserWizard1.MoveTo(WizardStep1);
Where "WizardStep1" is the ID of the asp:WizardStep that you want to go back to (the "start" step).
Have a windows service that listens to a msmq. In the OnStart method is have this
protected override void OnStart(string[] args)
{
try
{
_queue = new MessageQueue(_qPath);//this part works as i had logging before and afer this call
//Add MSMQ Event
_queue.ReceiveCompleted += new ReceiveCompletedEventHandler(queue_ReceiveCompleted);//this part works as i had logging before and afer this call
_queue.BeginReceive();//This is where it is failing - get a null reference exception
}
catch(Exception ex)
{
EventLogger.LogEvent(EventSource, EventLogType, "OnStart" + _lineFeed +
ex.InnerException.ToString() + _lineFeed + ex.Message.ToString());
}
}
where
private MessageQueue _queue = null;
This works on my machine but when deployed to a windows 2003 server and running as Network service account, it fails
Exception recvd:
Service cannot be started. System.NullReferenceException: Object reference not set to an instance of an object.
at MYService.Service.OnStart(String[] args)
at System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object state)
Solved:
Turned out that the Q that i set up, I had to explicitly add Network Service account to it under security tab
You're seeing that particular exception because you're calling ex.InnerException.ToString(). The InnerException property is not always populated (in fact, it frequently isn't, nor should it be).
Your root problem is likely that the Network Service account doesn't have permissions to access the queue (in this case, read from it).
Here's some code that will help you get the actual error in your event log:
catch(Exception ex)
{
Exception e = ex;
StringBuilder message = new StringBuilder();
while(e != null)
{
if(message.Length > 0) message.AppendLine("\nInnerException:");
message.AppendLine(e.ToString());
e = e.InnerException;
}
EventLogger.LogEvent(EventSource, EventLogType, "OnStart" + _lineFeed +
message.ToString());
}