|
-
August 5th, 2008, 11:41 AM
#1
[RESOLVED]Using textwriter
Hi all!
I code this function in order to create a log of my app.
My app calls this function every something wrong occurs. The file is created if it does not exist before.
The problem is that when I open the file, using the windows explorer, it is blank. So, any information was written in it. But, the function was called many times, as I could see debugging the app (the logMessage parameter is not empty anytime).
What is it wrong?
Code:
public void Log (String logMessage, String w) {
TextWriter textWriter = new StringWriter();
using (TextWriter streamWriter = new StreamWriter(w)) {
textWriter.Write("\r\nLog Entry : ");
textWriter.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(),
DateTime.Now.ToLongDateString());
textWriter.WriteLine(" :");
textWriter.WriteLine(" :{0}", logMessage);
textWriter.WriteLine("-------------------------------");
// Update the underlying file.
textWriter.Flush();
textWriter.Close();
}
}
Thank you in advance.
Last edited by gborges; August 6th, 2008 at 05:59 AM.
-
August 5th, 2008, 11:50 AM
#2
Re: Using textwriter
Edited to show a bit more:
Code:
public static class Logger
{
//Log Filename
private static string fileName = "output.txt";
public static void Write(string inString)
{
if (LogExists())
{
System.IO.StreamWriter sw;
sw = System.IO.File.AppendText(fileName);
sw.WriteLine(inString);
sw.WriteLine("\r\n-----------------------------------------------\r\n");
sw.Close();
}
}
private static bool LogExists()
{
if (System.IO.File.Exists(fileName))
return true;
else
{
try
{
System.IO.StreamWriter sw = System.IO.File.CreateText(fileName);
sw.Flush();
sw.Close();
return true;
}
catch
{
return false;
}
}
}
}
Last edited by crackersixx; August 5th, 2008 at 11:52 AM.
-
August 5th, 2008, 04:42 PM
#3
Re: Using textwriter
Here's one way of doing it. If you use the using block, there's no need to call Flush or Close.
Code:
public static void Log( string logMessage, string fileName )
{
StringBuilder sb = new StringBuilder( );
sb.AppendLine( String.Format( "{0}Log Entry : {1} {2}"
, Environment.NewLine
, DateTime.Now.ToLongTimeString( )
, DateTime.Now.ToLongDateString( ) ) );
sb.AppendLine( String.Format( " :{0} {1}", Environment.NewLine, logMessage ) );
sb.AppendLine( "-------------------------------" );
using( StreamWriter writer = new StreamWriter( fileName, true ) )
{
writer.Write( sb );
}
}
-
August 8th, 2008, 01:50 AM
#4
Re: Using textwriter
cracker and arjay !
it would be intersting if you correct the error in gborges' code or at least point what he is doing wrong.
-
August 8th, 2008, 02:49 AM
#5
Re: Using textwriter
Sometimes it's not worth the effort to correct something that isn't close to being correct.
For example a StreamWriter object (called streamWriter) is created using the String w parameter. I can only assume that 'w' is the file name. Anyway back to the streamWriter, although it's created properly inside a using block, it's never used.
What I would hope is the OP will look at the code I've taken the time to provide, try the code out to find out it works and then either use it or compare it with his[her] own code to determine the differences.
-
August 8th, 2008, 03:24 AM
#6
Re: [RESOLVED]Using textwriter
It si simple - you never write to streamWriter created on file, but you are writing to in memory textWriter, which content is never written to disk. Also, create the stream writer as arjay sugested with constructor accepting boolean flag indicating if the file shoul be just opened or created. You are creating always new file, so even if the log would be written to it, only the last will be kept.
- Make it run.
- Make it right.
- Make it fast.
Don't hesitate to rate my post. 
-
August 11th, 2008, 10:27 AM
#7
Re: Using textwriter
 Originally Posted by nabeelisnabeel
cracker and arjay !
it would be intersting if you correct the error in gborges' code or at least point what he is doing wrong.
I would really be curious to see some helpful posts coming from your side instead of naging and carping at other people posts. This isn't the standard forum behaviour, we are expecting from CG members. Instead of your 'curious' request you could have done help and shown up the errors, if you want to get it done.
Last edited by JonnyPoet; August 11th, 2008 at 10:29 AM.
 Jonny Poet
To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
If anyone felt he has got help, show it in rating the post.
Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
My latest articles :
Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|