开发者

c# Windows Service Console.Writeln

开发者 https://www.devze.com 2023-01-30 06:57 出处:网络
I wrote, installed, and successfully started a Windows Service in c# that does nothing :)Initially I just want echo stuff to the console, db queries the service makes etc.I used the OnStart in my serv

I wrote, installed, and successfully started a Windows Service in c# that does nothing :) Initially I just want echo stuff to the console, db queries the service makes etc. I used the OnStart in my service, but from a cmd prompt when I do a net start "My Service" where do these messages show up?

I'm open to better ways. I'm new to this and feeling my way through it step by step by echoing back to the console my progress. Should I echo to the event log instead? How do I do that?(I know, I know, google it)

开发者_JAVA百科 protected override void OnStart(string[] args)
    {
        base.OnStart(args);
        Console.WriteLine("Sham-Wow!");
    }


You cannot write the console from a Windows Service.

I recommend you using any logging utility, such as log4net.

The second alternative is writing to the Event Log (System.Diagnostics.EventLog)


Try System.Diagnostics.Trace.WriteLine and you will see your messages in Output window of Visual Studio or with dbgview utility.


If you want to run your application as a console application (so you can see your console output) or as a service you can achieve this with the following:

  • Ensure that your application is compiled as a Console application.
  • Alter the application's Main method so that you can branch for service or console execution.
  • Run your application as follows to get a console "myservice.exe /console".

It's been years since I have done this so might need a little tweaking, but something like follows:

static void Main(string[]] args) 
{ 
    if (args.Length == 0)
    {
          //Service entry
          System.ServiceProcess.ServiceBase[] services; 
          services = new System.ServiceProcess.ServiceBase[] { new WinService1() }; 
          System.ServiceProcess.ServiceBase.Run(services);
    }
    else
    {
          //Console entry
          OnStart(args);
    } 
}

protected override void OnStart(string[] args)
{
    base.OnStart(args);
    Console.WriteLine("Sham-Wow!");
}

This is fine for some early experimentation, but I would recommend Log4Net once you have got your head around things.


The most common/accepted way of communicating the status of your service is to write to the Windows Event Log.

For easier debugging, I would recommend that you put all of your business code into a separate class from the service component. You can then use that class from either your service, or from a console application. While you are creating the service, you will use the console application to host your component, so that you can easily step-into the code.


Very old question, but very relevant. This is how I implemented a logging mechanism that I could inspect while the Windows service is running.

  • Create a log file. My favorite is Log4net as mentioned before.
  • Open PowerShell and run this command Get-Content "path/to/file.log" -Wait

You will be able to monitor the file as it changes. This is like the tail command in linux.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号