Hack Forums
[C#] Writing to a text file really inneficient [C#] - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C#] Writing to a text file really inneficient [C#] (/showthread.php?tid=6070288)



Writing to a text file really inneficient [C#] - draxyee - Apr 22, 2020

I'm using this code to write to a text file using many threads at the same time. (Parallel.Foreach a list) with a high thread count.

However, I've noticed that multi thread doesn't increase the speed from a normal foreach when using this method.

PHP Code

<?php 
public static void WriteDebug(string pathFile, string text)
 
           {
 
               try
                
{
 
                   locker.AcquireWriterLock(int.MaxValue);
 
                   File.AppendAllText(pathFile, text + Environment.NewLine);
 
               }
 
               finally
                
{
 
                   locker.ReleaseWriterLock();
 
               }
 
           } 

Is there any other efficient way to output a lot of data to somewhere (preferably to a file)?

I need it to be super super quick.

Cheers


RE: Writing to a text file really inneficient [C#] - __Digital__ - Apr 22, 2020

(Apr 22, 2020 01:49 PM)draxyee Wrote:  I'm using this code to write to a text file using many threads at the same time. (Parallel.Foreach a list) with a high thread count.

However, I've noticed that multi thread doesn't increase the speed from a normal foreach when using this method.

PHP Code

<?php 
public static void WriteDebug(string pathFile, string text)
 
           {
 
               try
                
{
 
                   locker.AcquireWriterLock(int.MaxValue);
 
                   File.AppendAllText(pathFile, text + Environment.NewLine);
 
               }
 
               finally
                
{
 
                   locker.ReleaseWriterLock();
 
               }
 
           } 

Is there any other efficient way to output a lot of data to somewhere (preferably to a file)?

I need it to be super super quick.

Cheers

try using streamwriter it might help you


RE: Writing to a text file really inneficient [C#] - Shadow_Cloud - Apr 22, 2020

I would strongly advise log4net for this


Code
class Program
    {
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        static void Main(string[] args)
        {
            log.Info("Hello logging world!");
            Console.WriteLine("Hit enter");
            Console.ReadLine();
        }
    }

Have a look here > https://stackify.com/log4net-guide-dotnet-logging/


RE: Writing to a text file really inneficient [C#] - Sickness - Apr 23, 2020

static Streamwriter text = new Steamwriter("file.txt", true);

That line in top of main.

Inside main:
text.Autoflush = true;
text.WriteLine(string);


RE: Writing to a text file really inneficient [C#] - Shadow_Cloud - Apr 23, 2020

(Apr 23, 2020 08:13 AM)Sickness. Wrote:  static Streamwriter text = new Steamwriter("file.txt", true);

That line in top of main.

Inside main:
text.Autoflush = true;
text.WriteLine(string);

By default, a StreamWriter is not thread safe.