-4

I have the following code to write to a log file and it works fine in my console application:

try
{
    string log = "some message";
    string mFileName = "some directory to the log file";
    Console.WriteLine(log);
    using (StreamWriter w = File.AppendText(mFileName))
    {
         w.WriteLine(log);
         w.Dispose();
    }
}
catch (Exception e)
{
    Console.WriteLine("Error Log: " + e.Message);
}

But when I run the code as a service on my PC, it stops logging altogether. I've already checked and the service has same rights as me.

Please note: The service doesn't crash, it simply runs without logging.

honk
  • 7,217
  • 11
  • 62
  • 65
Michael Tot Korsgaard
  • 3,582
  • 9
  • 47
  • 84
  • 4
    First start by removing `w.Dispose()` this is already handled at the end of the `using`. See [CA2202: Do not dispose objects multiple times](https://msdn.microsoft.com/en-us/library/ms182334.aspx). – Enfyve Oct 26 '16 at 12:08
  • Does `mFileName` include full path? You may also consider: [How to write to an event log by using Visual C#](https://support.microsoft.com/en-us/kb/307024) – Johnny Mopp Oct 26 '16 at 12:13
  • `mFileName` is the full filepath including file name and type – Michael Tot Korsgaard Oct 26 '16 at 12:14
  • 1
    ***What exception are you seeing?*** The windows application event log should help you here. As an aside, why not pick a location that is definitely writable, such as `Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),"MyServiceName")`? As a second aside, why are you rolling your own logging? It's a considerably more subtle problem than is immediately apparent, and is already solved. There are more important things to debug than your own logging framework. – spender Oct 26 '16 at 12:15
  • is the location a network location? Or some local one? – Jakub Szumiato Oct 26 '16 at 12:17
  • If you want to debug your service, throw in a line at startup `System.Diagnostics.Debugger.Break()` and attach VS. – spender Oct 26 '16 at 12:22
  • @spender: There's no exception. When I run it as a service everything goes well, but it doesn't log anything. I already have a try catch around the code. – Michael Tot Korsgaard Oct 26 '16 at 12:22
  • @JakubSzumiato: It's the local network – Michael Tot Korsgaard Oct 26 '16 at 12:22
  • "I already have a try catch around the code"... if you swallow your exceptions, you'll never find out what's going wrong. Anyway, attach the debugger and win. – spender Oct 26 '16 at 12:24
  • I closed this because the lack of information is gathering speculative answers. The real issue is that you haven't gathered enough information about the cause of the crash because you aren't able to debug your service. Hence the dupe vote/closure. – spender Oct 26 '16 at 12:30
  • http://meta.stackoverflow.com/questions/336954/is-this-an-appropriate-use-of-the-dupehammer – spender Oct 26 '16 at 12:35
  • "some directory to the log file" You realize that your service isn't running under *your user account*. It's running under Network Service or some other system account by default. These system accounts must be granted rights to files, folders and other system resources **just like any other account**. You cannot simply use a resource in a service and expect it to succeed as it would when you run the same command (probably as an admin) in a console app. This is an important thing to understand about developing software. –  Oct 26 '16 at 14:46

2 Answers2

-1

Most likely this has something to do with user rights. My guess is that the system user that starts the service, doesn't have the rights to write to the file location.

One solution is to start the service with the user credentials of your own account (because you have the rights.)

Another solution is to give the user (local system ,service user or IIS user for web-service) write access to the directory, or change the path of the file to a directory were the user has write access.

if you want to make sure, add a try catch block around the using, and write the exception to console, debug or messagebox. Then you will know what goes wrong.

see possible exceptions for File.AppendText on MSDN

My guess is it throws a UnauthorizedAccessException

Gwen Royakkers
  • 431
  • 2
  • 11
  • 1
    "and write the exception"? With what? The logging method that doesn't work? It's a service, so can't interact with the user via normal means. – spender Oct 26 '16 at 12:20
  • The system event log? [link]http://stackoverflow.com/questions/25725151/write-to-windows-application-event-log-without-registering-an-event-source – Gwen Royakkers Oct 26 '16 at 12:22
-1

Why not simply create the StreamWriter directly, wrap it in a try-catch, and if there is an exception, log it with System.Diagnostics.EventLog?

try 
{
    using (Streamwriter sw = new StreamWriter(path, true) 
    {
        sw.WriteLine(log);
    }
}
catch (Exception ex)
{
    // Log exception using System.Diagnostics.EventLog
}
Enfyve
  • 894
  • 9
  • 20
  • How does this answer the question? – spender Oct 26 '16 at 12:23
  • Because the more explicit typing will be easier to understand, plus as OP stated, he has a try-catch already surrounding it, but without actually seeing the code he could be catching the error and not logging it to the EventLog, which wouldn't crash the service, and also wouldn't give any feedback. – Enfyve Oct 26 '16 at 12:25