Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 52609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T16:56:02+00:00 2026-05-10T16:56:02+00:00

What would be the most efficient way of recording to a log (.txt) from

  • 0

What would be the most efficient way of recording to a log (.txt) from a console program on C# and .NET 2.2? My program loops multiple times always outputting different data based on what the user wants, so I’m searching for the most efficient way to achieve this.

I know I can always reopen a stream and then close it, but everytime I do that it would be writing just one line, then next time around (seconds later) the program reloops and needs tor write again. In my opinion, that doesn’t seem very resourse friendly.

I’m using multiple threads that all have output data that I want to log (opening/closing the same file or accessing the same file on different threads might be bad). The ‘holds a reference to a stream writer that auto-flushes’ sounds like a good idea, however I don’t know how to do that.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T16:56:02+00:00Added an answer on May 10, 2026 at 4:56 pm

    You could hook into the tracing framework that forms part of the CLR. Using a simple class like: http://www.chaosink.co.uk/files/tracing.zip you can selectively log diagnostic information. To use it add the class to your application. Create an inistance of the tracer in your class like:

    private Tracing trace = new Tracing('My.Namespace.Class'); 

    and call it using:

    MyClass() {     trace.Verbose('Entered MyClass');     int x = 12;     trace.Information('X is: {0}', x);     trace.Verbose('Leaving MyClass'); } 

    There are 4 levels of information in the inbuilt tracing framework:

    Verbose – To log program flow

    Information – To log specific information of interest to monitors

    Warning – To log an invalid state or recoverable exception

    Error – To log an unrecoverable exception or state

    To access the information from your application then add into the app.config (or web.config) the following:

    <system.diagnostics>     <trace autoflush='false' indentsize='4'>         <listeners>             <add name='myListener' type='System.Diagnostics.TextWriterTraceListener' initializeData='c:\mylogfile.log' />         </listeners>     </trace>     <switches>         <add name='My.Namespace.Class' value='4'/>           </switches> </system.diagnostics> 

    You can also attach listeners for publishing to the eventlog or anywhere else that interests you. More information on the tracing framework can be found at:

    http://msdn.microsoft.com/en-us/library/ms733025.aspx

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 70k
  • Answers 70k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Pedro Moreno and others at Google/Youtube work on it. They… May 11, 2026 at 12:49 pm
  • added an answer I'm not so sure that being a software architect is… May 11, 2026 at 12:49 pm
  • added an answer I believe I found the problem. I'm still not exactly… May 11, 2026 at 12:49 pm

Related Questions

What would be the most efficient data type to store a UUID/GUID in databases
What would be the most efficient method of reading a text file into a
Given a URL, what would be the most efficient code to download the contents
What would be the most optimal algorithm (performance-wise) to calculate the number of divisors
I am looking for a solution for detecting edge whitespace of c# bitmap, from
How do I multiply 10 to an Integer object and get back the Integer
What would be the most efficient way to do a paging query in SQLServer
This is the MySQL table structure that I have: itemID (BIGINT) userID (BIGINT) wasAdded

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.