Solved

How can I create a log files in C Sharp?

Posted on 2011-03-24
4
384 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you both. Have a good day.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

771 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now