Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging from a console application [closed]

I have created a console application which transfers data from an excel sheet and transfers it to a database. I require to do logging into it.

How can i go about it? because i have absolutely no idea about how to go about it.

like image 986
user1746174 Avatar asked Oct 15 '12 06:10

user1746174


People also ask

Why does my console application closes immediately?

When console applications have completed executing and return from their main method, the associated console window automatically closes. This is expected behavior.

How do I stop the console app from closing?

The first solution is to run the application without debugging by using Ctrl+F5 instead of just F5. The console window will remain open when the program has finished.

How do I debug a console application?

Press F5 to run the program in Debug mode. Another way to start debugging is by choosing Debug > Start Debugging from the menu. Enter a string in the console window when the program prompts for a name, and then press Enter . Program execution stops when it reaches the breakpoint and before the Console.


4 Answers

If you simply want to write text to a file as your log you can do something like the following:

string strLogText = "Some details you want to log.";

// Create a writer and open the file:
StreamWriter log;

if (!File.Exists("logfile.txt"))
{
  log = new StreamWriter("logfile.txt");
}
else
{
  log = File.AppendText("logfile.txt");
} 

// Write to the file:
log.WriteLine(DateTime.Now);
log.WriteLine(strLogText);
log.WriteLine(); 

// Close the stream:
log.Close();

Source of this code is from this article.
Here is some documentation of the StreamWriter Class.

Otherwise if you want something more you can do as others have suggested and check out Log4Net.

like image 158
Chimera Avatar answered Oct 03 '22 15:10

Chimera


I require to do logging into it....

Do you mean you wish to add logging to the application or that you want people to log in?

To log thing to a file Log4Net exists to make it easy for you. If you don't want to rely on third party stuff then you could just do something like

Trace.Listeners.Clear();

if (options.Verbose)
{
    var ctl = new ConsoleTraceListener(false) { TraceOutputOptions = TraceOptions.DateTime };
    Trace.Listeners.Add(ctl);
}

if (options.Log)
{
    var logFileFs = new FileStream("my.log", FileMode.Append);
    var twtl = new TextWriterTraceListener(logFileFs)
    { TraceOutputOptions = TraceOptions.ThreadId | TraceOptions.DateTime };
    Trace.Listeners.Add(twtl);
}

Trace.AutoFlush = true;

Trace.WriteLine(DateTime.Now.ToString(CultureInfo.InvariantCulture) + "   My App Started");

This enables you to add different outputs such as a log file and verbose printing to your ouput so that you could log to a file and also print it in console if the user has choose to use verbose output.

The line

Trace.WriteLine(DateTime.Now.ToString(CultureInfo.InvariantCulture) + "   My App Started");

should be made into a function that takes your message and perhaps a log level so you have a uniform look to all your written log messages.

If you don't need that much functionality you could just write to the file stream directly.

like image 40
inquam Avatar answered Oct 03 '22 15:10

inquam


there are many ways to log. You can use your own logging

else try Log4net it is simple configuration changes you are done.

Good article http://www.codeproject.com/Articles/140911/log4net-Tutorial

Just an additional info for you reg ELMAH(Another way of error logging) Using ELMAH in a console application

like image 32
Peru Avatar answered Oct 03 '22 16:10

Peru


If you can use a third part product, go for log4net. It is a great logging framework for .NET.

like image 29
Saurabh R S Avatar answered Oct 03 '22 14:10

Saurabh R S