473,387 Members | 3,750 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Writing to a File in a Windows Service...

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
Dec 18 '07 #1
2 14165
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" <cm******@gmail.comskrev i en meddelelse
news:1c**********************************@s12g2000 prg.googlegroups.com...
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

Dec 18 '07 #2

"Solomon_Man" <cm******@gmail.comwrote in message
news:1c**********************************@s12g2000 prg.googlegroups.com...
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.

Dec 18 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Glenn Venzke | last post by:
I'd like to write a windows service. Unfortunately, my company would only spring for the standard edition of VB.NET which doesn't support the creation of windows services. Is it possible to write one...
4
by: Lenard Gunda | last post by:
Hello! I have created a program, that receives files, and then stores them locally for later access. This program is run on a Windows 2000 Server computer as a system service. Some of the files...
3
by: Jiho Han | last post by:
Should ASPNET user belong to the local Users group? I may have made some changes that affected my workstation setup and I am experiencing some unexpected behaviors. For example, I have my IIS...
2
by: Richard Thornley | last post by:
Hello, I am working on a VB.Net application and I am getting an error ONLY on the main computer that uses the application. The main computer is on the other coast so of course it would be the...
3
by: Chris Dunaway | last post by:
I am writing a Windows Service that reads and processes files on a set schedule. I want to create a second app that can monitor the Windows service. The Windows service will write trace messages...
4
by: Elhanan | last post by:
hi.. we are planning to do a file distibution system in dotnet 2.0 (the system will recieve request for file transfers from one legacy system to another using connect direct api, the execution...
4
by: =?Utf-8?B?QmlsbCBNYW5yaW5n?= | last post by:
I have an internal ASP.NET 2.0 web service that does, among other things, expose a method that causes files to be written to a location that the user configures. The web service has been around...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.