How to log apps?

How to log apps?

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 2917 Views - Last Post: 25 November 2010 - 05:44 AM Rate Topic: -----

#1 CodderMan   User is offline

  • New D.I.C Head

Reputation: 10
  • View blog
  • Posts: 48
  • Joined: 18-November 10

How to log apps?

Posted 23 November 2010 - 06:28 AM

What is the best way to log applications?
Take the program from the Quick And Easy Custom Events tutorial.

No matter what I do I can't get something really functionable...


I'm using a Stream Writer to write...
StreamWriter myWriter = new StreamWriter("Log.txt");


The best way to log was to create an array, but the problem is that you enter the size...(And I don't know how many entries there will be)
string[] strings = new string[*]
//* I have to put in the (maximum)size of the array...



Then...
listBox1.Items.CopyTo(strings, 1);

foreach (string line in strings)
            {
                myWriter.WriteLine(line);
            }
myWriter.Close();



The problem with this is that you have to limit the size of the array.

P.S. The ORIGINAL problem was that whenever you myWriter.WriteLine(e.Message) it overwrites the previus entry...
I guess it has something to do with the myWriter.Close(); being in the same block of code as the writeline method.I don't know where else to put it...
 void LogFeedback(object sender, TextArgs e)
        {
            StreamWriter myWriter = new StreamWriter("Log.txt");
            myWriter.WriteLine(e.Message);
            myWriter.Close();
        }



ANY help apreciated....

Is This A Good Question/Topic? 1
  • +

Replies To: How to log apps?

#2 Amrykid   User is offline

  • D.I.C Lover
  • member icon

Reputation: 150
  • View blog
  • Posts: 1,589
  • Joined: 16-December 08

Re: How to log apps?

Posted 23 November 2010 - 06:32 AM

Instead of a array, use a List<string>. So you will not be limited to how many strings that can be logged.
Was This Post Helpful? 0
  • +
  • -

#3 CodderMan   User is offline

  • New D.I.C Head

Reputation: 10
  • View blog
  • Posts: 48
  • Joined: 18-November 10

Re: How to log apps?

Posted 23 November 2010 - 06:37 AM

I tried that I can't because of this part...
listBox1.Items.CopyTo(strings, 1);

(It lets you to copy the items ONLY to a object array... like string[])
Was This Post Helpful? 0
  • +
  • -

#4 Sergio Tapia   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1258
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: How to log apps?

Posted 23 November 2010 - 07:25 AM

Convert that List<string> to an Array when you need to use it. You can also look into the File.AppendText() method.
Was This Post Helpful? 1
  • +
  • -

#5 CodderMan   User is offline

  • New D.I.C Head

Reputation: 10
  • View blog
  • Posts: 48
  • Joined: 18-November 10

Re: How to log apps?

Posted 23 November 2010 - 07:52 AM

if you meant like this
List<string> strings1 = new List<string>();
string[] strings2 = strings1.ToArray<string>();

It isn't working...(Doesn't write anything)

As for the File.AppendText it has the same problem as from the first post(It overwrites the previous entry)...
Was This Post Helpful? 0
  • +
  • -

#6 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7480
  • View blog
  • Posts: 15,502
  • Joined: 16-October 07

Re: How to log apps?

Posted 23 November 2010 - 07:52 AM

I'm not sure why you need to store messages in an array. Or better yet, a list. You log something, you write it somewhere, you forget it. If you store messages, then your log itself can become a point of failure.

Here's a quick and dirty I found on my hard drive.
using System;
using System.Diagnostics;

class Logger {
	private static Logger instance = new Logger();
	public static Logger Instance { get { return instance; } }

	private StreamWriter logWriter;

	~Logger() { logWriter.Close(); }

	private Logger() {
		string filename = Application.StartupPath + Path.DirectorySeparatorChar + "Logger.log";
		logWriter = new StreamWriter(filename, false);
		logWriter.AutoFlush = true;
		WriteLine("Init");
	}

	public void WriteLine(object obj) {
		logWriter.WriteLine("[{0}] {1}", DateTime.Now, obj);
		logWriter.Flush();
		Debug.WriteLine(string.Format("[{0}] {1}", DateTime.Now, obj));
	}
}



Regardless of your approach, I'd have a global instance that you can simply throw messages at.

If you want to be clever, you can just hook into the Debug object with a listener and have that dump the data somewhere. I use Debug for all my feedback. In VS, it dumps out to the little output window and you can scroll back and see what actually happened.

More on logging in VS here: http://support.microsoft.com/kb/815788
Was This Post Helpful? 2
  • +
  • -

#7 Sergio Tapia   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1258
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: How to log apps?

Posted 23 November 2010 - 08:06 AM

Can you explain what this does? Thanks!

~Logger() { logWriter.Close(); }


By the way OP, this is the ouput window:

Posted Image

This post has been edited by Sergio Tapia: 23 November 2010 - 08:08 AM

Was This Post Helpful? 0
  • +
  • -

#8 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7480
  • View blog
  • Posts: 15,502
  • Joined: 16-October 07

Re: How to log apps?

Posted 23 November 2010 - 08:56 AM

Ah, feel the joy of C#'s C++ connection. :P

It's an explicit destructor: http://msdn.microsof...y/66x5fx1b.aspx

Basically, when the garbage collector gets around to killing the instance, it should try to execute that code. Since this is a Singleton, this should happen when the program exits. Knowing that the program may exit with an exception and not do a proper cleanup is why class flushes on every line.
Was This Post Helpful? 3
  • +
  • -

#9 CodderMan   User is offline

  • New D.I.C Head

Reputation: 10
  • View blog
  • Posts: 48
  • Joined: 18-November 10

Re: How to log apps?

Posted 23 November 2010 - 12:32 PM

View Postbaavgai, on 23 November 2010 - 02:52 PM, said:

I'm not sure why you need to store messages in an array. Or better yet, a list. You log something, you write it somewhere, you forget it. If you store messages, then your log itself can become a point of failure.

Here's a quick and dirty I found on my hard drive.
using System;
using System.Diagnostics;

class Logger {
	private static Logger instance = new Logger();
	public static Logger Instance { get { return instance; } }

	private StreamWriter logWriter;

	~Logger() { logWriter.Close(); }

	private Logger() {
		string filename = Application.StartupPath + Path.DirectorySeparatorChar + "Logger.log";
		logWriter = new StreamWriter(filename, false);
		logWriter.AutoFlush = true;
		WriteLine("Init");
	}

	public void WriteLine(object obj) {
		logWriter.WriteLine("[{0}] {1}", DateTime.Now, obj);
		logWriter.Flush();
		Debug.WriteLine(string.Format("[{0}] {1}", DateTime.Now, obj));
	}
}



Regardless of your approach, I'd have a global instance that you can simply throw messages at.

If you want to be clever, you can just hook into the Debug object with a listener and have that dump the data somewhere. I use Debug for all my feedback. In VS, it dumps out to the little output window and you can scroll back and see what actually happened.

More on logging in VS here: http://support.microsoft.com/kb/815788


Whenever it gets to this line
logWriter = new StreamWriter(filename, false);

it throws me an excepion

Quote

The process cannot access the file 'H:\Users\Matija\documents\visual studio 2010\Projects\Logging\Logging\bin\Debug\Logger.log' because it is being used by another process.

Was This Post Helpful? 0
  • +
  • -

#10 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7480
  • View blog
  • Posts: 15,502
  • Joined: 16-October 07

Re: How to log apps?

Posted 23 November 2010 - 12:57 PM

That error means that you have another program accessing the file. Chances are, the program you're working on has an instance that's still running and needs killing. If a program has a file lock, you won't even be able to delete the thing in file explorer.

For the moment, you can simply change the file name. If you continue to get file locking issues on your logfile, just tack a form of DateTime.Now onto the filename, so you always have a unique logging name.
Was This Post Helpful? 1
  • +
  • -

#11 MentalFloss   User is offline

  • .
  • member icon

Reputation: 619
  • View blog
  • Posts: 1,590
  • Joined: 02-September 09

Re: How to log apps?

Posted 23 November 2010 - 01:00 PM

There's already a great logging utility out there.
Check out http://logging.apach...4net/index.html
Was This Post Helpful? 3
  • +
  • -

#12 CodderMan   User is offline

  • New D.I.C Head

Reputation: 10
  • View blog
  • Posts: 48
  • Joined: 18-November 10

Re: How to log apps?

Posted 23 November 2010 - 01:08 PM

View Postbaavgai, on 23 November 2010 - 07:57 PM, said:

That error means that you have another program accessing the file. Chances are, the program you're working on has an instance that's still running and needs killing. If a program has a file lock, you won't even be able to delete the thing in file explorer.

For the moment, you can simply change the file name. If you continue to get file locking issues on your logfile, just tack a form of DateTime.Now onto the filename, so you always have a unique logging name.


I think you misunderstood me...
WHATEVER I make the filename it throws me that exception...
Was This Post Helpful? 0
  • +
  • -

#13 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7480
  • View blog
  • Posts: 15,502
  • Joined: 16-October 07

Re: How to log apps?

Posted 23 November 2010 - 01:13 PM

Well, some process had to create it... That's the one that's holding it. The nature of a singleton is that you can only have one instance, so it's hard to bite yourself like that.

This isn't in a ASP.NET app, is it?


The unique filename is still a good test. Try:
string filename = Application.StartupPath + Path.DirectorySeparatorChar + "Logger" + DateTime.Now.ToLongTimeString() + ".log";
logWriter = new StreamWriter(filename, false);


This post has been edited by baavgai: 23 November 2010 - 01:16 PM

Was This Post Helpful? 0
  • +
  • -

#14 CodderMan   User is offline

  • New D.I.C Head

Reputation: 10
  • View blog
  • Posts: 48
  • Joined: 18-November 10

Re: How to log apps?

Posted 23 November 2010 - 01:38 PM

The problem was an already started debug...
I don't know how i managed to start another...(since its a singleton)!
Thanks for all the info baavgai...
Was This Post Helpful? 0
  • +
  • -

#15 CodderMan   User is offline

  • New D.I.C Head

Reputation: 10
  • View blog
  • Posts: 48
  • Joined: 18-November 10

Re: How to log apps?

Posted 24 November 2010 - 05:45 AM

Scratch that previous post....
I still cant get it to work??!!
So as a last resort I'm posting all my code

my logger class:

using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;


namespace Logging
{
    class Logger
    {
        private static Logger instance = new Logger();
        public static Logger Instance { get { return instance; } }

        private StreamWriter logWriter;

        ~Logger() { logWriter.Close(); }

        public Logger()
        {
            string filename = Application.StartupPath + Path.DirectorySeparatorChar + "Logger.log";
            logWriter = new StreamWriter(filename, false);
            logWriter.AutoFlush = true;
            WriteLine("Init");
        }

        public void WriteLine(object obj)
        {
            logWriter.WriteLine("[{0}] {1}", DateTime.Now, obj);
            logWriter.Flush();
            Debug.WriteLine(string.Format("[{0}] {1}", DateTime.Now, obj));
        }
    }
}



and my form1 code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Logging
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            button1.Click += new EventHandler(button1_Click);
            button2.Click += new EventHandler(button1_Click);
            button3.Click += new EventHandler(button1_Click);
            button4.Click += new EventHandler(button1_Click);
        }
        Logger myLogger = new Logger();
        void button1_Click(object sender, EventArgs e)
        {
            myLogger.WriteLine("Buttton 1 is clicked...");
        }

        void button2_Click(object sender, EventArgs e)
        {
            myLogger.WriteLine("Buttton 2 is clicked...");
        }

        void button3_Click(object sender, EventArgs e)
        {
            myLogger.WriteLine("Buttton 3 is clicked...");
        }

        void button4_Click(object sender, EventArgs e)
        {
            myLogger.WriteLine("Buttton 4 is clicked...");
        }
    }
}

I only corrected the "private" part of the constructor to "public" so i can make a new [inline]Logger[/inline]



As an alternative I'm posting my project...

I have discovered that it reports that only in the debug version...
As for the release it works fine...

P.S. I guess you figured what I wand my program to do...
When you click one of four buttons it loggs that...

Oh and yes i made custom events just to experiment don't mind that...
Attached File  Logging.zip (71.6K)
Number of downloads: 69
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2