[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Create Log File

Posted on 2008-10-01
5
Medium Priority
?
1,457 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
[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
  • 3
5 Comments
 
LVL 5

Accepted Solution

by:
libin_v earned 1200 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 800 total points
ID: 22616309
ArrayList is in System.Collections Namespace
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

656 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