5

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.

Usman
  • 3,155
  • 3
  • 25
  • 44
user1746174
  • 61
  • 1
  • 1
  • 5
  • This might helpful to you http://stackoverflow.com/questions/3475861/logger-application-for-c-sharp-console-application – Usman Oct 15 '12 at 06:29
  • Seems you are new here. You "accept" the answer that best answers your question by clicking the on the check mark next to each answer. This how people getting rewarded for helping others out. – Chimera Oct 15 '12 at 07:29

4 Answers4

14

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.

Chimera
  • 5,552
  • 6
  • 41
  • 77
  • 1
    You know, I actually think the line `File.AppendText` would work even when the file doesn't already exist - I don't think you have to check, it's unnecessary. I've tested it myself in VS2013, I deleted the log file and ran the code with just AppendText and it created the file with the logs as expected – TKoL Apr 30 '19 at 09:57
7

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.

ahsteele
  • 25,470
  • 26
  • 131
  • 238
inquam
  • 11,960
  • 14
  • 53
  • 95
2

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

Community
  • 1
  • 1
Peru
  • 2,501
  • 4
  • 32
  • 62
  • How to go about if i want to use my own logging? – user1746174 Oct 15 '12 at 06:20
  • See this http://stackoverflow.com/questions/1742078/log4net-configuration-for-console-app – Peru Oct 15 '12 at 06:22
  • I want to implement my own logger? So how do i go about? – user1746174 Oct 15 '12 at 06:23
  • If you mean you want to log something into the same database that you just transferred Excel data into, you can use the very same connection to run an INSERT query on a logging table (which you can create if you don't have one). – CptSupermrkt Oct 15 '12 at 06:24
  • Google you get lot of options ! http://stackoverflow.com/questions/147557/error-logging-in-c-sharp – Peru Oct 15 '12 at 06:24
1

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

Saurabh R S
  • 2,779
  • 1
  • 27
  • 40