Solved

How can I create a log files in C Sharp?

Posted on 2011-03-24
4
386 Views
Last Modified: 2012-05-11
I need a simple example that send the log into a text file.
i.e. Let say that I have a form with a text box and two buttons.
     one button sum and the other multiply.
When I press the button sum button, I want to send the sum of A + B to the text box and if the sum goes fine, write time, date, and small text massage to a file saying that all went fine.

Does not have to be this example, I am just trying to explain what I need.
Thank you.
0
Comment
Question by:iscivanomar
  • 2
4 Comments
 
LVL 7

Expert Comment

by:mkobrin
ID: 35205171
I have a method called logger that does just this. I'll post the code in a couple of minutes.
0
 
LVL 7

Accepted Solution

by:
mkobrin earned 125 total points
ID: 35205188
Here it is:
public static void Logger(string fileName, string data)
        {
            try
            {
                fileName = fileName + DateTime.Today.ToString("yyyy-MM-dd") + ".LOG";
                if (File.Exists(fileName))
                {
                    using (StreamWriter SW = File.AppendText(fileName))
                    {
                        SW.WriteLine(DateTime.Now.ToString("dd MMM HH:mm:ss") + ": " + data);
                    }
                }
                else
                {
                    using (StreamWriter SW = File.CreateText(fileName))
                    {
                        SW.WriteLine(DateTime.Now.ToString("dd MMM HH:mm:ss") + ": " + data);
                    }
                }
            }
            catch
            {
                Logger(fileName, "Error logging " + data);
            }
        }

Open in new window

0
 
LVL 19

Assisted Solution

by:Rikin Shah
Rikin Shah earned 125 total points
ID: 35205280
using System;
using System.IO;
using System.Windows.Forms;

namespace Logger
{
    class EventLogger
    {
        public void LogErrors(string data)
        {
            Application.DoEvents();
            try
            {
                File.AppendAllText("C:\\Log.txt", string.Format("{0} : {1}{2}", DateTime.Now, data, Environment.NewLine));
            }
            catch (Exception exception)
            {
                throw new Exception("Error writing log event", exception);
            }
            Application.DoEvents();
        }
    }
}

Open in new window


use this class and call a method whenever you want to log anything.
0
 

Author Closing Comment

by:iscivanomar
ID: 35205395
Thank you both. Have a good day.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question