Solved

How can I create a log files in C Sharp?

Posted on 2011-03-24
4
385 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Data Saving(2) 2 41
ADO.NET ENTITY DATA MODEL 3 30
Not showing page correctly 3 29
Importing CSV file encoded in UTF-8 in SQL SERVER 2008 using BULK INSERT 5 20
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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

948 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

21 Experts available now in Live!

Get 1:1 Help Now