Sangeetha
Sangeetha

Reputation: 591

Writing log files

I am developing c# application, which is running as a windows service. What ever transactions we are doing in the application i am writing it into log file. A log directory is added in app.config file as below.

<add key ="LogDir" value="log" />
    <add key ="LogLevel" value="2" />

And in the c# code the above one is accessing as below.

int logLevel = Convert.ToInt32(ConfigurationManager.AppSettings["logLevel"]);
                if (logLevel > 0)
                {
                    logger = new Logger();
                    logger.TraceLevel = logLevel - 1;
                    logger.logDir = ConfigurationManager.AppSettings["logDir"];
                    logger.logFileBaseName = "touchserver";
                }

And then when any process is happening i am writing the data to the log as below.

TouchServer.Log(Logger.MessageType.Trace, 1, "Item successfully deleted");

And when i run my application in debug mode (i mean as console application) the log file will be created in the application's debug folder and the data will write into the log file.

But my problem is that when i install my application as service the log file is not getting created in the debug folder, and i am unable to see the actions performed , in case if anything went wrong.

Please help me to find a solution in this.

And i am installing service using Installutil command.

Thanks in advance sangita

Upvotes: 1

Views: 1118

Answers (2)

Richard
Richard

Reputation: 109140

But my problem is that when i install my application as service the log file is not getting created in the debug folder, and i am unable to see the actions performed , in case if anything went wrong.

Check out what are the result of the IO operations by using Process Monitor. I suspect you'll find the identity being used to run the service process does not have write permissions where it is trying to write the log file.

But the better option is to use an existing logging library as Hemal suggests.

Upvotes: 2

Miserable Variable
Miserable Variable

Reputation: 28761

While you could get into why this is not working and fix the solution, overall there is no need to implement a logging component.

There are excellent free libraries available that do this very well. log4net is very popular. It is easy to use, feature rich and efficient. Take a look at it.

Upvotes: 3

Related Questions