C# Application logger - c#

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

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.

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

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.

Related

How can I tell when tomcat is finished starting? (controlling the service with C#)

I'm writing a C# application to control the windows tomcat service. I need a way to get some sort of event when tomcat finishes starting after I run the service. (i.e. all the apps deploy.)
I can read the log file and wait for the "Server startup in" message but it seems like there must be a less messy way. I know you can register a lifecycle listener using Java but I can't think of a way to do something similar with C#.
Does anyone know a good way to monitor tomcat with C#? Thanks in advance.
Edit: In case I wasn't clear, simply checking the status of the isn't sufficient since it will report "Running" during the startup time while the apps are being deployed and it isn't yet accessible.
Unless there's a reason you can't do so (permissions, etc) I would just issue a web request to the service and wait until it comes back.
Even if you don't want to give the account hosting the service permission to use the app(s) in question, you can still create an app that only it has permission to use, and have it call that.
I ended up writing a tomcat wrapper in Java which acts as a lifecycle listener. The C# application launches the java program and receives lifecycle messages through a named pipe.

C# Remoting IPC, how to test if remote application is running?

Good morning everyone!
I'm doing a distributed application using .NET 4.0, C# and Remoting (IPC at the moment but that may change, Object is WellKnownObjectMode.Singleton).
Upon startup of the client I'd like to test whether the server is up and running.
What would be the best practice to implement this? Call any one method? Implement a special method just for that startup test?
At the moment I start the client and it then calls methods on the server periodically (every 100 ms). That causes the client to show an estimated 1,000,000,000 error dialogs pointing out that the server is unreachable.
You could probably use a global mutex.

How to test the reactivity of a Windows Service?

I'm programming a monitoring application that needs to display the state of several windows services. In the current version, I can know whether a service is Running, Stopped, Suspended or in one of the pending states. That's good, but I'm wondering if there is a way to test if a service is actually responding? I guess it can be in a running state but not responding at all!
I am using the ServiceController class from System.ServiceProcess. Do you think that if a service is not responding, the ServiceController.Status would return an exception?
How would you approach the problem?
Thanks
EDIT
Seems that: ServiceController.Status can return 2 types of exceptions:
System.ComponentModel.Win32Exception: An error occurred when accessing a system API.
System.InvalidOperationException: The service does not exist as an installed service.
Nothing about reactivity.
This might be obvious, but have you tried talking to the service?
There's no common way to talk to a service, so there is no way Windows can interrogate whether the service is still responding as normal. It is perfectly normal for a service to go into a complete sleep waiting for external I/O to happen, and thus Windows would not get a response while the service is actually alive and functioning exactly as designed.
The only way is to actually send a request to it, and wait for the response, and for that you need some inter-process communication channel, like:
Network
Named pipes
Messages
Basically, if you need to determine if a service is able to respond, you need to check if it is responding.
The service controller types and APIs can only provide information on the basis of the service's response to those APIs.
E.g. you can create a service which responds to those APIs correctly, but provides no functionality on even numbered hours.
In the end you need to define "responsive" in terms of the services functionality (e.g. a batch processor is processing batches) and provide a mechanism (A2A API, WMI, Performance Counters) to surface this.

Load balancing a console application or service

So it's easy to load balance an ASP.NET web application. You set up a load balancer between two servers, and if the web server isn't responding on Port 80, it won't receive requests.
Are there any proven techniques for doing this for a C# console application or Windows service that takes actions of its own volition? Are there any frameworks for knowing if peer processes are alive or dead, doing heartbeats, etc?
I've been experimenting a bit with NServiceBus and it seems like, for certain kinds of applications, it would help to have most of the work done as a response to an event, which makes it more like a web application, actually, and therefore easier to scale and load balance with multiple processes, but I feel like that's a half-baked solution since in most cases there usually needs to be some concept of a "master" process that's responsible for getting work started.
NServiceBus does indeed handle this for you with its Distributor process (described here: http://docs.particular.net/nservicebus/scalability-and-ha/distributor/). The generic host that comes with NServiceBus allows you to have the exact same code and configuration run both as a console app and as a windows service (described here: http://docs.particular.net/nservicebus/hosting/nservicebus-host/).
You can have this for events as well as for regular command messages.
If you want a "master" process to decide what to do when all the load-balanced work completes, that is provided to you in the form of the saga infrastructure (described here: http://docs.particular.net/nservicebus/sagas/ and demonstrated in the Manufacturing sample that comes with NServiceBus).
In short, you should pretty much be covered.

C# Error Report E-Mail - Web Service

Hey guys, I am trying to add the capability of my Windows Form application being able to send me an e-mail message when my application generates an error message. How do I setup a web service for my application? Any feedback would be greatly appreciated.
You don't need a web service for something like this.
You can use the MailMessage class from the System.Net namespace - the trick is to use the correct SMTP server settings, possibly with the SmtpClient class. This is something you will have to get on your own - from your network admin or ISP.
Take a look at the Enterprise Library Exception Handling Application Block.
The Exception Handling Application Block is designed to address the most common tasks developers face when they write applications that use exception handling. These tasks are arranged according to scenarios. Each scenario gives an example of a real-world situation, discusses the exception handling functions the situation requires, and shows the code that accomplishes the task.
The goal of arranging these tasks according to scenarios is to give the code some context. Instead of displaying an isolated group of methods, with no sense of where they can best be used, scenarios provide a setting for the code and describe situations that are familiar to many developers whose applications must handle exceptions.
The scenarios are the following:
Logging an Exception
Wrapping an Exception
Replacing an Exception
Propagating an Exception
Displaying User-Friendly Messages
Notifying the User
Assisting Support Staff
Shielding Exceptions at WCF Service Boundaries
You don't need a Web Service to send emails.
If you really want to use a web service, I would think you would actually create the web service on a server that will listen for message from the client. You could setup the windows form application with a WCF client and setup a WCF server to receive messages, but I think this is overkill.
I personally would use the log4net framework and configure an SmtpAppender to send emails when you log an error. It's really elegant when you get the hang of it.

Categories