Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / October 2007

Tip: Looking for answers? Try searching our database.

Running as a service or as an application.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
tom.sikes@eds.com - 17 Oct 2007 15:06 GMT
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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.