locked
Windows Service Programming RRS feed

  • Question

  • Hi,

    I am new to Windows Services and am having a problem with my first attempt. All I want my service to do initially is write to a log file (.txt) when it is started. I added a call to my logging function in the OnStart() function, but my log file doesn't get written to.

    This is the only modification I have made to the standard VS2008 service template.

    Is there anything else I need to do in order for my service to run correctly?

    Can anybody point me in the direction of a great tutorial on Windows Services?

    Thanks.
    Wednesday, May 6, 2009 9:42 AM

Answers

All replies

  • Hi,

    This doesn't read like it's a window service specific problem. Reads like the file hasn't been closed properly and the data hasn't been written out. Could you post some code.

    Windows Services programming is just the same as other programming, it changes slightly if you want to communicate with the service from another application. If your familar with SQL Server, which is a service, then you'll know what I mean. You can communicate with services using named pipes, http, tcp, and MSMQ, to name a few. 

    In fact if your wanting to understand services as a whole then tip your toe into WCF. With that you get both Windows and Web services.

    If your not wanting to communicate with the service then the programming approach doesn't change that much from a console application.
    www.dsmyth.net | www.dsmyth.net/wiki
    Wednesday, May 6, 2009 9:52 AM
    • Marked as answer by Jimmy Collins Wednesday, May 6, 2009 11:03 AM
    Wednesday, May 6, 2009 9:55 AM
  • Hi Derek,

    Here is the code from my main class:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Diagnostics;
    using System.Linq;
    using System.ServiceProcess;
    using System.Text;
    using DataAccessLayer;
    using VirtualAccessLayer;
    using System.Threading;
    
    namespace TestManager
    {
        public partial class TestManager : ServiceBase
        {
            /// <summary>
            /// Constructor
            /// </summary>
            public TestManager()
            {
                InitializeComponent();
            }
    
    
            /// <summary>
            /// Indicate what actions should be taken when service starts running
            /// </summary>
            /// <param name="args">Arguments passed to this function</param>
            protected override void OnStart(string[] args)
            {
                Log("Test Manager Service just started");
    
            }
    
    
            /// <summary>
            /// Indicate what actions should be taken when service stops
            /// </summary>
            protected override void OnStop()
            {
                Log("Test Manager Service just stopped");
            }
    
    
            /// <summary>
            /// Gets the path to the temp folder, used in Log()
            /// </summary>
            /// <returns>The path to the temp folder</returns>
            public string GetTempPath()
            {
                string path = System.Environment.GetEnvironmentVariable("TEMP");
                if (!path.EndsWith("\\")) path += "\\";
                return path;
            }
    
    
    
            /// <summary>
            /// Logs a message to a specific file in the TEMP directory
            /// </summary>
            /// <param name="msg">The message to log</param>
            public void Log(string msg)
            {
                System.IO.StreamWriter sw = System.IO.File.AppendText(
                    GetTempPath() + "TestManager_Log.txt");
                try
                {
                    string logLine = System.String.Format(
                        "{0:G}: {1}.", System.DateTime.Now, msg);
                    sw.WriteLine(logLine);
                }
                finally
                {
                    sw.Close();
                }
            }
        }
    }
    Wednesday, May 6, 2009 9:56 AM
  • Hi,

    Doesn't look like there is a lot that can go wrong there. In fact there isn't it works fine :)

    Ok what I think is going on here is perhaps, and I don't want to assume anything, that your running the application from visual studio and nothing is happening. The application needs to be installed as a service. Once it is and it's started the log file is written fine.

    Create an installer for the service and use the InstallUtil.exe to install it. Also be careful of what user account is used.

    If you have any more trouble post again.
    www.dsmyth.net | www.dsmyth.net/wiki
    Wednesday, May 6, 2009 12:22 PM