2

Hi
is there a way to write a logger can log the exception occurred in another program written also in c# ?

Dabbas
  • 3,112
  • 7
  • 42
  • 75

3 Answers3

2

Yes, but it's not easy.

If you are running the application in an AppDomain from your current application, it's relatively easy. What you do is of that domain, you attach to the UnhandledException event handler and register the exceptions that way.

However, I suspect that the C# application you are referring to is running in a different process. In that case, you are still using roughly the same mechanism, but it's a lot more difficult to actually get the AppDomain. This involves managed C++ and executing a thread in a remote domain. You can look at Can I inject a thread in a remote app domain from C# and http://social.msdn.microsoft.com/Forums/en/winforms/thread/e4cfa5dd-e254-4088-8754-09dc40d4fb5b for more information about this.

Community
  • 1
  • 1
Pieter van Ginkel
  • 29,160
  • 8
  • 71
  • 111
1

Exceptions are a mechanism that exists strictly in-process, so no.

Jon
  • 428,835
  • 81
  • 738
  • 806
  • Well, Exceptions can be made serializable, and hence could be logged by a different process – flq Nov 28 '10 at 11:45
  • Exceptions can only be handled in-process, but there is no reason that they can't be logged in a different application. – cspolton Nov 28 '10 at 11:55
  • All approaches to this end require that the process in which the exceptions are raised cooperates with the process that logs the exceptions. In this case the question should definitely clarify that modification to the code of both processes is an option. – Jon Nov 28 '10 at 12:02
0

Upon an exception you could send the Exception details to another application which is responsible for logging the Exception. You could use one of a variety of Remote procedure call mechanisms such as might use web services or Windows Communication Foundation (WCF).

Here is a good example of Error Handling Extensions in WCF.

cspolton
  • 4,495
  • 4
  • 26
  • 34