I have a windows service created using the “new … c# service” menu in VS 2010. In the constructor I have:
TextWriterTraceListener tr1 = new TextWriterTraceListener( System.IO.File.CreateText("d:\\output\\test.log"));
Trace.Listeners.Add(tr1);
Trace.WriteLine("Created");
Setting the service to run in either LocalService or LocalSystem accounts (WinXP) I get the file created when the service starts, but it’s always empty. Adding a Flush() call to the destructor doesn’t help. I have more trace calls in the OnStart/OnStop/OnPause/OnContinue methods.
(edit) I have trace and debug enabled in the project – the trace output file is being created, it’s just empty.
The service will pull records out of a database and write files to disk for a messaging system we have to interface with. So the log file problem is the just first instance of a broader problem – I need to be able to write to a user-selected messaging directory.
This looks like the symptoms in another question but also I get the problem as LocalSystem (which I thought had too many permissions so was deprecated).
- What am I doing wrong with the trace calls? (why is the file created but nothing written to it?)
- If I want my service to run in the LocalService account is there some way to get the installer to give it permission to write to a folder/subtree?
- Or should I be running the service as LocalSystem?
- is this re-implementation of [Debug.Launch/EnsureWriter][2] a sane solution? I’m more inclined to say “why is the writer null” than paste code from elsewhere like that.
thanks for any help
Are you sure you have the
TRACEsymbol defined for your project? All theSystem.Diagnostics.Tracecode is conditionally compiled in based on this symbol. Note that you will still need to flush the trace output to ensure all the lines are written (or else setTrace.AutoFlushto true).As a side note, I would recommend against the use of the static
Tracemethods and suggest at least usingSystem.Diagnostics.TraceSource.