How to create a LogFile...?  
Author Message
IamHuM





PostPosted: Visual C# General, How to create a LogFile...? Top

Hi...

I want to create a Log file in my application. I want to use this log file to put the text received & transmitted on my PC serial port throgh my C# application. I am receiving large text on my PC serial port. Is there is any special class to create a log file in C#.

Thanks in advance,

Vinay



Visual C#5  
 
 
David L





PostPosted: Visual C# General, How to create a LogFile...? Top

Well, there is the EventLog class, which lets you utilize the Windows event logs. It may be worth looking into. You can find more info about it and some simple examples here: http://msdn2.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx

Was that what you were looking for


 
 
vinay.p





PostPosted: Visual C# General, How to create a LogFile...? Top

Thanks for your reply

But i dont want that event log...

Actually in my application i am sending & receiving some data on my serial port,so i just want to make a Log file which will have all the transmitted & received data in it.

Is there is any class which will create a text file in a specified directory & with that class i can directly write in that text file with some .Write() function. or some similar function.

Thanks,

Vinay


 
 
ahmedilyas





PostPosted: Visual C# General, How to create a LogFile...? Top

you can use a System.IO.StreamWriter to write entries into a text file yes.

System.IO.StreamWriter theWriter = new System.IO.StreamWriter("path\file.txt");

theWriter.WriteLine("hello");

theWriter.Close();



 
 
David L





PostPosted: Visual C# General, How to create a LogFile...? Top

Ok, then perhaps a simple StreamWriter would do the trick :-)

The following should work:


sw.WriteLine("Writing my log entry");

You can find more information about it here: http://msdn2.microsoft.com/en-us/library/system.io.streamwriter.aspx

Is that more like it


 
 
vinay.p





PostPosted: Visual C# General, How to create a LogFile...? Top

Thanks,

I have some doubts for what you told me to do...can you please give some informtion on these things...

In my application if i am receiving data 10 times & i'm transmitting data 10 times then every time i have to open & close the file or no need of opening the file evrey time.

Only on the application exit i have to close the file...

Since i'll recieve & transmit large data wont it be a problem if everytime i have to open & close the file... & what if my file size becomes too large if i received very learge data which i logged in that Log file...

Vinay


 
 
ahmedilyas





PostPosted: Visual C# General, How to create a LogFile...? Top

Thanks,

I have some doubts for what you told me to do...can you please give some informtion on these things...

In my application if i am receiving data 10 times & i'm transmitting data 10 times then every time i have to open & close the file or no need of opening the file evrey time.

Only on the application exit i have to close the file...

Since i'll recieve & transmit large data wont it be a problem if everytime i have to open & close the file... & what if my file size becomes too large if i received very learge data which i logged in that Log file...

Vinay

in this case, the BEST thing you can do is write it to a string then at the end of your app, write it to the file. It will also be less expensive.

so globally, make a StringBuilder variable:

System.Text.StringBuilder theLogEntry = new System.Text.StringBuilder();

then whenever you are going to be writing to it from the same class:

this.theLogEntry.Append("new text");

on your application exit event or form_closing event, write the entire contents to disk:

System.IO.StreamWriter theWriter = new System.IO.StreamWriter("path\file.txt");

theWriter.Write(this.theLogEntry.ToString());

theWriter.Close();

and thats it. during runtime you will be appending your inputs/outputs to the string, declared globally. this will store your log



 
 
vinay.p





PostPosted: Visual C# General, How to create a LogFile...? Top

Thanks,

It will surely help me...

One last thing...wont that StringBuilder variable "theLogEntry" will cause any problem to my running application if data to logged goes beyond say 10000 or more lines... (means if my logged data becomes too large)

Can i check file size or stringbuilder length by any chance(say 100KB)... If it is greater than this size then close first & make another file...

I think file size check will be better option...

Is it possible...

Thanks for all your replies,

Vinay


 
 
ahmedilyas





PostPosted: Visual C# General, How to create a LogFile...? Top

im not sure about that to be honest. It would probably handle itself. If you are going to be storing a large amount of strings, I would advise against it as I still think this is going to be overkill but you could use a MemoryStream and append data to that. The memorystream appends to itself and uses the memory as a backing storage. Then you can write the contents to file afterwards:

http://msdn2.microsoft.com/en-us/library/system.io.memorystream.aspx

again, I would advise against it since it maybe overkill for what you are trying to do.

perhaps at regular intervals, using a timer or something, write the contents of the stringbuilder to disk (as shown previously) then clear out the stringbuilder and start over again but appending to the file, giving the second parameter "true" to append to the file.

this would be a work around to perhaps prevent any overruns or length exceeded/memory issues when appending to the stringbuilder if you are appending large amounts of strings in memory.