Solved

Create Log File

Posted on 2008-10-01
5
1,445 Views
Last Modified: 2012-05-05
In  C3 Windows Form application I would like to create the method, I would call Write2Log where I will be writing my exceptions in a manner Write2Log(ex.Message);
References I am using in my applications are:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;
using System.Deployment.Application;

Help me with  a code.  

           


           
0
Comment
Question by:Aquarus
  • 3
5 Comments
 
LVL 5

Accepted Solution

by:
libin_v earned 300 total points
ID: 22616076

Extract from http://www.doogal.co.uk/exception.php
 

    private void Write2Log(string error)

    {

      string filename = Path.GetDirectoryName(Application.ExecutablePath);

      filename += "\\BugReport.txt";
 

      ArrayList data = new ArrayList();
 

      lock (this)

      {

        if (File.Exists(filename))

        {

          using (StreamReader reader = new StreamReader(filename))

          {

            string line = null;

            do

            {

              line = reader.ReadLine();

              data.Add(line);

            }

            while (line != null);

          }

        }
 

        // truncate the file if it's too long

        int writeStart = 0;

        if (data.Count > 500)

          writeStart = data.Count - 500;
 

        using (StreamWriter stream = new StreamWriter(filename, false))

        {

          for (int i = writeStart; i < data.Count; i++)

          {

            stream.WriteLine(data[i]);

          }
 

          stream.Write(error);

        }

      }

    }

Open in new window

0
 
LVL 13

Expert Comment

by:Michael_D
ID: 22616081
Attached is simple method of logging.
using System.IO;

......
 

    public static void Write2Log(String logMessage)

    {

        using (StreamWriter w = File.AppendText("log.txt"))

        {

        w.Write("\r\nLog Entry : ");

        w.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(),

            DateTime.Now.ToLongDateString());

        w.WriteLine("  :");

        w.WriteLine("  :{0}", logMessage);

        w.WriteLine ("-------------------------------");

        // Update the underlying file.

        w.Flush(); 

        w.Close();

        }

    }

Open in new window

0
 
LVL 13

Expert Comment

by:Michael_D
ID: 22616100
oops, forgot to refresh browser...
0
 

Author Comment

by:Aquarus
ID: 22616281
I received the following error:

The type or namespace name 'ArrayList' could not be found (are you missing a using directive or an assembly reference?)      
What references am I missing?
0
 
LVL 13

Assisted Solution

by:Michael_D
Michael_D earned 200 total points
ID: 22616309
ArrayList is in System.Collections Namespace
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Handling arrays 14 35
Visual Studio: built-in keystroke automation 2 27
C# code editing and collaboration 3 50
Image(2) 3 30
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…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This video discusses moving either the default database or any database to a new volume.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

743 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

14 Experts available now in Live!

Get 1:1 Help Now