Solved

How can I create a log files in C Sharp?

Posted on 2011-03-24
4
390 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

707 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