We wrote some code (utility stuff) that is intended to run as part of
either a windows service, a console app or a windows app. What I'd
like to do is when it is running as a service have it log exceptions
to the event log, when as a console app, write them to the Console,
and when running as a windows app. raise an event.
Anybody have any ideas other than setting some flag values in the
launching application. We can't do that because we don't control the
app that uses these utilities.
Nicholas Paldino [.NET/C# MVP] - 17 Oct 2007 15:45 GMT
I would have your library take a trace provider (or something that you
have provided in an abstract form, like an interface or abstract class) and
then just write to that trace provider. This way, it is the responsibility
of the app calling you to set up where the logging goes to, not you.

Signature
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com
> We wrote some code (utility stuff) that is intended to run as part of
> either a windows service, a console app or a windows app. What I'd
[quoted text clipped - 5 lines]
> launching application. We can't do that because we don't control the
> app that uses these utilities.
Vadym Stetsiak - 17 Oct 2007 15:47 GMT
Hello, tom.sikes@eds.com!
First of all there must be one (for all hosts) logging interface. Then 3
separate implementations of that logging interface.
Say
interface ILog
{
void Write(string msg);
}
ConsoleLogger : ILog
{
public void Write(string msg)
{ Console.WriteLine(msg); }
}
and other implementations for service and windows app.
Then there must be a factory class that will detect if app runs as a service
or console, or WinForms app and that's the hardest part here :)
Here is what I can suggest (it is not 100% logic):
- to detect if under WinForms - check via Reflection if there is access to
Application class
- to detect if under WindowsService - check via Reflection if there is
ServiceBase class there
- if neither is correct then code is working as a Console App
Also you have to consider the possibility of running under ASP.NET
application (the same Application class but from different namespace ).
HTH
--
With best regards, Vadym Stetsiak.
Blog: http://vadmyst.blogspot.com
You wrote on Wed, 17 Oct 2007 07:06:32 -0700:
ts> We wrote some code (utility stuff) that is intended to run as part
ts> of either a windows service, a console app or a windows app. What
ts> I'd like to do is when it is running as a service have it log
ts> exceptions to the event log, when as a console app, write them to
ts> the Console, and when running as a windows app. raise an event.
ts> Anybody have any ideas other than setting some flag values in the
ts> launching application. We can't do that because we don't control
ts> the app that uses these utilities.