How catch & log exceptions on .net?

When a exception in the .net server happend, I only get on the client the main description, but not the more useful traceback.

For example I’m getting this when try to apply updates on a dataset:

Project CatalogSync.exe raised exception class EROUnregisteredServerException with message ‘An exception was raised on the server: Input string was not in a correct format.’.

Could be more useful if can get more info about this.

I try using (the server is console only):

    [STAThread]
    static void Main(string[] args)
    {
		AppDomain.CurrentDomain.UnhandledException += 
	        new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
		
        BestSellerCLI cli = new BestSellerCLI();
        cli.Run();
    }


    static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
        Exception ex = (Exception)e.ExceptionObject;

		Console.Write(ex.Message + ex.StackTrace);
    }

But nothing happend. How can I do this?

First of all, the code you’ve sent is for handling of exceptions that was unhandled by your application logic. They are not supposed to be sent the client. Clients receive only exception that were occurred during processing of current user request to server Service. Also such exceptions are not re-raised on server, so you can’t catch them with AppDomain.CurrentDomain.UnhandledException handler.

Secondly, usually server message component send exceptions that are either inherited from or wrapped with the ServerException class that has a ServerStackTrace property. So, on DA client for .Net you can use next code to catch such exception:

private void btLoad_Click(object sender, EventArgs e)
{
    try
    {
        this.fDataModule.DataAdapter.Fill(data, new string[] { "Clients" });
    }
    catch (ServerException se)
    {
        ShowException(se);
    }

}

private void ShowException(ServerException ex)
{
    try
    {
        MessageBox.Show(string.Format("Message: {0}\nStack: {1}", ex.Message, ex.ServerStackTrace), ex.GetType().Name);
    }
    catch (ServerException se)
    {
        ShowException(se);
    }
}

private void btUpdate_Click(object sender, EventArgs e)
{
    this.fDataModule.DataAdapter.Update(data, new string[] { "Clients" });
}

If this won’t help, please tell us what message component and what version of DA you are using.

My clients are both in delphi & python.

I prefer to log the errors in the server, not show in the client. The server will be a public cloud service so is important to log the exceptions to take measures…

I use BIN message (for delphi/ios) & json for python

To log unhandled exceptions on server (both from main or worker threads) you can use our class AplicationServer The good example of using it will be a server application project, generated by DataAbstract Wizard. In it select option Client and a New Custom DA Server on the first page. In generated project, look at Program.cs source file.

But DA server components is build in manner to prevent their fall due to some client request. Thus almost all exception on server side are caught within correspondent component.
In that case, to see what’s happening on the server side, I suggest you look at the approach that is used in our DA Sample Server to log server side activity. DataService provides events you can handle to log errors.

Another option is to catch all exceptions, that server is going to send to client. Handle OnInitializeMessage event to do this:

private void Message_OnInitializeMessage(object sender, RemObjects.SDK.InitMessageEventArgs e)
{
    if (e.Exception != null)
    {
        // log exception
    }
}

Event is available for all messages.

I have another exception that not get logged:


Debugger Exception Notification

Project Project1.exe raised exception class EROUnregisteredServerException with message ‘An exception was raised on the server: No such host is known’.

However, I don’t get on _OnInitializeMessage the exception. So I change my code to use “AplicationServer” base class, neither it get logged when pass the -D param to the server.

Where else I need to hook to see the error?

As I understand the exception above was raised on client side and is related to communication problem between server and client. According to text “No such host is known” the reason was in resolving server ip address by its dns name. Anyway, it was a client error and that’s why it was not caught by your server exception handlers.

But such errors can be handled on client side (or at least logged) by subscribing on ClientChannel.OnException event. More about this exception here.