473,387 Members | 1,575 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

[C#] Saving Logs To A TXT File One At A Time

I have an application that reaches out and gathers stats, if those stats are out of bounds, it logs them.

Currently I have a string builder thats accumulates all logs durring the session and writes them all to the file at the end. This is a memory hog. Is it ok to open a text file every 2-4 seconds for 6 hours.
Aug 18 '08 #1
7 1419
Curtis Rutland
3,256 Expert 2GB
I don't see a problem with that. Although, you don't really have to open and close the file each time. If you aren't going to be reading from it, you can just open it at the beginning. Write each entry in real time, then close the file when the application exits.

Either way can work.
Aug 18 '08 #2
I don't see a problem with that. Although, you don't really have to open and close the file each time. If you aren't going to be reading from it, you can just open it at the beginning. Write each entry in real time, then close the file when the application exits.

Either way can work.
What method would you use to write to the file. I'm new to C# and I can't make it through my books fast enough to develop what I need. lol. Stupid 1600 page books.
Aug 18 '08 #3
Curtis Rutland
3,256 Expert 2GB
As long as nothing else needs to access it, I'd leave it open. But I'd make sure to catch any exceptions in the program at all, and in the finally statement, close the stream.
Aug 19 '08 #4
tlhintoq
3,525 Expert 2GB
I don't see a problem with that. Although, you don't really have to open and close the file each time. If you aren't going to be reading from it, you can just open it at the beginning. Write each entry in real time, then close the file when the application exits.

Either way can work.
Sounds great in theory but doesn't this pose a problem in the real world where power goes out, computers drop suddenly, networks go down while the destination file is on a server someplace else?

What happens to the file while it is open for 6 hours, when the power goes out and it is not closed properly?
Aug 19 '08 #5
Curtis Rutland
3,256 Expert 2GB
Yeah, didn't think about that. Of course, it's still possible for that to happen if the file is being open and closed every few seconds, but it does reduce the risk somewhat.
Aug 19 '08 #6
tlhintoq
3,525 Expert 2GB
After re-reading your question I had one other 'real world' consideration and option... <every 2-4 *seconds* for 6 hours a day> That really is just hammering the hard drive.

There are some good logging classes already out there
[HTML]http://www.codeproject.com/KB/cs/ObserverLogging.aspx?fid=854106&df=90&mpp=25&noise =3&sort=Position&view=Quick&fr=26#xx0xx[/HTML]
as well as the built in Trace function that have buffering built in. So you write several lines to your logger object and it remains in memory until you flush it, causing it to actually write to the drive. You could add a timer for 30-60 seconds and thus only write 1-2 times a minute instead of 20-30 times a minute, reducing drive wear and tear.

Pro: You aren't hammering the drive every 2 seconds.
Con: You could loose the last 20 seconds of log events if the power goes out. (but that' what smart UPS is for with USB connect so you get an event for the power out and can clean up nicely then shut down)
Aug 20 '08 #7
Our whole infrastructure is on UPS and generator. And the information isn't LifeSaving. So losing a few seconds of info won't hurt. I just want to make sure that in my newbie state, i don't implement something that will be processor/computer heavy. I am going to rewrite the whole project as its own self containted namespace so i can add it to another global app already in production.

Once I get that done I will make the changes to the saving times.

Thanks Everyone For Your Help!
Aug 20 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Wayno | last post by:
Hoping someone can help me out with this problem. First problem, is I am unable to view my php error log. In my php.ini I have my log in /var/www/logs/php_error_log. However, it has remained empty....
0
by: Tim Clark | last post by:
We've been having a lot of event ID 37 with src W3SVC errors popping up. We have 50+ sites in medium isolation, and i ran IISstate on the two instances of DLLHOST and the one of inetinfo. Is there...
0
by: marfi95 | last post by:
I'm automating a piece of my app to allow my customers to send me a copy of their event logs (for diagnostics) on demand just through the click of a button. I know I can use the...
2
by: Wolfgang B. | last post by:
Hi. I have a problem with many transaction logs generated by DB2 Version 7.2 on Redhat 7.3. Log file size (4KB) (LOGFILSIZ) = 1000 Number of primary log files ...
1
by: marfi95 | last post by:
I'm automating a piece of my app to allow my customers to send me a copy of their event logs (for diagnostics) on demand just through the click of a button. I know I can use the...
2
by: dayjah | last post by:
Hi, We are in the process of setting my mysql replication on mysql ver 4.1.18. However we have noticed an odd bug that occurs every so often. Every hour we issue flush logs to mysql so that we...
4
by: aj | last post by:
DB2 LUW v8.2 FP 14 RHAS 2.1 I have a DB2 online DB backup that was done w/ the INCLUDE LOGS option. I am interested in restoring that backup, and rolling forward ONLY the logs contained in the...
3
by: Kofa | last post by:
Hi, we have an application which uses online backups, so archive logging is a requirement. We have a small/moderate size database (a few hundred megabytes isn't much by today's standards) with...
0
by: shorti | last post by:
We are running DB2 UDB 8.1 fp 14 on AIX using archival logging and online backups. I was running some disaster tests and found my database restore was not restoring to the latest active log. ...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.