Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How can I create a log files in C Sharp?

Posted on 2011-03-24
4
Medium Priority
?
394 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 500 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 500 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

618 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