PC Review


Reply
Thread Tools Rate Thread

Writing to a File in a Windows Service...

 
 
Solomon_Man
Guest
Posts: n/a
 
      19th Dec 2007
All,
I have a Windows Service application that has database connectivity
and needs the capability to let a user know that there has been a db
failure. What is the proper way to notify a user that there has been
error within a service?

I was thinking of writing to a file and using the below code;

public void LogMessageToFile(string msg)
{
//get current directory and then prepare to create a file
name Logger.txt
string Dir = Directory.GetCurrentDirectory();
System.IO.StreamWriter sw = System.IO.File.AppendText(
Directory.GetCurrentDirectory() + "Logger.txt");


try
{
//include the time of error
string logLine = System.String.Format(
"{0:G}: {1}.", System.DateTime.Now, msg);
sw.WriteLine(logLine); //write the string to file
sw.Flush();
}
finally //clean up after myself
{
sw.Close();
}
}

Unfortunately this code does not seem to write to a file when in a
windows service. It seems to work fine in a Windows form. Any ideas or
approaches?

Thanks,
Chris
 
Reply With Quote
 
 
 
 
Soren S. Jorgensen
Guest
Posts: n/a
 
      19th Dec 2007
Did you check in Windows\System32 for the file ??
Default directory for services is the system dir!!

To get the dir your service exe is located in you need to do something like:
Assembly a = Assembly.GetEntryAssembly()
string exeDir = Path.GetDirectoryName(a.Location);

SSJ

"Solomon_Man" <(E-Mail Removed)> skrev i en meddelelse
news:1cb64e1e-3815-4805-b82b-(E-Mail Removed)...
> All,
> I have a Windows Service application that has database connectivity
> and needs the capability to let a user know that there has been a db
> failure. What is the proper way to notify a user that there has been
> error within a service?
>
> I was thinking of writing to a file and using the below code;
>
> public void LogMessageToFile(string msg)
> {
> //get current directory and then prepare to create a file
> name Logger.txt
> string Dir = Directory.GetCurrentDirectory();
> System.IO.StreamWriter sw = System.IO.File.AppendText(
> Directory.GetCurrentDirectory() + "Logger.txt");
>
>
> try
> {
> //include the time of error
> string logLine = System.String.Format(
> "{0:G}: {1}.", System.DateTime.Now, msg);
> sw.WriteLine(logLine); //write the string to file
> sw.Flush();
> }
> finally //clean up after myself
> {
> sw.Close();
> }
> }
>
> Unfortunately this code does not seem to write to a file when in a
> windows service. It seems to work fine in a Windows form. Any ideas or
> approaches?
>
> Thanks,
> Chris



 
Reply With Quote
 
 
 
 
Scott Roberts
Guest
Posts: n/a
 
      19th Dec 2007

"Solomon_Man" <(E-Mail Removed)> wrote in message
news:1cb64e1e-3815-4805-b82b-(E-Mail Removed)...
> All,
> I have a Windows Service application that has database connectivity
> and needs the capability to let a user know that there has been a db
> failure. What is the proper way to notify a user that there has been
> error within a service?


I believe the "accepted" method is to write to the Event Log.

> I was thinking of writing to a file and using the below code;
>
> public void LogMessageToFile(string msg)
> {
> //get current directory and then prepare to create a file
> name Logger.txt
> string Dir = Directory.GetCurrentDirectory();
> System.IO.StreamWriter sw = System.IO.File.AppendText(
> Directory.GetCurrentDirectory() + "Logger.txt");
>
>
> try
> {
> //include the time of error
> string logLine = System.String.Format(
> "{0:G}: {1}.", System.DateTime.Now, msg);
> sw.WriteLine(logLine); //write the string to file
> sw.Flush();
> }
> finally //clean up after myself
> {
> sw.Close();
> }
> }
>
> Unfortunately this code does not seem to write to a file when in a
> windows service. It seems to work fine in a Windows form. Any ideas or
> approaches?


Check your file/folder permissions for the account that the service is
running under (probably "Local System"). Also, when running as a service the
"Current Directory" is probably a system directory ("Windows" or
"Windows\System32") which probably isn't the best place for a log file.

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Writing a structure to a file (writing it as binary data) Mufasa Microsoft C# .NET 2 12th Jun 2007 04:28 PM
Any problems with writing the information into a file - Multi-users perform writing the same file at the same time ???? HNguyen Microsoft ASP .NET 4 21st Dec 2004 02:53 PM
Re: Writing text to a text file(writing in the middle of the textfile content) Nicholas Paldino [.NET/C# MVP] Microsoft C# .NET 0 11th Mar 2004 04:23 PM
Re: Writing text to a text file(writing in the middle of the textfile content) Jon Skeet [C# MVP] Microsoft C# .NET 0 11th Mar 2004 04:20 PM
Typing online sometimes does not work, will bring up "open folder", make writing uppercase, stop writing for no reason... =?Utf-8?B?U29waGll?= Windows XP Help 1 27th Jan 2004 03:22 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:45 AM.